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.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

Compiler Construction
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.
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.
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.
Pertemuan 12, 13, 14 Bottom-Up Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
CS 536 Spring Introduction to Bottom-Up Parsing Lecture 11.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
1 LR parsing techniques SLR (not in the book) –Simple LR parsing –Easy to implement, not strong enough –Uses LR(0) items Canonical LR –Larger parser but.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
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.
BOTTOM-UP PARSING Sathu Hareesh Babu 11CS10039 G-8.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
1 Compiler Construction Syntax Analysis Top-down parsing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
1 Compiler Construction Syntax Analysis Top-down parsing.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
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.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
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.
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.
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.
Operator precedence parser Lecturer: Noor Dhia
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Bottom-Up Parsing.
Unit-3 Bottom-Up-Parsing.
Revision ? E  TE  E   + TE  |  T  FT  T   * FT  | 
Parsing IV Bottom-up Parsing
LALR Parsing Canonical sets of LR(1) items
Syntax Analysis Part II
Shift Reduce Parsing Unit -3
4d Bottom Up Parsing.
BOTTOM UP PARSING Lecture 16.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Bottom Up Parsing.
Parsing IV Bottom-up Parsing
Bottom-Up Parsing “Shift-Reduce” Parsing
4d Bottom Up Parsing.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Compiler Construction
Bottom-up parsing is also known as shift-reduce parsing
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

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 left-to-right fashion) to the right side of some production and then it is substituted by the non- terminal in the left hand side of the production. Consider: S  aABe A  Abc | b B  d abbcde aAbcde aAde aABe S Rightmost Derivation: S  aABe  aAde  aAbcde  abbcde rm rm

2Handles  Handle of a string = substring that matches the RHS of some production AND whose reduction to the non-terminal on the LHS is a step along the reverse of some rightmost derivation.  Formally:  A phrase is a substring of a sentential form derived from exactly one Non-terminal  A simple phrase is a phrase created in one step  handle is a simple phrase of a right sentential form  i.e. A  i.e. A   is a handle of  x, where x is a string of terminals, if: S =>  Ax =>  x   A certain sentential form may have many different handles.   Right sentential forms of a non-ambiguous grammar have one unique handle [but many substrings that look like handles potentially !]. * rm

3Example S  aABe  aAde  aAbcde  abbcde Consider: S  aABe A  Abc | b B  d It follows that: (S  ) aABe is a handle of aABe (B  ) d is a handle of aAde (A  ) Abc is a handle of aAbcde (A  ) b is a handle of abbcde rm

4 Example, II Grammar: S  aABe A  Abc | b B  d Consider aAbcde (it is a right sentential form) Is [A  b, aAbcde] a handle? S  rm …  rm aAAbcde  rm aAbcde if it is then there must be: no way ever to get two consecutive A’s in this grammar. => Impossible

5 Example, III Grammar: S  aABe A  Abc | b B  d Consider aAbcde (it is a right sentential form) Is [B  d, aAbcde] a handle? S  rm …  rm aAbcBe  rm aAbcde if it is then there must be: we try to obtain aAbcBe S  rm aABe  ?? aAbcBe not a right sentential form

6 Shift Reduce Parsing with a Stack  The “big” problem : given the sentential form locate the handle  General Idea for S-R parsing using a stack: 1. “shift” input symbols into the stack until a handle is found on top of it. 2. “reduce” the handle to the corresponding non- terminal. 3. “accept” when the input is consumed and only the start symbol is on the stack. 4. “error” call the error handler  Viable prefix: prefix of a right sentential form that appears on the stack of a Shift-Reduce parser.

7 What happens with ambiguous grammars Consider: E  E + E | E * E | | ( E ) | id Derive id+id*id By two different Rightmost derivations

8Example $ $ id $ E $ E + $ E + id $ E + E id + id * id$ + id * id$ id * id$ * id$ STACKINPUTRemark Shift E  E + E | E * E | ( E ) | id Reduce by E  id Shift Reduce by E  id Both reduce by E  E + E, and Shift can be performed: Shift/reduce conflict

9Conflicts  Conflicts [appear in ambiguous grammars] either “shift/reduce” or “reduce/reduce”  Another Example: stmt  if expr then stmt | if expr then stmt else stmt | other (any other statement) StackInput if … thenelse … Shift/ Reduce conflict

