Question 2: Precedence and context free grammars [6] ==================================================== ** Question tweaked for correction (announced during the exam in 2016) ** and changed from show parse tree to show derivation, ** (so folks don't have to incorporate hand drawn material ** into their exam submission) Suppose we have the following set of context free grammar rules, where non-terminals begin with an uppercase letter and terminals are shown in double-quotes: Program --> "begin", Statement, "end" Statement --> Variable, ":=", Expression Expression --> Expression, "#", SubExpr ** Expression --> SubExpr SubExpr --> SubExpr, "$", Value SubExpr --> SubExpr, "?", Value ** SubExpr --> Value Value --> Variable Value --> QuadInt Variable --> "w" Variable --> "x" Variable --> "y" Variable --> "z" QuadInt --> "0" QuadInt --> "1" QuadInt --> "2" QuadInt --> "3" Show a derivation for the following program, and explain what the precedence and associativity rules for the #, $, ? operators must be. begin x := 3 $ x # y ? z $ 2 end Sample solution: ================ Precedence and associativity: the # operators must be derived first, higher in parse tree, lower precedence while the $,? are of (equal) higher precedence the operations of equal precedence are done from left to right, again since we can see in the rules that the rightmost operator is the first one derived, thus highest in the parse tree, and last to actually be processed Derivation: ** showing the nonterminal we're processing at each point in derivation Program --> "begin", Statement, "end" Statement --> Variable, ":=", Expression Variable --> "x" Expression --> Expression, "#", SubExpr SubExpr --> SubExpr, "$", Value Value --> QuadInt QuadInt --> "2" SubExpr --> SubExpr, "?", Value Value --> Variable Variable --> "z" SubExpr --> Value Value --> Variable Variable --> "y" Expression --> SubExpr SubExpr --> SubExpr, "$", Value Value --> Variable Variable --> "x" SubExpr --> Value Value --> QuadInt QuadInt --> "3"