Compiler Construction

Slides:



Advertisements
Similar presentations
Compiler Designs and Constructions
Advertisements

Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
CS 31003: Compilers  Difference between SLR and LR(1)  Construction of LR(1) parsing table  LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013.
Mooly Sagiv and Roman Manevich School of Computer Science
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,
LR Parsing Table Costruction
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
1 Bottom Up Parsing. 2 Bottom-Up Parsing l Bottom-up parsing is more general than top-down parsing »And just as efficient »Builds on ideas in top-down.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
Bottom Up Parsing.
Chapter 4-2 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR Other.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
ANTLR Andrew Pangborn & Zach Busser. ANTLR in a Nutshell ANother Tool for Language Recognition generates lexers generates parsers (and parse trees)‏ Java-based,
LALR Parsing Canonical sets of LR(1) items
LESSON 24.
LR Parsing Compiler Baojian Hua
CS 153 A little bit about LR Parsing. Background We’ve seen three ways to write parsers:  By hand, typically recursive descent  Using parsing combinators.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
–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.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Conflicts in Simple LR parsers A SLR Parser does not use any lookahead The SLR parsing method fails if knowing the stack’s top state and next input token.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Parser Generation Tools (Yacc and Bison) CS 471 September 24, 2007.
2016/7/9Page 1 Lecture 11: Semester Review COMP3100 Dept. Computer Science and Technology United International College.
Announcements/Reading
Lec04-bottomupparser 4/13/2018 LR Parsing.
50/50 rule You need to get 50% from tests, AND
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Chapter 4 Syntax Analysis.
Compiler Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Simple, efficient;limitated
Syntax Analysis Part II
Syntax Analysis - LR(1) and LALR(1) Parsing
LR Parsing. Parser Generators.
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Parsing #2 Leonidas Fegaras.
Lecture (From slides by G. Necula & R. Bodik)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LALR Parsing Adapted from Notes by Profs Aiken and Necula (UCB) and
Chapter 4. Syntax Analysis (2)
LR Parsing. Parser Generators.
Parsing #2 Leonidas Fegaras.
Prepared by VISHWA DEEPAK 04CS1029
Syntax Analysis - 3 Chapter 4.
Kanat Bolazar February 16, 2010
Compiler Construction
Compiler Construction
Compiler Construction
Chapter 4. Syntax Analysis (2)
Parsing Bottom-Up LR Table Construction.
Parsing Bottom-Up LR Table Construction.
Review for the Midterm. Overview (Chapter 1):
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Compiler Construction Sohail Aslam Lecture 29 compiler: Bottom-up Parsing

Shift/Reduce Conflicts Consider the ambiguous grammar E → E + E | E  E | int We will DFA state containg [E → E  E, +] [E → E  + E, +] This is a note compiler: Bottom-up Parsing

Shift/Reduce Conflicts Consider the ambiguous grammar E → E + E | E  E | int We will DFA state containg [E → E  E, +] [E → E  + E, +] This is a note compiler: Bottom-up Parsing

Again we have a shift/reduce conflict [E → E  E, +] [E → E  + E, +] Again we have a shift/reduce conflict We need to reduce because  has precedence over + This is a note compiler: Bottom-up Parsing

Again we have a shift/reduce conflict [E → E  E, +] [E → E  + E, +] Again we have a shift/reduce conflict We need to reduce because  has precedence over + This is a note compiler: Bottom-up Parsing

Reduce/Reduce Conflicts If a DFA states contains both [X → a, a] and [Y → b, a] Then on input “a” we don’t know which production to reduce This is a note compiler: Bottom-up Parsing

Reduce/Reduce Conflicts This is called a reduce-reduce conflict Usually due to gross ambiguity in the grammar This is a note compiler: Bottom-up Parsing

LR(1) Table Size LR(1) parsing table for even a simple language can be extremely large with thousands of entries. This is a note compiler: Bottom-up Parsing

LR(1) Table Size It is possible to reduce the size of the table Many states in the DFA are similar This is a note compiler: Bottom-up Parsing

LR(1) Table Size It is possible to reduce the size of the table Many states in the DFA are similar This is a note compiler: Bottom-up Parsing

E→ int , $/+ 1 S→ E, $ E→ E+(E), $/+ E→ int, $/+ int E → int on $,+ E E→ E+(E), $/+ 3 + 2 S→ E, $ E→ E +(E), $/+ ( E→ E+(E), $/+ E→ E+(E), )/+ E→ int, )/+ accept on $ 4 E 6 E→ E+(E), $/+ E→ E+(E), )/+ E→ int, )/+ int E→ int , )/+ 5 E → int on ),+

E→ int , $/+ 1 S→ E, $ E→ E+(E), $/+ E→ int, $/+ int E → int on $,+ E E→ E+(E), $/+ 3 + 2 S→ E, $ E→ E +(E), $/+ ( E→ E+(E), $/+ E→ E+(E), )/+ E→ int, )/+ accept on $ 4 E 6 E→ E+(E), $/+ E→ E+(E), )/+ E→ int, )/+ int E→ int , )/+ 5 E → int on ),+

LR(1) Table Size These states have the same core 1 5 E→ int , $/+ E → int on $,+ E → int on ),+ This is a note compiler: Bottom-up Parsing

LR(1) Table Size Upon merge, we obtain 1' E→ int , $/+/) E → int on $,+,) This is a note compiler: Bottom-up Parsing

The Core Definition: The core of set of LR items is the set of first components - without the lookahead terminals This is a note compiler: Bottom-up Parsing

The Core Example: the core of { [X → ab, b], [Y → gd, d] } is { X → ab, Y → gd } This is a note compiler: Bottom-up Parsing

Core of a Set of LR Items Consider the LR(1) states { [X → a, a], [Y → b, c] } { [X → a, b], [Y → b, d] } They have the same core and can be merged This is a note compiler: Bottom-up Parsing

Core of a Set of LR Items Consider the LR(1) states { [X → a, a], [Y → b, c] } { [X → a, b], [Y → b, d] } They have the same core and can be merged This is a note compiler: Bottom-up Parsing

LALR States The merged state contains { [X → a, a/b], [Y → b, c/d] } These are called the LALR(1) states This is a note compiler: Bottom-up Parsing

LALR States The merged state contains { [X → a, a/b], [Y → b, c/d] } These are called the LALR(1) states This is a note compiler: Bottom-up Parsing

LALR States LALR(1) stands for LookAhead LR(1) leads to tables that have 10 times fewer states than LR(1) This is a note compiler: Bottom-up Parsing

A LALR(1) DFA Repeat until all states have distinct code choose two distinct states with same core merge states by creating a new one with the union of all the items point edges from predecessors to new state new state points to all the previous successors This is a note compiler: Bottom-up Parsing

A LALR(1) DFA A B C D E F A C B,E D F This is a note compiler: Bottom-up Parsing

LALR(1) parsing LALR languages are not natural they are an efficiency hack on LR languages Any reasonable programming language has a LALR(1) grammar This is a note compiler: Bottom-up Parsing

LALR(1) parsing LALR languages are not natural they are an efficiency hack on LR languages Any reasonable programming language has a LALR(1) grammar This is a note compiler: Bottom-up Parsing

LALR(1) parsing LALR(1) has become a standard for programming languages and for parser generators Parser generators exist for LL(1) and LALR(1) grammars This is a note compiler: Bottom-up Parsing

Parser Generators LALR(1) - YACC, Bison, CUP LL(1) – ANTLR Recursive Descent - JavaCC This is a note compiler: Bottom-up Parsing