Number (0-9) + %% (/ Skip Blanks /) (Number) (Sscanf (Yytext,"%lf",&yylval) Return INTEGER ) - (Return Yytext (0) ) /N (Return Yytext (0) )
Number (0-9) + %% (/ Skip Blanks /) (Number) (Sscanf (Yytext,"%lf",&yylval) Return INTEGER ) - (Return Yytext (0) ) /N (Return Yytext (0) )
Number (0-9) + %% (/ Skip Blanks /) (Number) (Sscanf (Yytext,"%lf",&yylval) Return INTEGER ) - (Return Yytext (0) ) /N (Return Yytext (0) )
Assignment No 03:
We want to construct a simple pocket-calculator program using yacc and lex
which can parse strings such as 1+(10-5-3)*5+2 and print the result, 13 in this
case. Outline the overall structure of your program components. Give full
details of the input to yacc and lex. (Precise syntactic details are not important,
but your answer should reflect basic principles involved)
$lex calculator.l
$yacc calculator.y
$cc y.tab.c –ly -ll
****************Lexical Analyzer**************************
number [0-9]+
%%
[ ] { /*skip blanks*/}
{number} {
sscanf(yytext,"%lf",&yylval);
return INTEGER;
}
. {return yytext[0];
}
\n {return yytext[0];
}
%token INTEGER
%left '+' '-'
%left '*'
%right UMINUS
%%
lines : lines expr '\n' {printf("%g\n",$2);}
| lines '\n'
|
| error '\n' {yyerror("reenter last line :");
yyerrok;}
;
%%
#include "lex.yy.c"