COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

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

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.
Compiler Designs and Constructions
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.
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.
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Chapter 4 Parsing Sequences. Recursive Descent Parsing expr() – term() lex() +/- lex() term() factor() – if id lex(), if ( expr() right lex(), term()
Syntax and Semantics Structure of programming languages.
LANGUAGE TRANSLATORS: WEEK 17 scom.hud.ac.uk/scomtlm/cis2380/ See Appel’s book chapter 3 for support reading Last Week: Top-down, Table driven parsers.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntax and Semantics Structure of programming languages.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Three kinds of bottom-up LR parser SLR “Simple LR” –most restrictions on eligible grammars –built quite directly from items as just shown LR “Canonical.
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.
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 
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
COMPILER CONSTRUCTION
Syntax and Semantics Structure of programming languages.
Context-free grammars
Chapter 4 - Parsing CSCE 343.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Lecture #12 Parsing Types.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Table-driven parsing Parsing performed by a finite state machine.
Recursive Descent Parsing
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Winter LR(0) Parsing Summary
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
Canonical LR Parsing Tables
Syntax Analysis Part II
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lexical and Syntax Analysis
Recursive Descent Parsing
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Bottom-up derivation tree, original grammar
Bottom-up derivation tree, original grammar
Parsing #2 Leonidas Fegaras.
Bottom-up derivation tree generation
TaBle-driven LL(1) Parsing
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Compiler SLR Parser.
Parsing #2 Leonidas Fegaras.
Regular Expression to NFA
Regular Expression to NFA
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Compiler Construction
Bottom-up derivation tree generation
Parsing Bottom-Up LR Table Construction.
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.
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

COP4620 – Programming Language Translators Dr. Manuel E. Bermudez LR(0) parsing COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

topics Recursive Descent Parsing with “items” NFA connecting items DFA connecting items: LR(0) parser LR Parser Operations LR Driver Algorithm Direct LR(0) Construction

LR(0) parsing Recursive descent code: “annotate” code “items”, e.g. A  . . Use items to describe operation of RD parser. An FSA describes all possible calling sequences in the RD parser.

Example: proc E(); T(); while Next_Token = T_+ do Read(T_+); od end; LR(0) parsing Each “item” is a dotted production We’ll use items to describe all possible situations in the parser. Transitions between items form an NFA. Example: proc E(); T(); while Next_Token = T_+ do Read(T_+); od end; {E → .E + T} { E →.T} {E → E. + T} {E → T.} {E → E. + T} {E → E + .T } {E → E + T.} {E → E + T.} {E → T.} T T + T

NFA Connecting Items The FSA is: M = (DP, V, , S’ → .S, {S’ → S.}) where DP is the set of all possible items (DP: dotted productions), and  is defined such that simulate a call to B simulate the execution of statement X, if X is a nonterminal, or Read(X), if X is a terminal. B → . ω 1. A → α.Bβ  2. A → α.Xβ A→X.β X

FSA Connecting Items Example: S → E  E → E + T → T T → (E) → i E  ε

FSA Connecting Items Run this NFA using a stack, i.e. keep track of the recursive calling sequence. To “return” from A → ω., back up |ω| + 1 states, then advance on A. Problem with this machine: it is nondeterministic. No problem. Be happy . Transform it to a DFA !

DFA Connecting Items  i + THIS IS AN LR(0) AUTOMATON E → E + T. S → . E S → E. S → E. E → . T E → T . T → . i T → i . T → . (E) T → (E) . T → (E.) T → (.E) E → .E + T E → E. + T E → E +. T E T ( i T → .i E → .T T → .(E) + ) THIS IS AN LR(0) AUTOMATON

LR(0) Parsing LR means “Left-to-Right, Right-most Derivation”. Need a stack of states to operate the parser. No look-ahead required, thus LR(0). DFA describes all possible positions in the RD parser’s code. Once the automaton is built, items can be discarded.

LR(0) Parsing Operation of an LR parser Two moves: shift and reduce. Shift: Advance from current state on Next_Token, push new state on stack. Reduce: (on A → ω). Pop |ω| states from stack. Advance from new top state on A.

LR(0) Parsing Stack Input Derivation Tree 1 i + (i + i)  i + ( i + i ) 14 + (i + i)  13 + (i + i)  12 + (i + i)  127 (i + i)  1275 i + i)  12754 + i)  12753 + i)  12758 + i)  127587 i)  1275874 )  1275879 )  12758 )  12758 10  1279  12  126 ------ LR(0) Parsing E T T T E T 1 3 2 4 5 6 7 8 9 10 E i ( ) +  T E → E+T T → (E) E → T T→i E E

LR(0) PARSE TABLEs Two Tables: Action Table: indexed by state, and by terminal symbol. Contains all shift and reduce moves. GOTO Table: indexed by state, and by nonterminal symbol. Contains all transitions on nonterminals symbols.

LR(0) PARSE TABLES 1 3 2 4 5 6 7 8 9 10 i + ┴ ACTION GOTO i + ( ) ┴ E → E+T T → (E) E → T T→i ACTION GOTO i + ( ) ┴ E T 1 S/4 S/5 2 3 S/7 S/6 R/E→T 4 R/T→ i 5 8 6 Accept 7 9 S/10 R/E→E+T 10 R/T →(E) R/T → (E)

LR(0) Driver algorithm Push(Start_State, S); while ACTION (Top(S), ) ≠ Accept do case ACTION (Top(S), Next_Token) of s/r: Read(Next_Token); Push(r, S) R/A → ω: Pop(S) |ω| times; Push(GOTO (Top(S), A), S); empty: Error; end end;

Direct LR(0) construction PT(G) = Closure({S’ → .S  }) U {Closure(P) | P  Successors(P’), P’  PT(G)} Closure(P) = P U {A → .w | B → α.Aβ  Closure(P)} Successors(P) = {Nucleus(P, X) | X  V} Nucleus(P, X) = {A → αX .β | A → α.Xβ  P}

Direct LR(0) construction 2 S → . E  E → .E + T E → .T T → .i T → . ( E ) T → (.E) E → .E + T E → .T T → .i T → .( E ) 8 T → (E.) E → E. + T 1 10 E 5 E 8 + 7 T 3 T 3 i 4 i 4 9 E → E + T. ( ( 5 5 10 T → (E).  6 S → E .  E → E. + T 2 6 S → E . + Grammar: S → E  E → E + T → T T → (E) → i 7 T E → E + .T T → .i T → .(E) 9 7 i 4 3 E → T. ( 5 4 T → i.

Direct LR(0) construction Notes: Two states are “equal” if their Nuclei are identical. This grammar is LR(0) because there are no “conflicts”. A conflict occurs when a state contains i – Both a final (dot-at-the-end) item and non-final item (shift-reduce), or ii – Two or more final items (reduce-reduce). We’ll deal with conflicts soon.

summary Recursive Descent Parsing with “items” NFA connecting items DFA connecting items: LR(0) parser LR Parser Operations LR Driver Algorithm Direct LR(0) Construction