COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

Slides:



Advertisements
Similar presentations
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

Compiler Designs and Constructions
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
 CS /11/12 Matthew Rodgers.  What are LL and LR parsers?  What grammars do they parse?  What is the difference between LL and LR?  Why do.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
CS 31003: Compilers  Difference between SLR and LR(1)  Construction of LR(1) parsing table  LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013.
1 May 22, May 22, 2015May 22, 2015May 22, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
1 Example S'  S, S  CC, C  cC, C  d State 0. Closure({[S'  S, $]}) = { S'  S, $ S   CC, FIRST( $)  S   CC, $ C   cC, FIRST(C$)  C.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Bottom Up Parsing.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Bottom-up parsing Goal of parser : build a derivation
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
Chap. 6, Bottom-Up Parsing J. H. Wang May 17, 2011.
A New Look at LR(k) Bill McKeeman, MathWorks Fellow for Worcester Polytechnic Computer Science Colloquium February 20, 2004.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
–Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L –The grammar can have shift/reduce conflict or reduce/reduce conflict.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bernd Fischer RW713: Compiler and Software Language Engineering.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
Announcements/Reading
Context-free grammars
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Compiler design Bottom-up parsing Concepts
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Context-free grammars, derivation trees, and ambiguity
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
LL(1) grammars Module 07.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
CS 404 Introduction to Compiler Design
Compiler Construction
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Implementing a LR parser engine
Regular grammars Module 04.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Top-down parsing Module 06.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
A New Look at LR(k) Bill McKeeman, MathWorks Fellow for
LR(1) grammars The Chinese University of Hong Kong Fall 2010
TaBle-driven LL(1) Parsing
Lecture (From slides by G. Necula & R. Bodik)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
TaBle-driven LL(1) Parsing
LALR Parsing Adapted from Notes by Profs Aiken and Necula (UCB) and
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
First, Follow and Select sets
5. Bottom-Up Parsing Chih-Hung Wang
Syntax Analysis - 3 Chapter 4.
Kanat Bolazar February 16, 2010
LR(1) grammars The Chinese University of Hong Kong Fall 2011
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Operator precedence and AST’s
Compiler Construction
Parsing Bottom-Up LR Table Construction.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Parsing Bottom-Up LR Table Construction.
Chap. 3 BOTTOM-UP PARSING
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 7, 10/09/2003 Prof. Roy Levow.
Presentation transcript:

COP4620 – Programming Language Translators Dr. Manuel E. Bermudez LALR(1) parsing COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

topics Non SLR(1) Grammars Calculating LALR(1) lookahead sets LALR(1) examples. Summary of Parsing (top-down vs. bottom-up)

LALR(1) parsing LR(0) Automaton: SLR(1) Analysis: (State 5) 2 S 6 3 7 A 10 b a A → a 4 8 B A → AbAa 9 11 5 S → Ba B → a Grammar is not LR(0): reduce-reduce conflict.  Grammar: S’ → S  S → AbAa → Ba A → a B → a LR(0) Automaton: SLR(1) Analysis: (State 5) Follow(A) = {a, b} Follow(B) = {a} Conflict not resolved. Grammar not SLR(1). Need Follow(A) ’in the context of’ State 1, not state 7. State a b  5 R/A→a R/B→a State a b  5 R/A→a R/B→a LR(0) table. SLR(1) table.

LR Parsing history LR parsing: D. Knuth seminal paper, 1965. SLR(1): F. DeRemer, Ph.D. thesis, MIT, 1969. LALR(1): various algorithms (from LR(1) DFA), 1970’s. LALR(1): Efficient Computation of LALR(1) Lookahead sets, (from LR(0) DFA) F. DeRemer, T. Pennello, UCSC, 1981. 5. LALR(k): M. Bermudez, Ph.D. thesis, UCSC, 1984. 6. LALR(1): Simple Computation of LALR(1) Lookahead sets, M. Bermudez and G. Logothetis, UF, 1989.

Simple LALR(1) parsing I. For each conflicting reduction A → ω at each conflicting state q, find all nonterminal transitions (pi, A) such that II. Need the union of Follow(pi, A) for all i. p1 A q pn A → ω ω .

Simple LALR(1) parsing Build G’, an ’expanded’ version of G: For each (p, A) and for each A → w1w2…wn, we have For each such situation, G’ has a production of the form: (p, A) → (p, w1)(p2, w2)…(pn, wn) G’ structurally similar to G. Uses vocabulary of LR(0) transitions, rather than symbols in G. Follow sets in G’ are the key ! p A p2 A → w1…wn … w2 wn w1

Simple LALR(1) parsing Grammar: Not LR(0). Not SLR(1). S’ → S  S → AbAa → Ba A → a B → a Not LR(0). Not SLR(1). 1 2 S 6 3 7 A 10 b a A → a 4 8 B A → AbAa 9 11 5 S → Ba B → a  For the conflict in state 5, we need Follow(1, A) = {(3, b)} Follow(1, B) = {(4, a)}. Extract the symbols. {b} {a} Disjoint. Grammar is LALR(1)! G’: (1, S) → (1, A)(3, b)(7, A)(9, a) → (1, B)(4, a) (1, A) → (1, a) (7, A) → (7, a) (1, B) → (1, a) These have split ! State a b  5 R/B→a R/A→a LALR(1) table.

Simple LALR(1) parsing 1 2 5 8 11 7 4 6 3 12 10 9 13 Grammar: S’ → S  B → A S → bBb A → c → aBa → acb Simple LALR(1) parsing 1 2 S 5 8 11 b A → c 7 4 6 3 a S → bBb S → aBa 12 10 9 A c B B → A 13 S → acb  LALR(1) Analysis: Need Follow(4, A). G’: (1,S) → (1, b)(3, B)(6, b) → (1, a)(4, B)(9, a) → (1, a)(4, c)(10, b) (3, B) → (3, A) (3, A) → (3, c) (4, B) → (4, A) (4, A) → (4, c) Follow(4, A)⊇Follow(4, B) = {(9, a)}. The lookahead set is {a}. {a} State 10: shift-reduce conflict. Grammar is not LR(0). SLR(1) Analysis, state 10: Follow(A) ⊇ Follow(B) ={a,b}. Grammar is not SLR(1). Since b ∉ {a}, the grammar is LALR(1).

Summary of parsing S part of tree known stack w β part of tree known stack part of tree left to predict remaining input input already parsed α Top-Down Parsing Hand-written or Table Driven:LL(1) S w β unknown (left to predict) stack known remaining input input already parsed α Bottom-up Parsing Usually Table-Driven: LR(0), SLR(1), LALR(1).

summary Non SLR(1) Grammars Calculating LALR(1) lookahead sets LALR(1) examples. Summary of Parsing (top-down vs. bottom-up)