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.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

Compiler Construction
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 Designs and Constructions
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
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.
Mooly Sagiv and Roman Manevich School of Computer Science
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
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)
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
CS 536 Spring Introduction to Bottom-Up Parsing Lecture 11.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
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
Lexical and syntax analysis
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.
Chap. 6, Bottom-Up Parsing J. H. Wang May 17, 2011.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
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.
1 Compiler Construction Syntax Analysis Top-down parsing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Syntax and Semantics Structure of programming languages.
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
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.
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
Bottom-Up Parsing.
Parsing and Parser Parsing methods: top-down & bottom-up
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.
Chapter 4 Syntax Analysis.
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
Subject Name:COMPILER DESIGN Subject Code:10CS63
Regular Grammar - Finite Automaton
Lexical and Syntax Analysis
4d Bottom Up Parsing.
Parsing #2 Leonidas Fegaras.
BOTTOM UP PARSING Lecture 16.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Ambiguity in Grammar, Error Recovery
Parsing IV Bottom-up Parsing
LR Parsing. Parser Generators.
Parsing #2 Leonidas Fegaras.
Bottom-Up Parsing “Shift-Reduce” Parsing
Parsing Bottom-Up.
Context Free Grammar – Quick Review
Presentation transcript:

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 Nonterminal or terminals). Trys to identify some substring of the sentential form that is the rhs of some production. E -> E + E | E * E | x x + x * x E + x * x E + E * x E * x E * E E The substring (shown in color and italics) for each step) may contain both terminal and non-terminal symbols. This string is the rhs of some production, and is often called a handle.

Bottom Up Parsing Implemented by Shift-Reduce parsing data structures: input-string and stack. look at symbols on top of stack, and the input-string and decide: – shift (move first input to stack) – reduce (replace top n symbols on stack by a non-terminal) – accept (declare victory) – error (be gracious in defeat)

Example Bottom up Parse Consider the grammar: (note: left recursion is NOT a problem, but the grammar is still layered to prevent ambiguity) 1. E ::= E + T 2. E ::= T 3. T ::= T * F 4. T ::= F 5. F ::= ( E ) 6. F ::= id stack Input Action x + y shift x + y reduce 6 F + y reduce 4 T + y reduce 2 E + y shift E + y reduce 6 E + F reduce 4 E + T reduce 1 E accept The concatenation of the stack and the input is a sentential form. The input is all terminal symbols, the stack is a combination of terminal and non- terminal symbols

LR(k) Grammars which can decide whether to shift or reduce by looking at only k symbols of the input are called LR(k). – Note the symbols on the stack don’t count when calculating k L is for a Left-to-Right scan of the input R is for the Reverse of a Rightmost derivation

Problems (ambiguous grammars) 1) shift reduce conflicts: stack Input Action x + y + z ? stack Input Action if x t if y t s2 e s3 ? 2) reduce reduce conflicts: suppose both procedure call and array reference have similar syntax: – x(2) := 6 – f(x) stack Input Action id id ( id ) id ? id Should id reduce to a parameter or an expression. Depends on whether the bottom most id is an array or a procedure.

Using ambiguity to your advantage Shift-Reduce and Reduce-Reduce errors are caused by ambiguous grammars. We can use resolution mechanisms to our advantage. Use an ambiguous grammar (smaller more concise, more natural parse trees) but resolve ambiguity using rules. Operator Precedence – Every operator is given a precedence – Precedence of the operator closest to the top of the stack and the precedence of operator next on the input decide shift or reduce. – Sometimes the precedence is the same. Need more information: Associativity information.

Example Precedence Parser + * ( ) id $+ * ( ) $ < : : > = input : x * x + y stack Input Action $ E * E + y $ reduce! topmost terminal next input accept

Precedence parsers Precedence parsers have limitations No production can have two consecutive non-terminals Parse only a small subset of the Context Free Grammars Need a more robust version of shift- reduce parsing. LR - parsers – State based - finite state automatons (w / stack) – Accept the widest range of grammars – Easily constructed (by a machine) – Can be modified to accept ambiguous grammars by using precedence and associativity information.

LR Parsers Table Driven Parsers Table is indexed by state and symbols (both term and non-term) Table has two components. – ACTION part – GOTO part state terminalsnon-terminals id + * ( ) $E T F shift (state = 5) reduce(prod = 12) goto(state = 2) ACTION GOTO

LR Table encodes FSA ( T E ) F * id ( * + ( F F F E ( T + T E -> E + T | T T -> T * F | F F -> ( E ) | id transition on terminal is a shift in action table, on nonterminal is a goto entry

Table vs FSA The Table encodes the FSA The action part encodes – Transitions on terminal symbols (shift) – Finding the end of a production (reduce) The goto part encodes – Tracing backwards the symbols on the RHS – Transition on non-terminal, the LHS Tables can be quite compact

LR Table state terminalsnon-terminals id + * ()$ E T F s5s s6acc r2 s7 r2 r4 s5 s48 23 r6 s5 s4 9 3 s5s4 10 s6s11 r1 s7 r1 r3 r5

Reduce Action If the top of the stack is the rhs for some production n And the current action is “reduce n” We pop the rhs, then look at the state on the top of the stack, and index the goto-table with this state and the LHS non-terminal. Then push the lhs onto the stack in the new s found in the goto-table. (?,0)(id,5) * id + id $ Where: Action(5,*) = reduce 6 Production 6 is: F ::= id And: GOTO(0,F) = 3 (?,0)(F,3) * id + id $

Example Parse Stack Input (?,0) id * id + id $ (?,0)(id,5) * id + id $ (?,0)(F,3) * id + id $ (?,0)(T,2) * id + id $ (?,0)(T,2)(*,7) id + id $ (?,0)(T,2)(*,7)(id,5) + id $ (?,0)(T,2)(*,7)(F,10) + id $ (?,0)(T,2) + id $ (?,0)(E,1) + id $ (?,0)(E,1)(+,6) id $ (?,0)(E,1)(+,6)(id,5) $ (?,0)(E,1)(+,6)(F,3) $ (?,0)(E,1)(+,6)(T,9) $ (?,0)(E,1) $ 1)E -> E + T 2)E -> T 3)T -> T * F 4)T -> F 5)F -> ( E ) 6)F -> id

Review Bottom up parsing transforms the input into the start symbol. Bottom up parsing looks for the rhs of some production in the partially transformed intermediate result Bottom up parsing is OK with left recursive grammars Ambiguity can be used to your advantage in bottom up partsing. The LR(k) languages = LR(1) languages = CFL