Compiler Construction Chapter 4 Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Parsing Determining whether a string is derivable a grammar or not. Two approaches based on the order in which a parse tree is constructed are given below: Top-down parsing ∙ Starts construction at root of parse tree and proceeds to children. ∙ Uses LMD Bottom-up parsing ∙ Starts at leaves and proceeds to root ∙ Uses reverse or rightmost derivation Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Top Down Parsing Example: of top-down parsing using a subset of the types of Pascal (the token dotdot stands for "..", and "^" means a pointer) type --> simple | id | array [ simple ] of type simple --> integer | char | num dotdot num The top down construction of a parse tree for the string array [ num dotdot num ] of integer Compiler Construction Dr K. V. N. Sunitha
Array [Num Dotdot Num] of Integer Grammar type --> simple | id | array [ simple ] of type simple --> integer | char | num dotdot num Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Bottom up parsing: S ->aABe A - > Abc | b B -> d , w= abbcde abbcde aAbcde aAde aABe S Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Types of Parsers Top down parsers Recursive descent (predictive /non-predictive) parsers Non-recursive descent predictive parser, i.e. LL(1) Bottom up parsers LR parsers LR(0) – Least powerful than SLR Simple LR, i.e. SLR(1) – Simple and least powerful. Look ahead LR, i.e. LALR(1) – power and cost is intermediate between the two Canonical LR, i.e. CLR(1)/LR(1) – Most powerful and costly Compiler Construction Dr K. V. N. Sunitha
Compiler Construction To design predictive parsers, grammar has to be free of left recursion and should be left factored. Recursive descent predictive parser for parsing i+i using E→ E+ T | T, T→ i After eliminating left recursion ‘G’ is, E → TE’ , E’ → +TE’ |ε , T → i $ Compiler Construction Dr K. V. N. Sunitha
Compiler Construction { if (L==‘+’) Match(‘+’); T(); E’(); } else return; Match(chat t) { if (t==L) L=getchar(); else Printf(“err\n”); } char L; L=getchar(); main() { E(); E() { T(); E’(); } T() { Match(i); } if (L==“$”) printf(“successful”); } Compiler Construction Dr K. V. N. Sunitha
Non-recursive Predictive Parsers Avoid recursion for efficiency reasons Used widely INPUT BUFFER a + b $ X Y Z $ STACK LL(1) Parser Output Parsing Table Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Parsing Algorithm Let X be the grammar symbol on top of stack, ‘a’ is current input symbol Stack contents and remaining input called parser configuration (initially push $ then start symbol S on to stack and read complete input string in input buffer) If X=a=$ halt and announce successful completion If X=a ≠ $ pop X off stack advance input ptr to next symbol If X is a nonterminal, the parser consults parsing table entry M[X,a]. This parsing table entry will be either a production of the grammar or blank entry. If, for example, X->UVW replace X with WVU (U on top) Output the production used above Compiler Construction Dr K. V. N. Sunitha
Example: input string: id + id *id E → TE’ E’ → +TE’ | ε T → F T’ T’ → *FT’ | ε F → id |(E) Example: input string: id + id *id Parsing table: id + * ( ) $ E E→TE’ E’ E’→+TE’ E’→ε T T→FT’ T’ T’→ε T’→*FT’ F F→id F→(E) Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Construction of Parsing Table Requires Two Functions: First() and Follow(); Evaluated by following 2 rules If α is terminal, First(X)=(X) If X is a non-terminal (a) & has null production (i.e.) X →ε , First(X)=(ε) (b) & has non null production, X →X1X2X3 First(X)=First(X1X2X3) = First(X1) if X1 => ε First(X)=First(X1)-{ε} U First(X2X3 ) if X1 => ε * * Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Expr Grammar Expr Grammar without Left Recursion E → E + T | T T → T * F | F F → id |(E) E → TE’ E’ → +TE’ | ε T → F T’ T’ → *FT’ | ε F → id |(E) First() E {id, ( } E’ {+, ε} T T’ {*, ε} F Compiler Construction Dr K. V. N. Sunitha
Compiler Construction More Examples A → Bb | C d B → a B | ε C → c C | ε First(A) = {a,b, c,d}, First (B) = {a, ε},First (C) = {c, ε} S → ABCDE A → a | ε B → b | ε C → c | ε D → d | ε E → e First(S)= ? Compiler Construction Dr K. V. N. Sunitha
Compiler Construction S → ACB | C bB | Ba A → da | BC B → g | ε C → h | ε First(S) = ? First(A) = ? First(B) = ? First(C) = ? Compiler Construction Dr K. V. N. Sunitha
Follow(A): Set of Terminals That May Follow Immediately to Right of A Evaluted by 3 rules If A is start symbol, Follow (A) = {$} If S → α A β is in G, Follow (A) = First(β) –{ε } If S → α A or S → α A β {β => ε}, Follow (A) = Follow (S); * Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Expr Grammar without left recursion Expr Grammar E → TE’ E’ → +TE’ | ε T → FT’ T’ → *FT’ | ε F → id |(E) E → E + T | T T → T * F | F F → id |(E) First() E {id, ( } E’ {+, ε} T T’ {*, ε} F Follow() { ), $ } { +, ),$} {*,+,),$} Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Examples - Follow( ) 1. S → a AB b A → c | ε B → d | ε Follow (A)={ b, d} Follow (B)={ b} Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Examples - Follow( ) 2. S → a B D h B → c C C → b C | ε D → E F E → g | ε F → f | ε Follow (B) = ? = Follow (C) Follow( D ) = ? Follow (E) = ? Follow (F)= ? Compiler Construction Dr K. V. N. Sunitha
LL(1) Parsing Table Construction For each production A → α , repeat the following steps. For each terminal ‘a’ in first(α), add A → α to M [A ,a] If ε is in First(α), add A → α to M [A,b] for each ‘b’ in Follow (A). Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Construct LL(1) parser to parse string (a,a) G is S → ( L ) | a, L → L , S | S. After eliminating left recursion, S → ( L ) | a L → S L’ L’ → , S L’ | є a , ( ) $ S S → a S → (L) L L → S L’ L’ L’ → , S L’ L’ → є. Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Construct LL(1) parser S → AaBb |AbBa, A → є, B → є Check whether above grammar is LL(1)? Compiler Construction Dr K. V. N. Sunitha