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.

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.
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.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
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.
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.
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,
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
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.
BOTTOM-UP PARSING Sathu Hareesh Babu 11CS10039 G-8.
OPERATOR PRECEDENCE PARSING
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
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.
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.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
COMPILERS 4 TH SEPTEMBER 2013 WEDNESDAY 11CS10045 SRAJAN GARG.
Lecture 5: LR Parsing CS 540 George Mason University.
Operator precedence parser Lecturer: Noor Dhia
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Syntax Analysis By Noor Dhia Left Recursion: Example1: S → S0s1s | 01 The grammar after eliminate left recursion is: S → 01 S’ S' → 0s1sS’
Syntax and Semantics Structure of programming languages.
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
Chapter 4 - Parsing CSCE 343.
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
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
lec04-bottomupparser June 6, 2018 Bottom-Up Parsing
Unit-3 Bottom-Up-Parsing.
Revision ? E  TE  E   + TE  |  T  FT  T   * FT  | 
Parsing IV Bottom-up Parsing
Compiler Construction
Bottom-Up Syntax Analysis
Shift Reduce Parsing Unit -3
Syntax Analysis source program lexical analyzer tokens syntax analyzer
4d Bottom Up Parsing.
BOTTOM UP PARSING Lecture 16.
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Compiler Construction
Parsing IV Bottom-up Parsing
LR Parsing. Parser Generators.
Bottom-Up Parsing “Shift-Reduce” Parsing
Syntax Analysis - Parsing
4d Bottom Up Parsing.
Parsing Bottom-Up.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Compiler Construction
Bottom-up parsing is also known as shift-reduce parsing
4d Bottom Up Parsing.
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Parsing Bottom-Up Introduction.
4d Bottom Up Parsing.
OPERATOR GRAMMAR No Ɛ-transition. No two adjacent non-terminals. Eg.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

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 LR parsing (Shift – Reduce Parser)

Bottom Up Parsing “Shift-Reduce” Parsing Reduce a string to the start symbol of the grammar. At every step a particular sub-string 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. Reverse order abbcde aAbcde aAde aABe S Consider: S  aABe A  Abc | b B  d Rightmost Derivation: S  aABe  aAde  aAbcde  abbcde

Operator-Precedence Parser Operator grammar small, but an important class of grammars we may have an efficient operator precedence parser (a shift-reduce parser) for an operator grammar. In an operator grammar, no production rule can have:  at the right side two adjacent non-terminals at the right side. Ex: EAB EEOE EE+E | Aa Eid E*E | Bb O+|*|/ E/E | id not operator grammar not operator grammar operator grammar

Precedence Relations In operator-precedence parsing, we define three disjoint precedence relations between certain pairs of terminals. a <. b b has higher precedence than a a =· b b has same precedence as a a .> b b has lower precedence than a The determination of correct precedence relations between terminals are based on the traditional notions of associativity and precedence of operators. (Unary minus causes a problem).

Using Operator-Precedence Relations The intention of the precedence relations is to find the handle of a right-sentential form, <. with marking the left end, =· appearing in the interior of the handle, and .> marking the right hand. In our input string $a1a2...an$, we insert the precedence relation between the pairs of terminals (the precedence relation holds between the terminals in that pair).

Operator-Precedence Parsing Algorithm The input string is w$, the initial stack is $ and a table holds precedence relations between certain terminals Algorithm: set p to point to the first symbol of w$ ; repeat forever if ( $ is on top of the stack and p points to $ ) then return else { let a be the topmost terminal symbol on the stack and let b be the symbol pointed to by p; if ( a <. b or a =· b ) then { /* SHIFT */ push b onto the stack; advance p to the next input symbol; } else if ( a .> b ) then /* REDUCE */ repeat pop stack until ( the top of stack terminal is related by <. to the terminal most recently popped ); else error();

Using Operator -Precedence Relations The partial operator-precedence table for this grammar Then the input string id+id*id with the precedence relations inserted will be: $ <. id .> + <. id .> * <. id .> $ id + * $ .> <.

Operator-Precedence Parsing Algorithm -- Example stack input action $ id+id*id$ $ <. id shift $id +id*id$ id .> + reduce E  id $ +id*id$ shift $+ id*id$ shift $+id *id$ id .> * reduce E  id $+ *id$ shift $+* id$ shift $+*id $ id .> $ reduce E  id $+* $ * .> $ reduce E  E*E $+ $ + .> $ reduce E  E+E $ $ accept id + * $ .> <.

Disadvantages of Operator Precedence Parsing It cannot handle the unary minus (the lexical analyzer should handle the unary minus). Small class of grammars. Difficult to decide which language is recognized by the grammar. Advantages: simple powerful enough for expressions in programming languages

Handling Unary Minus Operator-Precedence parsing cannot handle the unary minus when we also have the binary minus in our grammar. The best approach to solve this problem, let the lexical analyzer handle this problem. The lexical analyzer will return two different tokens for the unary minus and the binary minus. The lexical analyzer will need a lookhead to distinguish the binary minus from the unary minus. Then, we make  <. unary-minus for any operator unary-minus .>  if unary-minus has higher precedence than  unary-minus <.  if unary-minus has lower (or equal) precedence than 

Example id ( ) $ e3 .> <. =. e4 e2 e1 e1: Called when : whole expression is missing insert id onto the input issue diagnostic: ‘missing operand’ e2: Called when : expression begins with a right parenthesis delete ) from the input issue diagnostic: ‘unbalanced right parenthesis’

Example id ( ) $ e3 .> <. =. e4 e2 e1 e3: Called when : id or ) is followed by id or ( insert + onto the input issue diagnostic: ‘missing operator’ e4: Called when : expression ends with a left parenthesis pop ( from the stack issue diagnostic: ‘missing right parenthesis’

LALR Parser (Look Ahead LR) Advantages 1. An LALR parser can be automatically generated from an LALR grammar. 2. An LALR grammar can be used to define many computer languages. 3. An LALR parser is small. 4. An LALR parser is fast (if the parsing algorithm uses a matrix parser-table format). 5. An LALR parser is linear in speed (i.e. the speed is based on the size of the input text file only and not based on the size of the language being recognized). 6. The LALR grammar provides valuable documentation of the language being recognized. 7. Error recovery may already be built-in to the parser. 8. Generalized error messages may already be built into the parser. 9. Abstract-syntax-tree construction may already be built into the parser. 10. Recognition of context-sensitive language constructs may already be built into the parser

LALR Disadvantages 1. Software engineers are required to use an LALR parser generator, which may or may not be user friendly and may require some learning time. 2. Implementing meaningful error messages in the parser may be very difficult or impossible. 3. Understanding the parsing algorithm is often quite difficult. 4. If an error occurs, it may be difficult to determine whether it's in the grammar or the parser code. 5. If there is an error in the parser generator, this may be very difficult to fix.