Parsing Bottom-Up Introduction.

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 construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
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 536 Spring Introduction to Bottom-Up Parsing Lecture 11.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
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.
OPERATOR PRECEDENCE PARSING
Ambiguity, LL1 Grammars and Table-driven Parsing
Chapter 4 Syntactic Analysis II. Chapter 4 -- Syntactic Analysis II2 1. Introduction to Bottom-Up parsing  Grammar: E--> E+E | E*E | i  Expression:
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.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
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.
Operator precedence parser Lecturer: Noor Dhia
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
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.
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
Parsing #1 Leonidas Fegaras.
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
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Parsing IV Bottom-up Parsing
CS 404 Introduction to Compiler Design
Compiler Construction
Bottom-Up Syntax Analysis
Syntax Analysis Part II
4 (c) parsing.
Shift Reduce Parsing Unit -3
Subject Name:COMPILER DESIGN Subject Code:10CS63
Regular Grammar - Finite Automaton
Lexical and Syntax Analysis
Lexical and Syntactic Analysis
4d Bottom Up Parsing.
Parsing #2 Leonidas Fegaras.
Lecture (From slides by G. Necula & R. Bodik)
BOTTOM UP PARSING Lecture 16.
Lecture 8 Bottom Up Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Compiler Construction
Parsing IV Bottom-up Parsing
Chapter 4. Syntax Analysis (2)
LR Parsing. Parser Generators.
Parsing #2 Leonidas Fegaras.
Bottom-Up Parsing “Shift-Reduce” Parsing
4d Bottom Up Parsing.
Kanat Bolazar February 16, 2010
Parsing Bottom-Up.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Chapter 4. Syntax Analysis (2)
4d Bottom Up Parsing.
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Chap. 3 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:

Parsing Bottom-Up Introduction

Introduction to Bottom-Up parsing Grammar: E--> E+E | E*E | i Expression: i+i*i Rightmost derivation: E =>E+E E=> E+E*E E=>E+E*i E=>E+i*i E=>i+i*i Parsing Bottom-Up

Parsing with a Stack We will push tokens onto the stack until we see something to reduce. This something is called a "handle" This is known as shifting and reducing. Def: a handle is a right hand side of a production that we can reduce to get to the preceding step in the derivation. Parsing Bottom-Up

We carry out the reduction by popping the right hand side off of the stack and pushing the left hand side on in its place. Notice: a handle is not just any right-hand side; it has to be the correct one -- the one that takes us one step back in the derivation. Parsing Bottom-Up

More about Handles The bottom up parser's problem is to find a way of detecting when there is a handle at the top of the stack. If there is, reduce it; otherwise shift. For this reason bottom up parsers are often called shift-reduce parsers When selecting handles, some things may be the right hand side, but may not be handles. Parsing Bottom-Up

Parsing Bottom-Up

The Operator-Precedence Parser This is the simplest bottom-up parser (and the least powerful parser for CFG's) It is generally simpler to construct table entities consist of <., =, and .> handles look like <.====.> Parsing Bottom-Up

A Simple Operator-Precedence Parser Grammar: E -> E + E | E * E | ( E ) | i Table: Parsing Bottom-Up

Algorithm: Push a $ on stack and append $ to end of input repeat x=top teminal on stack, y is incoming Find table relationship (x,y) if x<.y or x=y, then shift. if x.>y there is a handle on stack (<. to .>) Reduce & push LHS of production on stack. If the table entry is blank, or handle is not a RHS, there is an error. until x = $ and y = $ or an error is found Parsing Bottom-Up

Parse: i+i*i Parsing Bottom-Up

Parsing Bottom-Up

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

Parsing Bottom-Up

Parse: ( ) Parsing Bottom-Up

Parsing Bottom-Up

Forming the Table Grammar Restrictions There must never be 2 or more consecutive non-terminals on the right hand side. No 2 distinct non-terminals may have the same right hand side. For any 2 terminals at most 1 of <., =, or .> may hold No e productions Parsing Bottom-Up

Rules for building the table: If a has higher precedence than b, then a .>b and b<.a, regardless of the associativity. If a and b have equal precedence, then relations depend upon associativity. If left associative a.>b and b.>a If right associative a<.b and b<.a Parsing Bottom-Up

Rules for building the table (cont.): Paired operators like ( ) or [ ] are related by = We force the parser to reduce expressions inside these operators by having ( <.a and a.>) Similarly we force the parser to reduce ( E ) before shifting any other terminals by having a <. ( and ) .>a, where a is any terminal that may legally precede ( or follow ) Parsing Bottom-Up

Rules for building the table (cont.): For identifiers, i.>a and a<.i, where a is any terminal that may legally precede or follow an identifier. End markers have lower precedence than any other terminal. Parsing Bottom-Up

Parsing Bottom-Up