%{ #include #include #include #include "lex.yy.c" extern int yylval; int symcount=0; int stk[1000]; int sptr=1; int temp; typedef int YYSTYPE; %} %token IF %token ELSE %token ENDIF %token ID %token ASSIGN %token NUM %token PRINT %left '<' '>' GE LE EQ NE %left '+' '-' %left '*' '/' %left UMINUS %token THEN %start stmtlist %% stmtlist: stmtlist stmt | stmt ; stmt: ID ASSIGN expr ';' { if (top() >=1) sym_tbl[$1].value=$3;} | PRINT expr ';' { if (top() >=1 ) printf("%d\n", $2); } | IF expr { if (top() >=1) push ($2 != 0); else push(0);}THEN stmtlist {pop();} ELSE {if (top() >=1) push ($2 ==0); else push(0);} stmtlist {pop();} ENDIF; ; expr : expr '+' expr { if (top() >= 1)$$=$1+$3;} | expr '-' expr { if (top() >= 1) $$ =$1-$3; } | expr '/' expr { if (top() >=1 )$$ = $1/$3;} | expr '*' expr { if (top() >=1) $$ = $1*$3; } | expr '>' expr { if (top()>=1) {if ($1 > $3) $$=1; else $$=0;}} | expr '<' expr { if (top() >=1 ){if ($1 < $3) $$=1; else $$=0;}} | expr GE expr { if (top() >=1 ) {if ($1 >= $3) $$=1; else $$=0;}} | expr LE expr { if (top() >=1 ) { if ($1 <= $3) $$=1; else $$=0;}} | expr EQ expr { if (top() >=1 ) {if ($1 == $3) $$=1; else $$=0;}} | expr NE expr { if (top() >=1) { if($1 != $3) $$=1; else $$=0;}} | '(' expr ')' { $$ = $2;} | ID { $$=sym_tbl[$1].value;} | NUM {$$ = sym_tbl[$1].value;} ; %% void main() { do { stk[0]=1; sptr=1; yyparse(); } while(!feof(stdin)); } void yyerror(char *msg) { printf("%s\n",msg); } void push(int x) { stk[sptr++]=x; } void pop(void) { sptr--; } int top(void) { return stk[sptr-1];}