Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Descent Technique CMSC 331. UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D ->

Similar presentations


Presentation on theme: "Recursive Descent Technique CMSC 331. UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D ->"— Presentation transcript:

1 Recursive Descent Technique CMSC 331

2 UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D -> [ '!' ] E E -> '(' A ')' | NUMBER where NUMBER is a int in C */ #include #include extern int lookahead ;

3 UMBC 3 int A () { int B(); int value; value = B(); for (;;){ switch (lookahead) { case '|' : Match('|'); value = value | B(); continue; default: return (value); } } } A( ) A -> B { '|' B }

4 UMBC 4 B( ) int B () { int value; int C(); value = C(); for (;;){ switch (lookahead) { case '&' : Match('&'); value = value & C(); continue; default: return (value); } } } B -> C { '&' C }

5 UMBC 5 C( ) int C () { int value; int D(); value = D(); for (;;){ switch (lookahead) { case '^' : Match('^'); value = value ^ D(); continue; default: return (value); } } } C -> D { '^' D }

6 UMBC 6 D( ) int D () { int value; int E(); if (lookahead == '!') { Match('!'); value = E(); value = ~ value; return (value); } else return (E()); } D -> [ '!' ] E

7 UMBC 7 E( ) int E () { int value; int A(); int get_number(); if ((lookahead >= '0') && (lookahead <= '9')) value= get_number(); else { Match('('); value = A(); Match( ')'); } return value; } E -> NUMBER | '(' A ')'

8 UMBC 8 Getting the Next Token #include #include int get_next_char(){ int ch ; ch = getchar(); if ((ch == ' ') || ( ch == '\t') || ( ch == '\n')) return get_next_char(); else return ch ; }

9 UMBC 9 The Matcher /* The function to use to match the lookahead */ void Match(int t) { if (lookahead !=t){ error(t, lookahead, "Syntax error"); } else lookahead = get_next_char(); }

10 UMBC 10 Reporting Errors void error(char e, char g, char*s) { printf("%s - Expected '%c' - Got '%c' \n", s, e, g); lookahead = get_next_char(); }

11 UMBC 11 Main Driver int main () { int value; int A(); int i; lookahead = get_next_char(); i = 1; do { value = A(); printf ("%d)\tValue = %d\n", i++,value); } while (lookahead != EOF); }


Download ppt "Recursive Descent Technique CMSC 331. UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D ->"

Similar presentations


Ads by Google