Exercises Text adapted from : Alessandro Artale, Free University of Bolzano Exercise: Grammar Rewriting Consider the following grammar for Boolean expressions: Bexp Bexp or Bterm Bterm Bterm Bterm and not ( Bexp ) id 1
Exercise: Grammar Rewriting Show the parse tree and the right-most derivation for the sentence not id or id and id Exercise: Grammar Rewriting Bexp Bexp or Bterm Bterm Bterm and id not id id 2
Exercise: Grammar Rewriting Bexp rm Bexp or Bterm rm Bexp or Bterm and rm Bexp or Bterm and id rm Bexp or and id rm Bexp or id and id rm Bterm or id and id rm or id and id rm not or id and id rm not id or id and id Exercise: Grammar Rewriting What is the precedence between the Boolean operators? Are they left- or rightassociative? 3
Exercise: Grammar Rewriting Precedence : not > and > or Left-associative : and, or Right-associative : not Exercise: Grammar Rewriting Rewrite the grammar in such a way that the operators and, or have the same precedence while the operator not has the highest precedence 4
Exercise: Grammar Rewriting Eliminate : Bexp Bexp or Bexp and not ( Bexp ) id Exercise: Grammar Rewriting Show the parse tree in the modified grammar for the sentence not id or id and id 5
Exercise: Grammar Rewriting Bexp Bexp and or id not id id Exercise: Grammar Rewriting What information is transmitted by the lexical analyzer to the parser when it recognizes an identifier and when it recognizes a boolean operator? 6
Exercise: Grammar Rewriting Identifier : id, pointer to symbol table Boolean symbol : relop, and, relop, or, etc. Exercise: Top-Down Parsing Define the notion of left-recursive grammar and explain why top down parsers cannot handle left-recursive grammars 7
Exercise: Top-Down Parsing Eliminate immediate left-recursion from the grammar Bexp Bexp or Bterm Bterm Bterm Bterm and not ( Bexp ) id Recall: Immediate Left- Recursion Elimination Method Rewrite every left-recursive production A A α A δ β γ into a right-recursive production: A β A R γ A R A R α A R δ A R ε 8
Exercise: Top-Down Parsing Unfold left recursion : not ( Bexp ) id Bterm Bterm R Bterm R and Bterm R ε Bexp Bterm Bexp R Bexp R or Bterm Bexp R ε Exercise: Top-Down Parsing Provide the parsing table for the LL(1) top down parser recognizing the grammar obtained from the previous point 9
Recall: FIRST() FIRST(α) = the set of terminals that begin all strings derived from α FIRST(a) = {a} if a T FIRST(ε) = {ε} FIRST(A) = A α FIRST(α) for A α P FIRST(X 1 X 2 X k ) : if for all j = 1,, i-1 : ε FIRST(X j ) then add FIRST(X i )\{ε} to FIRST(X 1 X 2 X k ) if for all j = 1,, k : ε FIRST(X j ) then add ε to FIRST(X 1 X 2 X k ) Exercise: Top-Down Parsing FIRST(a) = {a}, if a T = { and, or, not, (, } FIRST() = { not, (, id } FIRST(Bterm) = FIRST() = { not, (, id }, since cannot derive ε FIRST(Bterm R ) = { and, ε } FIRST(Bexp) = FIRST(Bterm) = { not, (, id }, since Bterm cannot derive ε FIRST(Bexp R ) = { or, ε } 10
Exercise: Top-Down Parsing A α FIRST(α) not not ( Bexp ) ( id id Bterm Bterm R not ( id Bterm R and Bterm R and Bterm R ε ε Bexp Bterm Bexp R not ( id Bexp R or Bterm Bexp R or Bexp R ε ε Recall: FOLLOW() FOLLOW(A) = the set of terminals that can immediately follow nonterminal A FOLLOW(A) = for all (B α A β) P do add FIRST(β)\{ε} to FOLLOW(A) for all (B α A β) P and ε FIRST(β) do add FOLLOW(B) to FOLLOW(A) for all (B α A) P do add FOLLOW(B) to FOLLOW(A) if A is the start symbol S then add $ to FOLLOW(A) 11
Exercise: Top-Down Parsing FOLLOW(Bexp) = { $, ) } FOLLOW(Bexp R ) = FOLLOW(Bexp) = { $, ) } FOLLOW(Bterm) = FIRST(Bexp R )\{ε} FOLLOW(Bexp) = { or } { $, ) } = { or, $, ) } FOLLOW(Bterm R ) = FOLLOW(Bterm) = { or, $, ) } Exercise: Top-Down Parsing FOLLOW() = FIRST(Bterm R )\{ε} FOLLOW(Bterm) FOLLOW(Bterm) = { and } { or, $, ) } { or, $, ) } = { and, or, $, ) } 12
Exercise: Top-Down Parsing A FOLLOW(A) and or $ ) Bterm or $ ) Bterm R or $ ) Bexp $ ) Bexp R $ ) Recall: Constructing an LL(1) Predictive Parsing Table for each production A α do for each a FIRST(α) do add A α to M[A, a] enddo if ε FIRST(α) then for each b FOLLOW(A) do add A α to M[A, b] enddo endif enddo Mark each undefined entry in M error 13
Bterm Bterm R Bexp Bexp R not or and ( ) id $ not Bterm Bterm R Bexp Bterm Bexp R Bterm R ε Bexp R or Bterm Bexp R Bterm R and Bterm R ( Bexp ) Bterm Bterm R Bexp Bterm Bexp R Bterm R ε Bexp R ε id Bterm Bterm R Bexp Bterm Bexp R Bterm R ε Bexp R ε Exercise: Top-Down Parsing Provide the stack and the moves of the LL(1) parser on input sentence not id or id 14
Example Table-Driven Parsing Stack $ Bx $ Bx R Bt $ Bx R Bt R Bf $ Bx R Bt R Bf not $ Bx R Bt R Bf $ Bx R Bt R id $ Bx R Bt R $ Bx R Input not id or id $ not id or id $ not id or id $ not id or id $ id or id $ id or id $ or id $ or id $ Production applied Bx Bt Bx R Bt Bf Bt R Bf not Bf Bf id Bterm R ε Bx R or Bt Bx R Exercise: Bottom-Up Parsing [ ] 15
Exercise: Semantic Analysis Consider the SDD in the next slide, where newlabel() generates a fresh symbolic label newtemp() generates a fresh variable name gen() generates strings code is the attribute with three-address code id.place is the name of the variable associated to the token id relop.op is a comparison operator (<, <=, =, ) Exercise: Semantic Analysis Productions Prog S S S 1 ; S 2 S while Test do { S 1 } Semantic rules S.next = newlabel(); Prog.code = S.code gen(s.next : ) S 1.next = newlabel(); S 2.next = S.next; S.code = S 1.code gen(s 1.next : ) S 2.code Test.begin = newlabel(); Test.true = newlabel(); Test.false = S.next; S 1.next = Test.begin; S.code = gen(test.begin : ) Test.code gen(test.true : ) S 1.code gen( goto Test.begin) 16
Exercise: Semantic Analysis Productions S id = E Test id 1 relop id 2 E E 1 + id E id Semantic rules S.code = E.code gen(id.place = E.place) Test.code = gen( if id 1.place relop.op id 2.place goto Test.true) gen( goto Test.false) E.place = newtemp(); E.code = E 1.code gen(e.place = E 1.place + id.place) E.Place = id.place; E.code = Exercise: Semantic Analysis Consider the input while x < y do { y = y 1; x = x + 1 } 17
Exercise: Semantic Analysis Provide The annotated parse tree (without the code attribute) for the input together with the values of the attributes The three-address code produced by the semantic actions for the given input The semantic rules for the production S if Test then S 1 18