LR Parsing. Parser Generators.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
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.
Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
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.
CS 536 Spring Introduction to Bottom-Up Parsing Lecture 11.
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
Bottom Up Parsing.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Prof. Bodik CS 164 Lecture 91 Bottom-up parsing CS164 3:30-5:00 TT 10 Evans.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Syntax and Semantics Structure of programming languages.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntax and Semantics Structure of programming languages.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
Syntax and Semantics Structure of programming languages.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
CS 404 Introduction to Compiler Design
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
4 (c) parsing.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Regular Grammar - Finite Automaton
Lexical and Syntax Analysis
Syntax-Directed Translation
LR Parsing. Parser Generators.
4d Bottom Up Parsing.
Parsing #2 Leonidas Fegaras.
Lecture (From slides by G. Necula & R. Bodik)
BOTTOM UP PARSING Lecture 16.
Lecture 8 Bottom Up Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Compiler Construction
Parsing IV Bottom-up Parsing
Parsing #2 Leonidas Fegaras.
4d Bottom Up Parsing.
Parsing Bottom-Up.
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Compiler Construction
Compiler Construction
Compiler Construction
4d Bottom Up Parsing.
Parsing Bottom-Up Introduction.
4d Bottom Up Parsing.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

LR Parsing. Parser Generators. Lecture 7-8 Prof. Bodik CS 164 Lecture 7-8

Bottom-up parsing is more general than top-down parsing And just as efficient Builds on ideas in top-down parsing Preferred method in practice Also called LR parsing L means that tokens are read left to right R means that it constructs a rightmost derivation ! Prof. Bodik CS 164 Lecture 7-8

An Introductory Example LR parsers don’t need left-factored grammars and can also handle left-recursive grammars Consider the following grammar: E  E + ( E ) | int Why is this not LL(1)? Consider the string: int + ( int ) + ( int ) Prof. Bodik CS 164 Lecture 7-8

str  input string of terminals repeat The Idea LR parsing reduces a string to the start symbol by inverting productions: str  input string of terminals repeat Identify b in str such that A  b is a production (i.e., str = a b g) Replace b by A in str (i.e., str becomes a A g) until str = S Prof. Bodik CS 164 Lecture 7-8

A Bottom-up Parse in Detail (1) int + (int) + (int) int + ( int ) + ( int ) Prof. Bodik CS 164 Lecture 7-8

A Bottom-up Parse in Detail (2) int + (int) + (int) E + (int) + (int) E int + ( int ) + ( int ) Prof. Bodik CS 164 Lecture 7-8

A Bottom-up Parse in Detail (3) int + (int) + (int) E + (int) + (int) E + (E) + (int) E E int + ( int ) + ( int ) Prof. Bodik CS 164 Lecture 7-8

A Bottom-up Parse in Detail (4) int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int) E E E int + ( int ) + ( int ) Prof. Bodik CS 164 Lecture 7-8

A Bottom-up Parse in Detail (5) int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int) E + (E) E E E E int + ( int ) + ( int ) Prof. Bodik CS 164 Lecture 7-8

A Bottom-up Parse in Detail (6) int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int) E + (E) E E A rightmost derivation in reverse E E E int + ( int ) + ( int ) Prof. Bodik CS 164 Lecture 7-8

An LR parser traces a rightmost derivation in reverse Important Fact #1 Important Fact #1 about bottom-up parsing: An LR parser traces a rightmost derivation in reverse Prof. Bodik CS 164 Lecture 7-8

Where Do Reductions Happen Important Fact #1 has an interesting consequence: Let g be a step of a bottom-up parse Assume the next reduction is by A  Then g is a string of terminals ! Why? Because Ag  g is a step in a right-most derivation Prof. Bodik CS 164 Lecture 7-8

Idea: Split string into two substrings Notation Idea: Split string into two substrings Right substring (a string of terminals) is as yet unexamined by parser Left substring has terminals and non-terminals The dividing point is marked by a I The I is not part of the string Initially, all input is unexamined: Ix1x2 . . . xn Prof. Bodik CS 164 Lecture 7-8

Bottom-up parsing uses only two kinds of actions: Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce Prof. Bodik CS 164 Lecture 7-8

Shift: Move I one place to the right Shifts a terminal to the left string E + (I int )  E + (int I ) Prof. Bodik CS 164 Lecture 7-8

Reduce Reduce: Apply an inverse production at the right end of the left string If E  E + ( E ) is a production, then E + (E + ( E ) I )  E +(E I ) Prof. Bodik CS 164 Lecture 7-8

Shift-Reduce Example int + ( int ) + ( int ) I int + (int) + (int)$ shift int + ( int ) + ( int )

Shift-Reduce Example int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int int + ( int ) + ( int )