10 More Conflicts stmt  id ( parameter-list ) stmt  expr := expr parameter-list  parameter-list, parameter | parameter parameter  id expr-list  expr-list, expr | expr expr  id | id ( expr-list ) Consider the string A(I,J) Corresponding token stream is id(id, id) After three shifts: Stack = id(idInput =, id) Reduce/Reduce Conflict … what to do? (it really depends on what is A, an array? or a procedure?

11 Removing Conflicts  One way is to manipulate grammar.  cf. what we did in the top-down approach to transform a grammar so that it is LL(1).  Nevertheless:  We will see that shift/reduce and reduce/reduce conflicts can be best dealt with after they are discovered.  This simplifies the design.

12 Operator-Precedence Parsing  problems encountered so far in shift/reduce parsing:  IDENTIFY a handle.  resolve conflicts (if they occur).  operator grammars: a class of grammars where handle identification and conflict resolution is easy.  Operator Grammars: no production right side is  or has two adjacent non-terminals.  note: this is typically ambiguous grammar. E  E - E | E + E | E * E | E / E | E ^ E | - E | ( E ) | id

13 Basic Technique  For the terminals of the grammar, define the relations and.=.  a <. b means that a yields precedence to b  a.=. b means that a has the same precedence as b.  a.> b means hat a takes precedence over b  E.g. *.> + or + + or + <. *  Many handles are possible. We will use to find the correct handle (i.e., the one that respects the precedence).

14 Using Operator-Precedence Relations  GOAL: delimit the handle of a right sentential form  will mark the end and.=. will be in between.  Since no two adjacent non-terminals appear in the RHS of any production, the general form sentential forms is as: a 1 a 2 a n, w  Since no two adjacent non-terminals appear in the RHS of any production, the general form sentential forms is as:  0 a 1  1 a 2  2 … a n  n, where each  i is either a nonterminal or the empty string.   At each step of the parse, the parser considers the top most terminal of the parse stack (i.e., either top or top-1), say a, and the current token, say b, and looks up their precedence relation, and decides what to do next:

15 Operator-Precedence Parsing 1. If a.=. b, then shift b into the parse stack 2. If a <. b, then shift <. And then shift b into the parse stack 3. If a.> b, then find the top most b, then find the top most <. relation of the parse stack; the string between this relation (with the non-terminal underneath, if there exists) and the top of the parse stack is the handle (the handle should match (weakly) with the RHS of at least one grammar rule); replace the handle with a typical non-terminal

16Example $ $ <. id $ E $ E <. + $ E <. + <. id $ E <. + E $ E <. + E <. * $ E <. + E <. * <. id $ E <. + E <. * E $ E <. + E $ E id + id * id$ + id * id$ id * id$ * id$ id$ $ STACKINPUTRemark $. + $ * + $ *.> $ +.> $ accept Parse Table + * ( ) id $.>.>.> <. <. <..=. <..>.> <. <. <. <..=. + * ( ) id $.>.> 1-2 E  E + T | T 3-4 T  T * F | F 5-6 T  ( E ) | id

17 Producing the parse table  FirstTerm(A) = {a | A  + a  or A  + Ba  }  LastTerm(A) = {a | A  +  a or A  +  aB}  a.=. b iff  U   ab  or  U   aBb   a <. b iff  U   aB  and b  FirsTerm(B)  a.> b iff  U   Bb  and a  LastTerm(B)

18Example:  FirstTerm (E) = {+, *, id, (}  FirstTerm (T) = {*, id, (}  FirstTerm (F) = {id, (}  LastTerm (E) = {+, *, id, )}  LastTerm (T) = {*, id, )}  LastTerm (F) = {id, )} 1-2 E  E + T | T 3-4 T  T * F | F 5-6 T  ( E ) | id

19 Precedence Functions vs Relations  f(a) < g(b) whenever a <. b  f(a) = g(b) whenever a.=. b  f(a) > g(b) whenever a.> b + - * /  ( ) id $ f g

20 Constructing precedence functions g idf id f * g * g + f $ f + g $ + * id $ f g

21 Handling Errors During Reductions  Suppose abEc is poped and there is no production right hand side that matches abEc  If there were a rhs aEc, we might issue message illegal b on line x  If the rhs is abEdc, we might issue message missing d on line x  If the found rhs is abc, the error message could be illegal E on line x, where E stands for an appropriate syntactic category represented by non-terminal E

22 Handling shift/reduce errors e1: /* called when whole expression is missing */ insert id onto the input print “missing operand e2: /* called when expression begins with a right parenthesis */ delete ) from the input print “unbalanced right parenthesis” e3”: /* called when id or ) is followed by id or ( */ insert + onto the input print “missing operator e4: /* called when expression ends with a left parenthesis */ pop ( from the stack print “missing right parenthesis” id ( ) $ e3 e3.>.> <.. <..=. e4 e3 e3.>.> <. <. e2 e1 id ( ) $

23 Extracting Precedence relations from parse tables E 1-2 E  E + T | T 3-4 T  T * F | F 5-6 T  ( E ) | id +E T T *F  + <. * id  * <. id

24 Extracting Precedence relations from parse tables E 1-2 E  E + T | T 3-4 T  T * F | F 5-6 T  ( E ) | id T T *F  id.> * T  *.> * *F F id

25 Pros and Cons  + simple implementation  + small parse table  - weak (too restrictive for not allowing two adjacent non-terminals  - not very accurate (some syntax errors are not detected due weak treatment of non-terminals)  Simple precedence parsing is an improved form of operator precedence that doesn’t have these weaknesses