LL(1) Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 7.

Slides:



Advertisements
Similar presentations
AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
Advertisements

Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
YANGYANG 1 Chap 5 LL(1) Parsing LL(1) left-to-right scanning leftmost derivation 1-token lookahead parser generator: Parsing becomes the easiest! Modifying.
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
Top-Down Parsing.
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
Top-Down parsing LL(1) parsing. Overview of Top-Down  There are only two actions 1.Replace 2.Match.
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()
Top-Down Parsing.
Parsing Chapter 4 Parsing2 Outline Top-down v.s. Bottom-up Top-down parsing Recursive-descent parsing LL(1) parsing LL(1) parsing algorithm First.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
Top-Down Parsing - recursive descent - predictive parsing
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
C Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 6, 10/02/2003 Prof. Roy Levow.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Exercise 1 A ::= B EOF B ::=  | B B | (B) Tokens: EOF, (, ) Generate constraints and compute nullable and first for this grammar. Check whether first.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
LL(1) Parser. What does LL signify ? The first L means that the scanning takes place from Left to right. The first L means that the scanning takes place.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Context-Free Languages Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.
Top-Down Parsing.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Fangfang Cui Mar. 29, Overview 1. LL(k) 1. LL(k) Definition 2. LL(1) 3. Using a Parsing Table 4. First Sets 5. Follow Sets 6. Building a Parsing.
Building AST's for RPAL Programs
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
Lecture #12 Parsing Types.
LL(1) grammars Module 07.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Recursive Descent Parsing
Fixing non-ll(1) grammars
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Compiler design Bottom-up parsing: Canonical LR and LALR
Top-down derivation tree generation
Lecture 7 Predictive Parsing
Recursive Descent Parsing
Bottom-up AST, original grammar
Top-down derivation tree generation
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Bottom-up derivation tree, original grammar
Bottom-up derivation tree, original grammar
TaBle-driven LL(1) Parsing
Replacing recursion with iteration
Bottom-up AST, original grammar
Bottom-up derivation tree generation
TaBle-driven LL(1) Parsing
Fixing non-ll(1) grammars
Computing Follow(A) : All Non-Terminals
Building AST's for RPAL Programs
Replacing recursion with iteration
Lecture 7 Predictive Parsing
Programming Language Principles
Bottom-up derivation tree generation
Compiler Construction
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Programming Language Principles
Programming Language Concepts
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

LL(1) Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 7

Example Build the LL(1) parse table for the following grammar. S → begin SL end{begin} → id := E;{id} SL → SL S{begin,id} → S{begin,id} E → E+T{(, id} → T{(, id} T → P*T{(, id} → P{(, id} P → (E) {(} → id {id} * * * * - not LL(1)

Example (cont’d) Lemma: Left recursion always produces a non-LL(1) grammar (e.g., SL, E above) Proof: Consider A → A  First (  ) or Follow (A) →  First (  ) Follow (A)

Problems with our Grammar 1.SL is left recursive. 2.E is left recursive. 3.T → P * T both begin with the same → P sequence of symbols (P).

Solution to Problem 3 Change: T → P * T { (, id } → P { (, id } to: T → P X { (, id } X → * T { * } → { +, ;, ) } Follow(X) Follow(T) due to T → P X Follow(E) due to E → E+T, E → T = { +, ;, ) } due to E → E+T, S → id := E ; and P → (E) Disjoint!

Solution to Problem 3 (cont’d) In general, change A →  1 →  2... →  n to A →  X X →  1... →  n Hopefully all the  ’s begin with different symbols

Solution to Problems 1 and 2 We want (…((( T + T) + T) + T)…) Instead, (T) (+T) (+T) … (+T) Change: E → E + T { (, id } → T { (, id } To: E → T Y { (, id } Y → + T Y { + } → { ;, ) } Follow(Y)  Follow(E) = { ;, ) } No longer contains ‘+’, because we eliminated the production E → E + T

Solution to Problems 1 and 2 (cont’d) In general, Change: A → A  1 A →  1... → A  n →  m to: A →  1 X X →  1 X... →  m X →  n X →

Solution to Problems 1 and 2 (cont’d) In our example, Change: SL → SL S{ begin, id } → S{ begin, id } To: SL → S Z{ begin, id } Z → S Z{ begin, id } → { end }

Modified Grammar S → begin SL end{begin} → id := E ;{id} SL → S Z{begin,id} Z → S Z{begin,id} → {end} E → T Y (,id} Y → + T Y {+} → {;,)} T → P X{(,id} X → * T {*} → {;,+,)} P → (E) {(} → id {id} Disjoint. Grammar is LL(1)

Recursive Descent Parsing Top-down parsing strategy, suitable for LL(1) grammars. One procedure per nonterminal. Contents of stack embedded in recursive call sequence. Each procedure “commits” to one production, based on the next input symbol, and the select sets. Good technique for hand-written parsers.

For our Modified, LL(1) Grammar proc S; {S → begin SL end → id := E; } case Next_Token of T_begin : Read(T_begin); SL; Read (T_end); T_id : Read(T_id); Read (T_:=); E; Read (T_;); otherwiseError; end end; “Read (T_X)” verifies that the upcoming token is X, and consumes it. “Next_Token” is the upcoming token.

For our Modified, LL(1) Grammar (cont’d) proc SL; {SL → SZ} S; Z; end; proc E; {E → TY} T; Y; end; Technically, should have insisted that Next Token be either T_begin or T_id, but S will do that anyway. Checking early would aid error recovery. // Ditto for T_( and T_id.

For our Modified, LL(1) Grammar (cont’d) proc Z;{Z → SZ → } case Next Token of T_begin, T_id: S;Z; T_end: ; otherwise Error; end end;

For our Modified, LL(1) Grammar (cont’d) proc Y; {Y → +TY → } if Next Token = T_+ then Read (T_+) T; Y; end; proc T; {T → PX} P; X end; Could have used a case statement Could have checked for T_( and T_id.

For our Modified, LL(1) Grammar (cont’d) proc X;{X → *T → } if Next Token = T_* then Read (T_*); T; end;

For our Modified, LL(1) Grammar (cont’d) proc P; {P → (E) → id } case Next Token of T_(: Read (T_(); E; Read (T_)); T_id: Read (T_id); otherwise Error; end end;

LL(1) Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 7