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