#include #include "recdesc.h" /* global variables */ int tok; int tokval; extern FILE *yyin; extern int yylex(); extern char *yytext; void start (void) { tok = yylex(); tokval = atoi(yytext); /* printf("Recognized %d\n", tok); */ } void advance(void) { tok = yylex(); tokval = atoi(yytext); /* printf("Recognized %d\n", tok); */ } void stop (void) { fclose(yyin); } void error(void) { printf("Error in token %4d\n", tok); } int eat(enum token t) { int cur_tokval; printf("Reading token %4d, expecting %4d\n", tok, t); cur_tokval = tokval; if (tok == t) advance(); else error(); return cur_tokval; } /* Implementation of the grammar */ int E (void) { int l, r; l = eat(NUM); eat(EQ); r = eat(NUM); return (l == r);} int S (void) { int e, l, r; switch (tok) { case IF: eat(IF); e = E(); eat(THEN); l = S(); eat(ELSE); r = S(); if (e) return l; else return r; break; case PRINT: eat(PRINT); return (eat(NUM)); break; default: error(); } } /* Main function */ int main (void) { int yylexres; start(); printf("Evaluation yields %d\n", S()); stop(); return 0; }