Answered step by step
Verified Expert Solution
Question
00
1 Approved Answer
To be done in groups of two. Pick your partner!This is a continuation of the previous assignment. Now, we are going to generate an abstract
To be done in groups of two. Pick your partner!This is a continuation of the previous assignment. Now, we are going to generate an abstract syntax tree of the input, and evaluate the tree to compute a value for the expression. We can consider the abstract syntax tree as the meaning of the expression that was parsed.An abstract syntax tree is like a parse tree, except at the nodes we have operators, rather than nonterminals. For example, the abstract syntax tree for is given below. Note that there is no need for parenthesis in the tree.Our original grammar remains unchanged.G:GEE E T E T T T T F T F F F E M NM a b c dN This grammar is not suitable for top down recursive descent parsing because of left recursion.Removing left recursion, we get the grammar G:G: GEE TRR T RT R T FSS FS F S F E M NM a b c dN means the empty production, ie if T then T can derive the empty string.Now, we can write recursive functions for parsing according to G generating an abstract syntax tree as a result of parsing, and evaluating the returned abstract syntax tree to compute the value of the expression. We shall assume that a is a constant with value b is a constant with value c is a constant with value and d is a constant with value Below is the pseudocode of the parser, tree printer and evaluator. We shall need a way to represent trees, hence the class Node.Class Nodechar symbol; either an operator, or one of abcd Node leftChild; will be NULL if node is a leafNode rightChild; will be NULL if node is a leafBool error FALSE; char nexttoken ;void mainNode theTree; open file for reading theTree G; if not errorprintTreetheTree;value evaluatetheTree; print The value is value;else print input not parsed correctly G E Node G Node tree;lex;print G E;tree E;if nexttoken$ and errorprint "success"; return tree; else return NULL; E T R Node ENode temp;if error return NULL; print E T R;temp T;return Rtemp; R T R T R e Node RNode treeNode temp temp;if error return NULL;if nexttoken print R T R; lex;print failure: unconsumed inputs unconsumedinput;temp T;temp Rtemp;return new Node tree, temp;else if nexttokenprint R T R;lex;tempT;tempRtemp;return new Node tree, temp; else print Re;returntree; T F S Node TNode temp;if error return; print T F S; temp F;return Stemp;S F S F S e Node temp temp;if error return;if nexttoken print S F S;lex;tempF;tempStemp;return new Node tree, temp;else if nexttokenNode SNode treeprint S F Slex;tempF;tempStemp;return new Nodetree,temp; else print S e;returntree; F E N M Node FNode temp;if error return NULL;if nexttoken print F E ; lex;tempE;if nexttoken lex;returntemp; else errorTRUE;printerror: unexptected token nexttoken; printunconsumedinput unconusmedinput; return NULL;else if nexttoken is one of a or b or c or d print FM;return M;else if nexttoken is one of or or or print FN;returnN; else errorTRUE;printerror: unexptected token nexttoken;printunconsumedinput unconusmedinput; returnNULL; M a b c d Node Mchar prevtoken nexttoken;if error return NULL;if nexttoken is one of a or b or c or dprint M nexttoken;lex;return new Nodeprevtoken, NULL,NULL; else errorTRUE;printerror: unexptected token nexttoken; printunconsumedinput unconusmedinput; returnNULL; N Node Nchar prevtoken nexttoken;if error return NULL;if nexttoken is one of or or or print N nexttoken;lex;return new Nodeprevtoken, NULL,NULL; else errorTRUE;printerror: unexptected token nexttoken; printunconsumedinput unconusmedinput; returnNULL print tree in postfix notation void printTreeNode treeif treeNULL return; printTreetreeleftChild; printTreetreerightChild; printtreesymbol; compute the value of the expression int evaluateNode treeif treeNULL return ;if treesymbola return ;if treesymbolb return ;if treesymbolc return ;if treesymbold return ;if treesymbol one of return the value of tree.symbol;if treesymbol return evaluatetreeleftChild evaluatetreerightChild; if treesymbol return evaluatetreeleftChild evaluatetreerightChild; if treesymbol return evaluatetreeleftChild evaluatetreerightChild; if treesymbol return evaluatetreeleftChild evaluatetreerightChild;OK now that the parser is mostly written for you, here is what you will do:Write a Python program based on the pseudocode given above to parse expressions as defined by the grammar above, print the abstract syntax tree and compute its value. The input should be in a file. You should have global variables error of type Boolean and nexttoken of type char Define a function lex that gets the next character from the file and places it inside nexttoken. lex should skip any white spaces, such as newlines or the space character. The function unconusmedinput should return the remaining input in the file. The last character in the file should always be $ Define functions hint: class methods G E R T F and N Inside ma
To be done in groups of two. Pick your partner!This is a continuation of the previous assignment. Now, we are going to generate an abstract syntax tree of the input, and evaluate the tree to compute a value for the expression. We can consider the abstract syntax tree as the meaning of the expression that was parsed.An abstract syntax tree is like a parse tree, except at the nodes we have operators, rather than nonterminals. For example, the abstract syntax tree for is given below. Note that there is no need for parenthesis in the tree.Our original grammar remains unchanged.G:GEE E T E T T T T F T F F F E M NM a b c dN This grammar is not suitable for top down recursive descent parsing because of left recursion.Removing left recursion, we get the grammar G:G: GEE TRR T RT R T FSS FS F S F E M NM a b c dN means the empty production, ie if T then T can derive the empty string.Now, we can write recursive functions for parsing according to G generating an abstract syntax tree as a result of parsing, and evaluating the returned abstract syntax tree to compute the value of the expression. We shall assume that a is a constant with value b is a constant with value c is a constant with value and d is a constant with value Below is the pseudocode of the parser, tree printer and evaluator. We shall need a way to represent trees, hence the class Node.Class Nodechar symbol; either an operator, or one of abcd Node leftChild; will be NULL if node is a leafNode rightChild; will be NULL if node is a leafBool error FALSE; char nexttoken ;void mainNode theTree; open file for reading theTree G; if not errorprintTreetheTree;value evaluatetheTree; print The value is value;else print input not parsed correctly G E Node G Node tree;lex;print G E;tree E;if nexttoken$ and errorprint "success"; return tree; else return NULL; E T R Node ENode temp;if error return NULL; print E T R;temp T;return Rtemp; R T R T R e Node RNode treeNode temp temp;if error return NULL;if nexttoken print R T R; lex;print failure: unconsumed inputs unconsumedinput;temp T;temp Rtemp;return new Node tree, temp;else if nexttokenprint R T R;lex;tempT;tempRtemp;return new Node tree, temp; else print Re;returntree; T F S Node TNode temp;if error return; print T F S; temp F;return Stemp;S F S F S e Node temp temp;if error return;if nexttoken print S F S;lex;tempF;tempStemp;return new Node tree, temp;else if nexttokenNode SNode treeprint S F Slex;tempF;tempStemp;return new Nodetree,temp; else print S e;returntree; F E N M Node FNode temp;if error return NULL;if nexttoken print F E ; lex;tempE;if nexttoken lex;returntemp; else errorTRUE;printerror: unexptected token nexttoken; printunconsumedinput unconusmedinput; return NULL;else if nexttoken is one of a or b or c or d print FM;return M;else if nexttoken is one of or or or print FN;returnN; else errorTRUE;printerror: unexptected token nexttoken;printunconsumedinput unconusmedinput; returnNULL; M a b c d Node Mchar prevtoken nexttoken;if error return NULL;if nexttoken is one of a or b or c or dprint M nexttoken;lex;return new Nodeprevtoken, NULL,NULL; else errorTRUE;printerror: unexptected token nexttoken; printunconsumedinput unconusmedinput; returnNULL; N Node Nchar prevtoken nexttoken;if error return NULL;if nexttoken is one of or or or print N nexttoken;lex;return new Nodeprevtoken, NULL,NULL; else errorTRUE;printerror: unexptected token nexttoken; printunconsumedinput unconusmedinput; returnNULL print tree in postfix notation void printTreeNode treeif treeNULL return; printTreetreeleftChild; printTreetreerightChild; printtreesymbol; compute the value of the expression int evaluateNode treeif treeNULL return ;if treesymbola return ;if treesymbolb return ;if treesymbolc return ;if treesymbold return ;if treesymbol one of return the value of tree.symbol;if treesymbol return evaluatetreeleftChild evaluatetreerightChild; if treesymbol return evaluatetreeleftChild evaluatetreerightChild; if treesymbol return evaluatetreeleftChild evaluatetreerightChild; if treesymbol return evaluatetreeleftChild evaluatetreerightChild;OK now that the parser is mostly written for you, here is what you will do:Write a Python program based on the pseudocode given above to parse expressions as defined by the grammar above, print the abstract syntax tree and compute its value. The input should be in a file. You should have global variables error of type Boolean and nexttoken of type char Define a function lex that gets the next character from the file and places it inside nexttoken. lex should skip any white spaces, such as newlines or the space character. The function unconusmedinput should return the remaining input in the file. The last character in the file should always be $ Define functions hint: class methods G E R T F and N Inside ma
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started