Fixing non-ll(1) grammars

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

Top-Down Parsing.
1 214 review. 2 What we have learnt Generate scanner and parser –We do not program directly –Instead we write the specifications for the scanner and parser.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 6, 10/02/2003 Prof. Roy Levow.
Top-Down Parsing.
LL(1) Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 7.
Syntax Analysis By Noor Dhia Left Recursion: Example1: S → S0s1s | 01 The grammar after eliminate left recursion is: S → 01 S’ S' → 0s1sS’
9/30/2014IT 3271 How to construct an LL(1) parsing table ? 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table What is the.
Storage Allocation Mechanisms
Context-free grammars
Language Theory Module 03.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Context-free grammars, derivation trees, and ambiguity
LL(1) grammars Module 07.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Paradigm blindness programming paradigms
Writing a scanner Module 05.5 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Grammars Module 03.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Recursive Descent Parsing
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
The chomsky hierarchy Module 03.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Compiler Construction
FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Top-Down Parsing.
Top-down derivation tree generation
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Finite-state automata
Regular grammars Module 04.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
D I s , a ·.... l8l8.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Top-Down Parsing.
CS 540 George Mason University
Recursive Descent Parsing
Bottom-up AST, original grammar
CSE322 LEFT & RIGHT LINEAR REGULAR GRAMMAR
Top-down derivation tree generation
Regular expressions Module 04.3 COP4020 – Programing Language Concepts Dr. Manuel E. Bermudez.
Top-down parsing Module 06.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Bottom-up derivation tree, original grammar
Bottom-up derivation tree, original grammar
TaBle-driven LL(1) Parsing
Replacing recursion with iteration
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom-up AST, original grammar
Bottom-up derivation tree generation
TaBle-driven LL(1) Parsing
CSE 311: Foundations of Computing
DFA-> Minimum DFA Module 05.4 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
COP4020 Programming Language Concepts Dr. Manuel E. Bermudez
NFA->DFA Module 05.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Fixing non-ll(1) grammars
I ll I
First, Follow and Select sets
Regular Expression to NFA
Computing Follow(A) : All Non-Terminals
Regular Expression to NFA
Replacing recursion with iteration
Programming Language Principles
' 1 A ./.\.l+./.\.l
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Recursion and Rpal’s synTax
Operator precedence and AST’s
Bottom-up derivation tree generation
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Paradigms and paradigm shifts
Operator Precedence and Associativity
Programming Language Principles
Programming Language Concepts
Presentation transcript:

Fixing non-ll(1) grammars Module 07.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

Topics Problems with model grammar Fixing common prefices Fixing left-recursion New grammar: LL(1)

Model grammar Problems: → id := E; {id} SL is left recursive. 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} Problems: SL is left recursive. E is left recursive. T → P * T have common → P prefices. Showing a grammar is not LL(1): easy. PL grammars: “mostly” LL(1). This is our “model” PL grammar. We’ll use it throughout.

Fixing common prefices 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)

Fixing common prefices In general, change A → 1 → 2 . . . → n to A →  X X → 1 → n Hopefully all the ’s begin with different symbols. If not, repeat !

Fixing left recursion of E We have (…((( T + T) + T) + T)…) Instead, we want (T) (+T) (+T) … (+T) Change: E → E + T { (, id } → T { (, id } To: E → T Y { (, id } Y → + T Y { + } → { ; , ) } Follow(Y)  Follow(E) = { ; , ) } Yikes: destroyed the left associatvity of ‘+’ ! Will fix this later. No longer contains ‘+’: we eliminated E → E + T

Fixing left recursion In general, Change: A → A1 A →  1 . . . . . . . . . . . . → An →  m to: A → 1 X X → 1 X . . . . . . → m X → n X → The ’s don’t begin with A.

Fixing left recursion of SL We have (…(((S)S)S)…) Instead, we want (S)(S) …(S) Change: SL → SL S { begin, id } → S { begin, id } To: SL → S Z { begin, id } Z → S Z { begin, id } → { end } Destroyed the left associatvity of ‘ ’. Fixable, but won’t matter.

Modified grammar → id := E ; {id} SL → S Z {begin,id} 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} Grammar is LL(1) !

summary Problems with model grammar Fixing common prefices Fixing left-recursion New grammar is LL(1)