Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Professor Yihjia Tsai Tamkang University
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
Syntax and Semantics Structure of programming languages.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
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.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntax and Semantics Structure of programming languages.
Ambiguity in Grammar By Dipendra Pratap Singh 04CS1032.
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.
4. Bottom-up Parsing Chih-Hung Wang
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Operator precedence parser Lecturer: Noor Dhia
7. Symbol Table Chih-Hung Wang Compilers References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., D.
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
Syntax and Semantics Structure of programming languages.
Introduction to Parsing
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Parsing #1 Leonidas Fegaras.
Parsing — Part II (Top-down parsing, left-recursion removal)
Constructing Precedence Table
Chapter 4 - Parsing CSCE 343.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Bottom-Up Parsing.
Lexical and Syntax Analysis
Unit-3 Bottom-Up-Parsing.
Introduction to Parsing (adapted from CS 164 at Berkeley)
8. Symbol Table Chih-Hung Wang
Parsing IV Bottom-up Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing with Context Free Grammars
Bottom-Up Syntax Analysis
4 (c) parsing.
CS416 Compiler Design lec00-outline September 19, 2018
Parsing Techniques.
Shift Reduce Parsing Unit -3
Compiler Design 4. Language Grammars
Lexical and Syntax Analysis
Introduction CI612 Compiler Design CI612 Compiler Design.
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
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.
R.Rajkumar Asst.Professor CSE
Parsing IV Bottom-up Parsing
Introduction to Parsing
Introduction to Parsing
Parsing #2 Leonidas Fegaras.
Parsing — Part II (Top-down parsing, left-recursion removal)
Bottom-Up Parsing “Shift-Reduce” Parsing
CS416 Compiler Design lec00-outline February 23, 2019
5. Bottom-Up Parsing Chih-Hung Wang
Kanat Bolazar February 16, 2010
USING AMBIGUOUS GRAMMARS
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Parsing Bottom-Up Introduction.
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Ben-Gurion University
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing Compiler Design Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing

Bottom-Up Parsing: Problems Two or more rules have the same handle Ambiguous grammars Operator-Precedence

Bottom-Up Parsers Operator-precedence parser LR parser the least powerful, but simple & easy-to-make LR parser the most powerful, but complex and hard-to-make

Operator-Precedence Parser Grammar must have three conditions: No two distinct non-terminals have the same handle No ε-rules No two adjacent non-terminals in any RHS Grammar: E ::= E A E | (E) | -E | id A ::= + | - | * | / Is not “Operator grammar” since it does not comply with the 3ed condition Can be converted to Operator grammar as follow: E ::= E + E | E – E | E * E | E / E | (E) | -E | id E ::= E + T | E - T | T T ::= T * F | T / F | F F ::= num | id

Precedence Relations a < b a has less precedence than b a = b a has the same precedence as b a > b a has higher precedence than b

Precedence Relations E ::= E + T | E - T | T T ::= T * F | T / F | F F F id(y) id(x) num(2) E ::= E + T | E - T | T T ::= T * F | T / F | F F ::= num | id 6

Idea Behind Operator Precedence Parsing Insert precedence relations in the input string between every adjacent terminals: $ id1 + id2 * id3 $ => $ < id1 > + < id2 > * < id3 > $ To find the handle: Scan the input left to right until first > scan right to left until first < The handle is between < > In the example above: the handle is < id1 > Then substitute the LHS non-terminal instead of the handle => $ E+ id2 * id3 $ And so on ..

Idea Behind Operator Precedence Parsing (cont) If the string is reduced to E1 + E2 * E3, we have two handles: E1 + E2 and E2 * E3 Which of them to choose? Reduce the string to $ + * $ by removing non-terminals Insert precedence relations: $ < + < * > $ The handle is: * Insert non-terminals around it E2 * E3

Precedence Table Let G= (N, T, P, S) be CFG, where T= {a1, a2, …, an}

Operator-Precedence Parser: Algorithm

Idea of the Algorithm Shift action causes the input operator to be evaluated before the stack operator Up in the stack Reduce soon Down at the parse tree Reduce action causes the stack operator to evaluated before the input operator

Operator-Precedence Parser: Example

Operator-Precedence Parsing: Example

Construction of Precedence Table Two methods to assign precedence relations between every two terminals in the grammar: Method 1: Use some rule of thumbs based on precedence and associativity of the language operators Method 2: Construct equivalent unambiguous grammar to reflect precedence and associativity Induce the precedence relations from it (as before from parse trees)

References Compilers: Principles, Techniques and Tools, Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. 1st Edition, Addison-Wesley, 1986 Teaching Materials of: Elements of Compiler Design, Alexander Meduna ,Taylor & Francis, New York, 2007