Compiler Construction Sohail Aslam Lecture 15 Recusive Descent

  • Slides: 15
Download presentation
Compiler Construction Sohail Aslam Lecture 15

Compiler Construction Sohail Aslam Lecture 15

Recusive Descent Parser § Let’s consider the implementation of the C++ classes for the

Recusive Descent Parser § Let’s consider the implementation of the C++ classes for the nonterminal § Start with Expr; recall the grammar 2

Recusive Descent Parser § Let’s consider the implementation of the C++ classes for the

Recusive Descent Parser § Let’s consider the implementation of the C++ classes for the nonterminal § Start with Expr; recall the grammar 3

1 2 3 4 5 6 7 8 9 10 11 12 Goal expr'

1 2 3 4 5 6 7 8 9 10 11 12 Goal expr' term' factor → → → | | expr term expr' + term expr' - term expr' e factor term' * factor term' ∕ factor term' e number id ( expr ) 4

bool Expr: : is. Present() { Term* op 1 = new Term(s); if(!op 1

bool Expr: : is. Present() { Term* op 1 = new Term(s); if(!op 1 ->is. Present()) return false; tree = op 1 ->AST(); Eprime* op 2 = new Eprime(s, tree); if(op 2 ->is. Present()) tree = op 2 ->AST(); return true; } 5

1 2 3 4 5 6 7 8 9 10 11 12 Goal expr'

1 2 3 4 5 6 7 8 9 10 11 12 Goal expr' term' factor → → → | | expr term expr' + term expr' - term expr' e factor term' * factor term' ∕ factor term' e number id ( expr ) 6

bool Eprime: : is. Present() { int op=s->next. Token(); if(op==PLUS || op==MINUS){ s->advance(); Term*

bool Eprime: : is. Present() { int op=s->next. Token(); if(op==PLUS || op==MINUS){ s->advance(); Term* op 2=new Term(s); if(!op 2 ->is. Present()) syntax. Error(s); Tree. Node* t 2=op 2 ->AST(); tree = new Tree. Node(op, expr. Sofar, t 2); . . . 7

. . . Eprime* op 3 = new Eprime(s, tree); if(op 3 ->is. Present())

. . . Eprime* op 3 = new Eprime(s, tree); if(op 3 ->is. Present()) tree = op 3 ->AST(); return true; } else return false; } 8

1 2 3 4 5 6 7 8 9 10 11 12 Goal expr'

1 2 3 4 5 6 7 8 9 10 11 12 Goal expr' term' factor → → → | | expr term expr' + term expr' - term expr' e factor term' * factor term' ∕ factor term' e number id ( expr ) 9

bool Term: : is. Present() { Factor* op 1 = new Factor(s); if(!op 1

bool Term: : is. Present() { Factor* op 1 = new Factor(s); if(!op 1 ->is. Present()) return false; tree = op 1 ->AST(); Tprime* op 2 = new Tprime(s, tree); if(op 2 ->is. Present()) tree = op 2 ->AST(); return true; } 10

bool Tprime: : is. Present() { int op=s->next. Token(); if(op == MUL || op

bool Tprime: : is. Present() { int op=s->next. Token(); if(op == MUL || op == DIV){ s->advance(); Factor* op 2=new Factor(s); if(!op 2 ->is. Present()) syntax. Error(s); Tree. Node* t 2=op 2 ->AST(); tree = new Tree. Node(op, expr. Sofar, t 2); . . . 11

. . . Tprime* op 3 = new Tprime(s, tree); if(op 3 ->is. Present())

. . . Tprime* op 3 = new Tprime(s, tree); if(op 3 ->is. Present()) tree = op 3 ->AST(); return true; } else return false; } 12

1 2 3 4 5 6 7 8 9 10 11 12 Goal expr'

1 2 3 4 5 6 7 8 9 10 11 12 Goal expr' term' factor → → → | | expr term expr' + term expr' - term expr' e factor term' * factor term' ∕ factor term' e number id ( expr ) 13

bool Factor: : is. Present() { int op=s->next. Token(); if(op == ID || op

bool Factor: : is. Present() { int op=s->next. Token(); if(op == ID || op == NUM) { tree = new Tree. Node(op, s->token. Value()); s->advance(); return true; }. . . 14

if( op == LPAREN ){ s->advance(); Expr* opr = new Expr(s); if(!opr->is. Present() )

if( op == LPAREN ){ s->advance(); Expr* opr = new Expr(s); if(!opr->is. Present() ) syntax. Error(s); if(s->next. Token() != RPAREN) syntax. Error(s); s->advance(); tree = opr->AST(); return true; } return false; } 15