









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth look into the implementation of the c++ classes for the nonterminals expr, eprime, term, and tprime in a recursive descent parser. The grammar rules for each nonterminal are presented, followed by the code for their respective ispresent() functions. These functions are responsible for parsing the input stream and constructing the parse tree.
Typology: Slides
1 / 15
This page cannot be seen from the preview
Don't miss anything!










2
4 1 Goal → expr 2 expr → term expr' 3 expr' → + term expr' 4 | - term expr' 5 | ε 6 term → factor term' 7 term' → (^) * factor term' 8 | ∕ factor term' 9 | ε 10 factor → number 11 | id 12 | ( expr )
5 bool Expr::isPresent() { Term* op1 = new Term(s); if(!op1->isPresent()) return false; tree = op1->AST(); Eprime* op2 = new Eprime(s, tree); if(op2->isPresent()) tree = op2->AST();
7 bool Eprime::isPresent() { int op=s->nextToken(); if(op==PLUS || op==MINUS){ s->advance(); Term* op2=new Term(s); if(!op2->isPresent()) syntaxError(s); TreeNode* t2=op2->AST(); tree = new
8
Eprime* op3 = new Eprime(s, tree); if(op3->isPresent()) tree = op3->AST(); return true; } else return false; }
10 bool Term::isPresent() { Factor* op1 = new Factor(s); if(!op1->isPresent()) return false; tree = op1->AST(); Tprime* op2 = new Tprime(s, tree); if(op2->isPresent()) tree = op2->AST(); return true; }
11 bool Tprime::isPresent() { int op=s->nextToken(); if(op == MUL || op == DIV){ s->advance(); Factor* op2=new Factor(s); if(!op2->isPresent()) syntaxError(s); TreeNode* t2=op2->AST(); tree = new TreeNode(op,exprSofar,t2); ......
13 1 Goal → expr 2 expr → term expr' 3 expr' → + term expr' 4 | - term expr' 5 | ε 6 term → factor term' 7 term' → (^) * factor term' 8 | ∕ factor term' 9 | ε 10 factor → number 11 | id 12 | ( expr )
14 bool Factor::isPresent() { int op=s->nextToken(); if(op == ID || op == NUM) { tree = new TreeNode(op,s->tokenValue()); s->advance(); return true; }
......