Compiler Construction

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

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.
Compiler Construction Sohail Aslam Lecture StackInput ¤0¤0 id – id  id $ s4 ¤0 id 4 – id  id $ r6 F → id ¤0F3¤0F3 – id  id $ r5 T → F ¤0T2¤0T2.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
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.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
1 CIS 461 Compiler Design & Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #12 Parsing 4.
Compiler Construction CS 606 Sohail Aslam Lecture 2.
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.
– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:
Bottom-up parsing Goal of parser : build a derivation
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.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntax and Semantics Structure of programming languages.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Operator precedence parser Lecturer: Noor Dhia
Syntax and Semantics Structure of programming languages.
Introduction to Parsing
Parsing #1 Leonidas Fegaras.
Parsing — Part II (Top-down parsing, left-recursion removal)
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.
Bottom-Up Parsing.
Parsing and Parser Parsing methods: top-down & bottom-up
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
CS 404 Introduction to Compiler Design
Fall Compiler Principles Lecture 4: Parsing part 3
Parsing Techniques.
Shift Reduce Parsing Unit -3
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lexical and Syntax Analysis
Lecture 7 Predictive Parsing
BOTTOM UP PARSING Lecture 16.
Lecture 7: Introduction to Parsing (Syntax Analysis)
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Compiler Construction
Parsing IV Bottom-up Parsing
Compiler Construction
LR Parsing. Parser Generators.
Parsing — Part II (Top-down parsing, left-recursion removal)
Bottom-Up Parsing “Shift-Reduce” Parsing
Syntax Analysis - Parsing
Compiler Construction
Lecture 7 Predictive Parsing
5. Bottom-Up Parsing Chih-Hung Wang
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Compiler Construction
Compiler Construction
Compiler Construction
Compiler Construction
Bottom-up parsing is also known as shift-reduce parsing
Compiler Construction
Nonrecursive Predictive Parsing
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Parsing Bottom-Up Introduction.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Compiler Construction Sohail Aslam Lecture 22 compiler: Bottom-up Parsing

Expression Grammar 1 Goal → Expr 2 Expr + Term 3 | Expr – Term 4 Term 5 Term  Factor 6 Term ∕ Factor 7 Factor 8 num 9 id 10 ( Expr )

The states of the bottom-up parser on input The states of the bottom-up parser on input x – 2  y (tokenized as id – num * id) are

word Stack Handle Action 1 id ► - none - shift 2 – id ► Factor → id,1 reduce 3 Factor ► Term → Factor,1 4 Term ► Expr → Term,1 5 Expr ► 6 num Expr – ► 7  Expr – num ► Factor → num,3 8 Expr – Factor ► Term → Factor,3

word Stack Handle Action 9  Expr – Term ► - none - shift 10 id Expr – Term  ► 11 $ Expr – Term  id► Factor → id,5 reduce 12 Expr–Term  Factor► Term→TermFactor,5 13 Expr → Expr – Term,3 14 Expr ► Goal → Expr,1 15 Goal accept

Handles The handle-finding mechanism is the key to effiecient bottom-up parsing As it process an input string, the parser must find and track all potential handles

Handles The handle-finding mechanism is the key to effiecient bottom-up parsing As it process an input string, the parser must find and track all potential handles

Handles For example, every legal input eventually reduces the entire frontier to grammar’s goal symbol Thus, Goal → Expr,1 is a potential handle at the start of every parse

Handles For example, every legal input eventually reduces the entire frontier to grammar’s goal symbol Thus, Goal → Expr,1 is a potential handle at the start of every parse

Handles As the parser builds a derivation, it discovers other handles At each step, the set of potential handles represent different suffixes that lead to a reduction.

Handles As the parser builds a derivation, it discovers other handles At each step, the set of potential handles represent different suffixes that lead to a reduction.

Handles Each potential handle represent a string of grammar symbols that, if seen, would complete the right-hand side of some production.

Handles For the bottom-up parse of the expression grammar string, we can represent the potential handles that the shift-reduce parser should track

Handles Using the placeholder  to represent top of the stack, there are nine handles:

Same Same Handles 1 Factor → id  2 Term → Factor 3 Expr → Term  4 Factor → num 5 6 7 Term → Term  Factor  8 Expr → Expr – Term  9 Goal → Expr  Same Same

Handles This notation shows that the second and fifth handles are identical, as are first and sixth It also create a way to represent the potential of discovering a handle in future.

Handles This notation shows that the second and fifth handles are identical, as are first and sixth It also create a way to represent the potential of discovering a handle in future.

Handles Consider the parser’s state in step 6: 6 num Expr – ► - none - shift 7  Expr – num ► Factor → num,3 reduce 8 Expr – Factor ► Term → Factor,3 9 Expr – Term ► 10 id Expr – Term  ► 11 $ Expr – Term  id► Factor → id,5 12 Expr–TermFactor► Term→TermFactor,5 13 Expr→Expr–Term,3

Handles The parser has recognized Expr – It needs to recognize, eventually, a Term before it can reduce this part of the frontier 6 num Expr – ► - none - shift 7  Expr – num ► Factor → num,3 reduce 8 Expr – Factor ► Term → Factor,3 9 Expr – Term ► 10 id Expr – Term  ► 11 $ Expr – Term  id► Factor → id,5 12 Expr–TermFactor► Term→TermFactor,5 13 Expr→Expr–Term,3

Handles Using the stack-relative notation, we can represent the parser’s state as Expr → Expr –  Term The parser has already recognized an Expr and a –

Handles Using the stack-relative notation, we can represent the parser’s state as Expr → Expr –  Term The parser has already recognized an Expr and a –

Handles If the parser reaches a state where it shifts a Term on top of Expr and –, it will complete the handle Expr → Expr – Term 

Handles How many potential handles must the parser recognize? The right-hand side of each production can have a placeholder at its start, at its end and between any two consecutive symbols

Handles How many potential handles must the parser recognize? The right-hand side of each production can have a placeholder at its start, at its end and between any two consecutive symbols

Handles Expr →  Expr –Term Expr → Expr  – Term Expr → Expr – Term if the right-hand side has k symbols, it has k +1 placeholder positions