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,

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.
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Section 4.8 Aggelos Kiayias Computer Science & Engineering Department
CH4.1 CSE244 Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box.
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
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.
OPERATOR PRECEDENCE PARSING
CS 321 Programming Languages and Compilers Bottom Up Parsing.
1 Compiler Construction Syntax Analysis Top-down parsing.
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.
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.
Compilers ABHISHEK REDDY PAM (11CS30002) DATE : 07/10/2013.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
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.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
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.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
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.
CH4.1 CSE244 Midterm Subjects Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
Operator precedence parser Lecturer: Noor Dhia
Compiler Construction
Introduction to LR Parsing
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
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
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.
BNF 9-Apr-19.
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.
OPERATOR GRAMMAR No Ɛ-transition. No two adjacent non-terminals. Eg.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

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, CT

CH4.2 CSE244 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

CH4.3 CSE244Handles  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: handle of a right sentential form  is that satisfies the above property.  i.e. A  i.e. A   is a handle of  at the location immediately after the end of , if: S =>  A  =>    A certain sentential form may have many different handles.   Right sentential forms of a non-ambiguous grammar have one unique handle * rm

CH4.4 CSE244Example S  aABe  aAde  aAbcde  abbcde Consider: S  aABe A  Abc | b B  d It follows that: S  aABe is a handle of aABe in location 1. B  d is a handle of aAde in location 3. A  Abc is a handle of aAbcde in location 2. A  b is a handle of abbcde in location 2.

CH4.5 CSE244 Handle Pruning  A rightmost derivation in reverse can be obtained by “handle-pruning.”  Apply this to the previous example. S  aABe A  Abc | b B  d abbcde Find the handle = b at loc. 2 aAbcde b at loc. 3 is not a handle: aAAcde... blocked. Also Consider: E  E + E | E * E | | ( E ) | id Derive id+id*id By two different Rightmost derivations

CH4.6 CSE244 Handle Pruning, II  Consider the cut of a parse-tree of a certain right sentential form. S A Left part Handle (only terminals here) Viable prefix

CH4.7 CSE244 Shift Reduce Parsing with a Stack  Two problems:locate a handle and decide which production to use (if there are more than two candidate productions).  General Construction: 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. (other operations: “accept” when the input is consumed and only the start symbol is on the stack, also: “error”).

CH4.8 CSE244Example $ $ id $E id + id * id$ + id * id$ STACKINPUTRemark Shift E  E + E | E * E | ( E ) | id Reduce by E  id

CH4.9 CSE244 More on Shift-Reduce Parsing  Viable prefix: prefix of a right sentential form that appears on the stack of a Shift-Reduce parser.  Conflicts either “shift/reduce” or “reduce/reduce”  Example: stmt  if expr then stmt | if expr then stmt else stmt | other (any other statement) StackInput if … thenelse … Shift/ Reduce conflict

CH4.10 CSE244 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?

CH4.11 CSE244 Operator-Precedence Parsing  Operator Grammars: no production right side is  or has two adjacent non-terminals. Consider: E  EAE | - E | ( E ) | id A  - | + | * | / | ^ Not an operator grammar, but: E  E - E | E + E | E * E | E / E | E ^ E | - E | ( E ) | id

CH4.12 CSE244 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 + <. *

CH4.13 CSE244 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 same is true for any any sentential form.  So given a 1 a 2 a n  So given  0 a 1  1 a 2  2 … a n  n where each  i is either a nonterminal or the empty string.   We drop all non-terminals and we write the corresponding relation between each consecutive pair of terminals.   example for $id+id*id$ using standard precedence: $ + * $   Example for $E+E*id$ … $ $

CH4.14 CSE244 Using Operator-Precedence  … Then 1. Scan the string to discover the first.> 2. Scan backwards skipping.=. (if any) until a $  The handle is E*E

CH4.15 CSE244 Operator Precedence Parser Set ip to point to the first symbol of w$ Stack=$ Repeat forever: if $==topofstack and ip==$ then accept Else { a=topofstack; b=ip; if a<.b or a.=.b then push(b);advance ip; if a.>b then repeat pop() until the top stack terminal is related by <. else error

CH4.16 CSE244Example $ $ id $ $ + $ + id $ + $ + * $ + * id $ + * $ + $ id + id * id$ + id * id$ id * id$ * id$ id$ $ STACKINPUTRemark $. + $ * + $ *.> $ +.> $ accept A sequence of pops corresponds to the application of some of the productions

CH4.17 CSE244 Operator Precedence Table Construction  Basic techniques for operators:  if operator  1 has higher precedence than  2 then set  1.>  2  If the operators are of equal precedence (or the same operator) set  1.>  2 and  2.>  1 if the operators associate to the left set  1 <.  2 and  2 <.  1 if the operators associate to the right  Make   and .>)  id has higher precedence than any other symbol  $ has lowest precedence.

CH4.18 CSE244 Unary Operators  Unary operators that are not also used as binary operators are treated as before.  Problem: the – sign.  Typical solution: have the lexical analyzer return a different token when it sees a unary minus.