Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parsing 4 Dr William Harrison Fall 2008 CS4430 – Compilers I.

Similar presentations


Presentation on theme: "Parsing 4 Dr William Harrison Fall 2008 CS4430 – Compilers I."— Presentation transcript:

1 Parsing 4 Dr William Harrison Fall 2008 CS4430 – Compilers I

2 Today Continuing the second phase parsing or grammatical analysis discovers the real structure of a program and represents it in a computationally useful way Predictive parsing also called recursive descent parsing Last time: basics of LL(1) parsing follow sets, table-driven predictive parsing, etc. Today: finish predictive parsing Reading: You should be reading Chapter 2 of Modern Compiler Design

3 Parsing concepts LanguageGrammar Parser * all inter-related, but different notions

4 Review: Parse Trees from Derivations S if E then S else S S begin S L S print E L end L ; S L E num = num S begin S L begin print E L begin print 1=1 L begin print 1=1 end S begin S L print E end 1 = 1 Parse Tree Associated with Derivation

5 The Process of constructing Parsers Construct CFG Recognize its Form Construct Parser Language Design Language Parser start again if not in appropriate form (e.g., LL(1), LR(1),…)

6 Predictive Parsing The first token on the RHS of each rule is unique. S if E then S else S S begin S L S print E L end L ; S L E num = num a.k.a. recursive descent If the first symbol on the r.h.s. of the productions is unique, then this grammar is LL(1).

7 Rolling your own Predictive Parsers int tok = getToken(); void advance() { tok = getToken(); } void eat(int t) { if (tok = t) { advance() } else { error(); } } void S(){switch(tok) { case IF: eat(IF); E(); eat(THEN); S(); eat(ELSE); S(); break; case BEGIN: … } void E() { eat(NUM); eat(EQ); eat(NUM); } … S if E then S else S S begin S L S print E L end L ; S L E num = num Has one function for each non-terminal, and one clause for each production

8 Road map for today Determining if a grammar is LL(1) First sets Follow sets Massaging a grammar into LL Using the technique of left factoring …and eliminating left recursion

9 Review: Table-driven Predictive Parsing 1.S E $ 2.E T E 3.E + T E 4.E - T E 5.E 6.T F T 7.T * F T 8.T / F T 9.T 10.F id 11.F num 12.F ( E ) SEETTFSEETTF + * id$ empty=error top of the parse stack front of token stream Actions are: predict( production ) match, accept, and error

10 Remaining Questions about Predictive Parsing We were given that grammar and prediction table Can all grammars be given a similar table Alas, no – only LL(1) grammars How did we come up with that table? First and Follow sets Techniques for converting some grammars into LL(1) Eliminating left-recursion Left factoring Well discuss these now

11 FIRST( ) is a sequence of symbols (terminal or non-terminal) For example, the right hand side of a production FIRST returns the set of all possible terminal symbols that begin any string derivable from. Consider two productions X and X If FIRST ( ) and FIRST ( ) have symbols in common, then the prediction mechanism will not know which way to choose. The Grammar is not LL(1)! If FIRST ( ) and FIRST ( ) have no symbols in common, then perhaps LL(1) can be used. We need some more formalisms.

12 FOLLOW sets and nullable productions FOLLOW(X) X is a non-terminal The set of terminals that can immediately follow X. nullable(X) X is a non-terminal True if, and only if, X can derive to the empty string λ. FIRST, FOLLOW & nullable can be used to construct predictive parsing tables for LL(1) parsers. Can build tables that support LL(2), LL(3), etc. X A X B C X d B a b FOLLOW(X) = ? X a B X B B d B λ nullable(X) = ?

13 FOLLOW sets and nullable productions FOLLOW(X) X is a non-terminal The set of terminals that can immediately follow X. nullable(X) X is a non-terminal True if, and only if, X can derive to the empty string λ. FIRST, FOLLOW & nullable can be used to construct predictive parsing tables for LL(1) parsers. Can build tables that support LL(2), LL(3), etc. X A X B C X d B a b FOLLOW(X) = { d, a } X a B X B B d B λ nullable(X) = true

14 Constructing the parse table X t For every non-terminal X and token t: X Enter the production (X ) if t FIRST( ), If X is nullable, enter the production (X ) if t FOLLOW( )

15 Constructing the parse table X t What if there are more than one production? X X

16 Constructing the parse table X t What if there are more than one production? X X Then the grammar cannot be parsed with predictive parsing, and it is (by definition) not LL(1).

17 Recursive descent renders a readable parser. depends on the first terminal symbol of each sub- expression providing enough information to choose which production to use. But consider a predictive parser for this grammar E E + T E T Shortcomings of LL Parsers void E(){switch(tok) { case ? : E(); eat(TIMES); T(); no way of choosing production case ? : T(); … } void T(){eat(ID);}

18 Consider this grammar its left-recursive Can not use LL(1) – why? Consider this alternative different grammar This derives the same language Now there is no left recursion. There is a generalization for more complex grammars. E T E E + T E E λ E E + T E T Eliminating left recursion

19 Consider Its not LL(1) – why? We can transform this grammar, and make it LL(1). S if E then S else S S if E then S S if E then S X X λ X else S Removing Common Prefixes * Remark: The resulting grammar is not as readable.

20 In Class Discussion (1) S S ; S S id := E E id E num E E + E How can we transform this grammar So that it accepts the same language, but Has no left recursion We may have to use left factoring

21 In Class Discussion (2) S S1 ; S was S S ; S S S1 S1 id := E E E + E1 was E E+E E E1 E1 id E1 num Write a grammar that accepts the same language, but Is not ambiguous Has no left recursion Prefix elimination doesnt work here

22 Class Discussion (3) S S1 S2 S1 id := E S2 ; S1 S2 S2 E E1 E2 E1 id E1 num E2 + E1 E2 E2 Write a grammar that accepts the same language, but … Has no left recursion Before: (# is a terminal) X X # Y X Y After: X Y X2 X2 # Y X2 X2 This final grammar is LL(1).

23 Next time LR Parsing LR(k) grammars are more expressive than LL(k) grammars, …but its not as obvious how to parse with them.


Download ppt "Parsing 4 Dr William Harrison Fall 2008 CS4430 – Compilers I."

Similar presentations


Ads by Google