Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Help me in question G , H , i Problem 4 (110 points). Consider the following CFG with the eight terminals: true, false, A, V,
Help me in question G , H , i
Problem 4 (110 points). Consider the following CFG with the eight terminals: true, false, A, V, !, ==, (, and ). expr true false expr A exprecpr V expr ! exprexpr == expr (expr) Indeed, the starting symbol is expr. Let's call this grammar G. This grammar is ambiguous, i.e., there exist at least two parse trees for some expression. For example, consider the following expression: !true false V true = true (a) Give two different derivations for this expression such that the corresponding parse trees are differ ent from each other. (10 points) (b) Give the corresponding parse trees for each derivation in the previous question. (10 points) (c) Give the corresponding two ASTs. (10 points) (Hint: Note that operators A. V, -- and I can appear as interior nodes in AST" You may remove the nonterminal expr from ASTs as it does not convey any computational information.) (d) If you pass the ASTs from the previous question to an evaluator, what would be the final value in each case? (10 points) (e) Now, let's disambiguate G. We want to impose the following precedence cascade among operators: The highest precedence is for parentheses, the second highest precedence is for !. the third highest precedence is for A, the fourth highest precedence is for V, and finally the least precedence is for Define the disambiguated version of the grammar in BNF You are free to define the operators A and V to be left-associative or right-associative. Let's call your disambiguated grammar G'. (10 points) left-associative or right-associative? What about operator V? (10 (f) In your defined G', is operator points) (g) Using G', give the derivation for the same expression as you did previously, i.e.. !true false V true == true Note that since G' is disambiguated, there must be a unique parse tree for this expression. (5 points) (h) Give the corresponding parse tree for the derivation in the previous question. (5 points) (i) Give the corresponding AST for the parse tree in the previous question. (5 points) () If you pass this AST to an evaluator, what would be the final result? (5 points) (k) Redefine G' using EBNF. Let's call this version of grammar G". (10 points) (1) Give the pseudo-code for the recursive-descent parser that implements G". The parser needs to generate the AST (so it is not a recognizer!). (20 points)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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