Compiler Construction Sohail Aslam Lecture 15 Recusive Descent
- Slides: 15
Compiler Construction Sohail Aslam Lecture 15
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 nonterminal § Start with Expr; recall the grammar 3
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 ->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' 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* 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()) tree = op 3 ->AST(); return true; } else return false; } 8
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 ->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 == 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()) tree = op 3 ->AST(); return true; } else return false; } 12
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 == 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() ) syntax. Error(s); if(s->next. Token() != RPAREN) syntax. Error(s); s->advance(); tree = opr->AST(); return true; } return false; } 15
- Dr sohail aslam
- Dr sohail lectures
- Explicit and recursive formulas
- Sequences recursive formula
- Neraca lajur perusahaan aslam
- Aslam has to study four books
- Aslam anis
- Cross compiler in compiler design
- Yet another compiler compiler
- If the lexical analyzer finds a token invalid then?
- Advantages of interpreter
- Phases of compiler construction
- Compiler construction principles and practice
- Compiler front end and back end
- Lexical analysis in compiler construction
- Compiler construction: principles and practice