Note: expression is a double type and stringexp is a char * type.
Objective: To realize the mixed input mode of calculator and character string.
Status: Simple calculator and grammar expression ADD stringexp (for example: 3+4 3+4+“sdfe ") have been implemented.
Problem: the combination of char * string type itself and its combination with double calculator type, that is, stringexp ADD stringexp and stringexp ADD expression syntax, is not implemented. Multi-party testing, I think, is a problem of its grammatical analysis and construction, but the root cause has never been found. So I hope you can give me some suggestions, thank you.
Key: The code segment marked 1, 2 is bold and underlined. If the comment is cancelled, the test shows that the value of the string cannot be passed to the final statement.
The code is as follows:
/////////header file ch3hdr.h
# define NSYMS 20
Structural symbol table {
char * name
Int type;
double(* funcptr)();
Double value;
char * str
} symtab[NSYMS];
struct symtab * symlook();
//////////Lexical Analysis mylexer.l
%{
# contains "myparser.h"
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# including "ch3hdr.h"
# include & ltmath.h & gt
%}
%%
([0-9]+|([0-9]*\.[0-9]+)([eE][-+]? [0-9]+)? ) {
yylval . dval = atof(YY text); //itoa
Quantity returned; }
[\ t];
"+" { return ADD; }
“-”{ return DEC; }
“*”{ return MUL; }
“/”{ return DIV; }
"(" { return LPAR}
")" {Returns RPAR;; }
"=" {returns QUE;; }
","{returns a comma; }
“$”{ return 0; }
//[\"]([A-Za-z0-9 ]*)[\"] {
[\"]([^\"]*)[\"] {
yylval.sval = yytext
Returns a string; }
[A-Za-z][A-Za-z0-9]* {
struct symtab * sp = symlook(YY text);
yylval.symp = sp
Returns the name; }
[.\n] returns yytext [0];
%%
//////////////////Parse my parser . y
%{
# contains "mylexer.h"
# including "ch3hdr.h"
# include & ltstring.h & gt
# include & ltmath.h & gt
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# Define MAXBUFF 100
%}
% United {
Double dval
char * sval
struct symtab * symp
}
% token & ltsymp & gt name
% token & ltdval & gt number
% token & ltsval & gt line
%token QUE adds DEC MUL DIV LPAR RPAR comma.
% left plus decimal
% Left Multiplication Lattice
% sucrose-free aluminum
% type & ltdval & gt means
% type & ltsval & gtstringexp
%%
Statement _ List: Statement' \n'
| Statement _ List Statement' \n'
Statement: named QUE expression {$1->; Value = $3; $ 1->; type = 1; Printf("%s is %g\n ",$1->; Name, $3); }
| name questring exp {$1->; Value = 0; $ 1->; str = strdup($ 3); $ 1->; type = 2; Printf("string %s is %s\n ",$1-> Name, $1-> str); }
| expression { printf(" expression = % g \ n ",$ 1); }
| string exp { printf(" string exp = % s \ n ",$ 1); }
stringe XP:expression ADD stringe XP { $ $ =(char *)malloc(sizeof(char *)max buff); sprintf($$," %g%s ",$ 1,$ 3); }
//| stringexp ADD stringexp { $ $ =(char *)malloc(sizeof(char *)max buff); sprintf($$," %s%s\n ",$ 1,$ 3); } - 1
//| string exp ADD expression { $ $ =(char *)malloc(sizeof(char *)max buff); } - 2
| STRING { strrev(strn set(strrev(strn set($ 1),' ', 1)),' ', 1); sprintf($$," %s ",$ 1); }
expression:expression ADD expression { $ $ = $ 1+$ 3; }
| NUMBER { $ $ = $ 1; }
| expression DEC expression { $ $ = $ 1-$ 3; }
| expression MUL expression { $ $ = $ 1 * $ 3; }
| expression DIV expression {
If ($3=0.0)
Yyerror ("divide by zero");
other
$$ = $ 1 / $3;
}
| DEC expression% prec UMINUS {$ $ =-$ 2;; }
| LPAR expression RPAR {$ $ = $ 2;; }
| NAME { $ $ = $ 1->; Value; }
| name LPAR expression RPAR {
if($ 1->; funcptr)
$ $ =($ 1->; funcptr)($ 3);
Otherwise {
Printf("%s is not a function \n ",$1-> Name);
$$=0.0;
}
}
| name LPAR expression comma expression RPAR {
if($ 1->; funcptr)
$ $ =($ 1->; funcptr)($3,$ 5);
Otherwise {
Printf("%s is not a function \n ",$1-> Name);
$$=0.0;
}
}
%%
Structural symbol table * symbol table (characters)
{
struct symtab * sp
for(sp = symtab; sp & lt& ampsymtab[NSYMS]; sp++)
{
if(sp-& gt; Name and name. & amp! strcmp(sp-& gt; Name)
Return to sp;
If (! sp-& gt; Name)
{
sp-& gt; name = strdup(s);
Return to sp;
}
}
Yyerror ("too many symbols");
Exit (1);
}
addfunc(char *name,double (*func)())
{
struct symtab * sp = symlook(name);
sp-& gt; funcptr = func
}
Major (invalid)
{
extern double sqrt(),exp(),log(),atan2(),fmod();
addfunc("sqrt ",sqrt);
addfunc("exp ",exp);
Addfunc("log ");
addfunc("atan2 ",atan 2);
addfunc("fmod ",fmod);
YY parse();
}
reprint