Shift-Reduce Example E int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int E I + (int) + (int)$ shift 3 times E int + ( int ) + ( int )

Shift-Reduce Example E int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int E I + (int) + (int)$ shift 3 times E + (int I ) + (int)$ red. E  int E int + ( int ) + ( int )

Shift-Reduce Example E E int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int E I + (int) + (int)$ shift 3 times E + (int I ) + (int)$ red. E  int E + (E I ) + (int)$ shift E E int + ( int ) + ( int )

Shift-Reduce Example E E int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int E I + (int) + (int)$ shift 3 times E + (int I ) + (int)$ red. E  int E + (E I ) + (int)$ shift E + (E) I + (int)$ red. E  E + (E) E E int + ( int ) + ( int )

Shift-Reduce Example E E E int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int E I + (int) + (int)$ shift 3 times E + (int I ) + (int)$ red. E  int E + (E I ) + (int)$ shift E + (E) I + (int)$ red. E  E + (E) E I + (int)$ shift 3 times E E E int + ( int ) + ( int )

Shift-Reduce Example E E E int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int E I + (int) + (int)$ shift 3 times E + (int I ) + (int)$ red. E  int E + (E I ) + (int)$ shift E + (E) I + (int)$ red. E  E + (E) E I + (int)$ shift 3 times E + (int I )$ red. E  int E E E int + ( int ) + ( int )

Shift-Reduce Example E E E E int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int E I + (int) + (int)$ shift 3 times E + (int I ) + (int)$ red. E  int E + (E I ) + (int)$ shift E + (E) I + (int)$ red. E  E + (E) E I + (int)$ shift 3 times E + (int I )$ red. E  int E + (E I )$ shift E E E E int + ( int ) + ( int )

Shift-Reduce Example E E E E int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int E I + (int) + (int)$ shift 3 times E + (int I ) + (int)$ red. E  int E + (E I ) + (int)$ shift E + (E) I + (int)$ red. E  E + (E) E I + (int)$ shift 3 times E + (int I )$ red. E  int E + (E I )$ shift E + (E) I $ red. E  E + (E) E E E E int + ( int ) + ( int )

Shift-Reduce Example E E E E E int + ( int ) + ( int ) I int + (int) + (int)$ shift int I + (int) + (int)$ red. E  int E I + (int) + (int)$ shift 3 times E + (int I ) + (int)$ red. E  int E + (E I ) + (int)$ shift E + (E) I + (int)$ red. E  E + (E) E I + (int)$ shift 3 times E + (int I )$ red. E  int E + (E I )$ shift E + (E) I $ red. E  E + (E) E I $ accept E E E E E int + ( int ) + ( int )

Left string can be implemented by a stack The Stack Left string can be implemented by a stack Top of the stack is the I Shift pushes a terminal on the stack Reduce pops 0 or more symbols off of the stack (production rhs) and pushes a non-terminal on the stack (production lhs) Prof. Bodik CS 164 Lecture 7-8

Key Issue: When to Shift or Reduce? Decide based on the left string (the stack) Idea: use a finite automaton (DFA) to decide when to shift or reduce The DFA input is the stack The language consists of terminals and non-terminals We run the DFA on the stack and we examine the resulting state X and the token tok after I If X has a transition labeled tok then shift If X is labeled with “A  b on tok” then reduce Prof. Bodik CS 164 Lecture 7-8

LR(1) Parsing. An Example int I int + (int) + (int)$ shift int I + (int) + (int)$ E  int E I + (int) + (int)$ shift(x3) E + (int I ) + (int)$ E  int E + (E I ) + (int)$ shift E + (E) I + (int)$ E  E+(E) E I + (int)$ shift (x3) E + (int I )$ E  int E + (E I )$ shift E + (E) I $ E  E+(E) E I $ accept 1 E E  int on $, + ( + 2 3 4 accept on $ E int ) E  int on ), + 7 6 5 E  E + (E) on $, + + int ( 8 9 E + 10 11 E  E + (E) on ), + )

Parsers represent the DFA as a 2D table Lines correspond to DFA states Representing the DFA Parsers represent the DFA as a 2D table Recall table-driven lexical analysis Lines correspond to DFA states Columns correspond to terminals and non-terminals Typically columns are split into: Those for terminals: action table Those for non-terminals: goto table Prof. Bodik CS 164 Lecture 7-8

Representing the DFA. Example The table for a fragment of our DFA: ( int + ( ) $ E … 3 s4 4 s5 g6 5 rEint 6 s8 s7 7 rEE+(E) 3 4 int E 5 6 E  int on ), + ) 7 E  E + (E) on $, + Prof. Bodik CS 164 Lecture 7-8

A Hierarchy of Grammar Classes From Andrew Appel, “Modern Compiler Implementation in Java” Prof. Bodik CS 164 Lecture 7-8