Solution 4 Week 9 Solutions Syntax Directed Translation 1 Write a syntax directed definition based on the following grammars to calculate the value of a binary number a B → B 0 | B 1 | 0 | 1 Productio[.]
Week Solutions Syntax-Directed Translation Write a syntax-directed definition based on the following grammars to calculate the value of a binary number a B → B | B | | Production B → B1 B → B1 B→ B→ Semantic rule B.val = B1.val * B.val = B1.val * + B.val = B.val = b B → B | B | | Production B → B1 B → B1 B→ B→ Semantic rule B.val = B1.val; B.len = B1.len + B.val = 2B1.len + B1.val ; B.len = B1.len + B.val = 0; B.len = B.val = 1; B.len = Given the following grammar for an assignment expression, write a syntaxdirected definition to check if the LHS of an assignment is id Otherwise, print out “error” E→T=E|T T→T+F|F F → id | num | ( E ) Production E → T = E1 E→T T→ T+F T→ F F → id F → num F→(E) Semantic rule if (!T.isLeft) print(“error”) E.isLeft = T.isLeft T.isLeft = false T.isLeft = F.isLeft F.isLeft = true F.isLeft = false F.isLeft = false Let a constant expression be an expression whose operands are constants or constant expressions Given the following grammar for an expression, construct a translation scheme to print out “Divide by zero” if the denominator in a divide expression is a constant-expression and its value is E → T E’ E’ → - T E’ | / T E’ | ∈ T → id | num | ( E ) E→T {E’.iConstant = T.oConstant; if (E’.iConstant) E’.ival = T.oval} E’ E’ → - T {E.oConstant = E’.oConstant; E.oval = E’.oval} {E1’.iConstant = T.oConstant && E’.iConstant; if (E1’.iConstant) E1’.ival = E’.ival - T.oval} E1’ E’ → / T {E’.oConstant = E1’.oConstant; if(E’.oConstant) E’.oval = E1’.oval} {if (T.oConstant && T.oval == 0) {print(“Divide by zero”); exit()} E1’.iConstant = T.oConstant && E’.iConstant; if (E1’.iConstant) E1’.ival = E’.ival / T.oval} E1’ E’ → ∈ T → id T → num T→ (E) {E’.oConstant = E1’.oConstant; if(E’.oConstant) E’.oval = E1’.oval} {E’.oConstant = E’.iConstant; if (E’.oConstant) E’.oval = E’.ival} {T.oConstant = false} {T.oConstant = true; T.oval = new Integer(num.Lexeme)} {T.oConstant = E.oConstant; if (E.oConstant) T.oval = E.oval} Transform the following translation scheme such that the attributes of its nonterminals can be evaluated by a top-down parser A → A1 B { A.a = f(A1.a, B.b) } A → C { A.a = g(C.c) } Read pages 304 of text book Transform the underlying grammar: A → C A’ A’ → B A1’ A’ → ∈ Transform the translation scheme: A → C { A’.i = g(C.c) } A’ {A.a = A’.o} A’ → B {A1’.i = f(A1’.i,B.b)} A’ {A’.o = A1’.o} A’ → ∈ {A’.o = A’.i} Transform the following translation scheme such that its semantic actions can be evaluated by a bottom-up parser D → T { L.in = T.type } L D → T [ ] { L.in = new ArrayType(T.type) } L T → int { T.type = new IntType() } T → float { T.type = new FloatType() } L → { L1.in = L.in } L1 , id { addtype(id.entry,L.in) } L → id { addtype(id.entry,L.in) } Transform the second production: D → T [ ] { M.i = T.type } M { L.in = M.o } L M → ∈ {M.o = new ArrayType(M.i) } Production D→TL D→T[]ML M→∈ T → int T → float L → L , id L → id Semantic rule val[ntop] = new ArrayType(val[top-2]) val[ntop] = new Integer() val[ntop] = new FloatType() addtype(val[top],val[top-3]) addtype(val[top],val[top-1])