#include #include "recdesc.h" /* global variables */ TOKTYPE tstr [] = {IF, NUM, EQ, NUM, THEN, PRINT, ELSE, PRINT, NUM}; int tstr_ptr; TOKTYPE tok; void start (void) { tstr_ptr = 0; tok = tstr[0]; } void advance(void) { tstr_ptr = tstr_ptr + 1; tok = tstr[tstr_ptr]; } void error(void) { printf("Error in token %4d\n", tok); } void eat(TOKTYPE 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(); return 0; }