Parsing Bottom-Up.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
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.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
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.
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.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
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.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
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.
Chapter 4 Syntactic Analysis II. Chapter 4 -- Syntactic Analysis II2 1. Introduction to Bottom-Up parsing  Grammar: E--> E+E | E*E | i  Expression:
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
March 1, 2009 Dr. Muhammed Al-Mulhem 1 ICS 482 Natural Language Processing LR Parsing Muhammed Al-Mulhem March 1, 2009.
Parsing Top-Down.
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.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
Lecture 5: LR Parsing CS 540 George Mason University.
Operator precedence parser Lecturer: Noor Dhia
Error recovery in predictive parsing An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Bottom-up parsing Goal of parser : build a derivation
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Chapter 2 :: Programming Language Syntax
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
CS 404 Introduction to Compiler Design
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
4 (c) parsing.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Top-Down Parsing CS 671 January 29, 2008.
4d Bottom Up Parsing.
BOTTOM UP PARSING Lecture 16.
Lecture 8 Bottom Up Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Ambiguity in Grammar, Error Recovery
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Compiler Construction
Parsing IV Bottom-up Parsing
Compiler SLR Parser.
LR Parsing. Parser Generators.
Bottom-Up Parsing “Shift-Reduce” Parsing
Syntax Analysis - Parsing
4d Bottom Up Parsing.
Kanat Bolazar February 16, 2010
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
4d Bottom Up Parsing.
Compiler Construction
Compiler Construction
Parsing Bottom-Up LR Table Construction.
4d Bottom Up Parsing.
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Parsing Bottom-Up Introduction.
Parsing Bottom-Up LR Table Construction.
Chap. 3 BOTTOM-UP PARSING
4d Bottom Up Parsing.
Readahead FSMs, Readback FSMs, and Reduce States
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Parsing Bottom-Up

Review Bottom-up Parsing is often called what? What is a Handle? Shift-Reduce Parsing What is a Handle? Def: a handle is a right hand side of a production that we can reduce to get to the preceding step in the derivation. What Parser did we cover last time? Operator Precedence Parser Parsing Bottom-Up

Parsing Bottom-Up

The LR Parser The most powerful of all parsers that we will consider (Knuth, 1965) They can handle the widest variety of CFG's (including everything that predictive parsers and precedence parsers can handle) Parsing Bottom-Up

They work fast, and can detect errors as soon as possible. (as soon as the first incorrect token is encountered) It is also easy to extend LR parsers to incorporate intermediate code generation. Parsing Bottom-Up

Def: LR(k) -- Left to right scan of the tokens, Rightmost derivation, k-character lookahead. The larger the lookahead (k) the larger the table. Hopcroft and Ullman (1979) have shown that any deterministic CFL can be handled by an LR(1) parser, so that is the kind we will learn. Parsing Bottom-Up

Table Layout: states | <- Terminals -> | <- Non-Term. -> -------------------------------------------------- 0 | | 1 | Action | go-to 2 | part | part ... | | LR Parser tables tend to be large. For economy, we don't place the productions themselves in the table; instead, we number them and refer to them in the table by number. Parsing Bottom-Up

place $ at end of input, state 0 on stack. Repeat Until input is accepted or an error Let qm be the current state (at the top of the stack) and let ai be the incoming token. Enter the action part of the table; X=Table[ qm,ai] Case X of Shift qn: Shift (that is, push) ai onto the stack and enter State qn. (We mark the fact that we have entered that state by pushing it onto the stack along with ai Reduce n: Reduce by means of production #n. (We do the reduction in essentially the same was as in the operator-precedence parser, except for managing the states.) When the left-hand side has been pushed, we must also place a new state on the stack using the go-to part of the table. Accept: parse is complete Error: Indicate input error Parsing Bottom-Up

The only complicated thing is reducing. 1. If the right hand side of the indicated production has k symbols, pop the top k things off the stack (that is, k state-symbol pairs). This is the handle. If the right hand side is epsilon, nothing is popped.) 2. Next, note the state on the top of the stack (after the handle has been popped). Suppose it is qj. Parsing Bottom-Up

3. Suppose the left-hand side is X. Enter the go-to part of the table at [qj, X] and note the entry. It will be a state; suppose it is qk 4. Push X and the new state qk onto the stack. Parsing Bottom-Up

We will use our familiar grammar for expressions: (with productions numbered) (1) E -> E + T (2) E -> E - T (3) E -> T (4) T -> T * F (5) T -> T / F (6) T -> F (7) F -> ( E ) (8) F -> I Parsing Bottom-Up

Parse (i+i)/i Parsing Bottom-Up

Parsing Bottom-Up

Parse i*(i-i Parsing Bottom-Up

Parsing Bottom-Up

Parsing Bottom-Up