/*Yacc specifications of simple desktop calculator*/
%{
#include ctype.h
%}
%token DIGIT
%%
line : expr '\n' {printf("%d\n",$1);}
expr : expr '+' term {$$=$1+$3;}
|term
term : term '*' factor {$$=$1*$3;}
|factor
factor : '('expr')' {$$=$2;}
|DIGIT
;
%%
yylex() {
int c;
c=getchar();
if(isdigit(c)){
yylval=c-'0';
return DIGIT;
}
return c;
}
OUTPUT:
$ yacc y1.y
$ cc y.tab.c -ly
$ ./a.out
2+3
5
$
Thursday, January 21, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment