Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive decent parsing

Similar presentations


Presentation on theme: "Recursive decent parsing"— Presentation transcript:

1 Recursive decent parsing

2 Recursive Decent parsing
Recursive descent parsing associates a procedure with each nonterminal in the grammar, it may require backtracking of the input string.

3 Example S [ U$ U] W| [U]W W[U]|ε Program involves function s , u, v
Calls on these three functions corresponds to substitution for respective variable during a derivation There is a variable curr_ch whose value is assigned before any of the three functions is called.

4 Example Char curr_ch; void s(),u(),w() void match(char);
void get_ch(); void error(char); void main() { get_ch();s(); Cout<<endl<<”parsing complete” }

5 Example Void s() { Match(‘[‘);u(); match(‘$’);} Void u()
{ switch(curr_ch) Case’]’: match(‘]’); w(); break; Case ‘[‘: match(‘[‘); u(); match(‘]’); w(); break Default error }

6 void w() {if(curr_ch==’[‘{) match(‘[‘); u();}} void get_ch() { if(cin>>curr_ch)cout<<curr_ch; If(cin.eof() && curr_ch!=$) { cout<<”(end of input)}

7 void match(char this_ch)
{if(curr_ch==this_ch) getch(); else error(this_ch);else error(this_ch);} void error(char * some_char) { cout<< error}

8 Example 2 Example: <type> -> <simple> | ^ id | array [<sample>] of <type> <simple> -> integer | char | num dotdot num void type() { if (lookahead == INTEGER || lookahead == CHAR || lookahead==NUM) simple(); else if (lookahead == ‘^’) { match (‘^’); match(ID); } else if (lookahead == ARRAY) { match (ARRAY); match(‘[‘); match (‘]’); match (OF); type(); } else error(); }

9 void simple() { if (lookahead == INTEGER) match (INTEGER); else if (lookahead == CHAR) match (CHAR); else if (lookahead == NUM) { match(NUM); match(DOTDOT); } else error(); } void match(token t) { if (lookahead == t) {lookahead = nexttoken();} else error();


Download ppt "Recursive decent parsing"

Similar presentations


Ads by Google