Compiler Construction

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.
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.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
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.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
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.
CS 536 Spring Introduction to Bottom-Up Parsing Lecture 11.
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,
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:
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.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
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.
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
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.
Syntax Analyzer (Parser)
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Syntax and Semantics Structure of programming languages.
CSE P501 – Compilers LR Parsing Build Bottom-Up Parse Tree Handles
Parsing & Context-Free Grammars
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
Parsing and Parser Parsing methods: top-down & bottom-up
Unit-3 Bottom-Up-Parsing.
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
Revision ? E  TE  E   + TE  |  T  FT  T   * FT  | 
Parsing IV Bottom-up Parsing
Compiler Construction
CS 404 Introduction to Compiler Design
Fall Compiler Principles Lecture 4: Parsing part 3
Parsing Techniques.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis source program lexical analyzer tokens syntax analyzer
4d Bottom Up Parsing.
Lecture (From slides by G. Necula & R. Bodik)
BOTTOM UP PARSING Lecture 16.
Lecture 8 Bottom Up Parsing
Bottom Up Parsing.
Compiler Construction
Parsing IV Bottom-up Parsing
LR Parsing. Parser Generators.
Bottom-Up Parsing “Shift-Reduce” Parsing
4d Bottom Up Parsing.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Compiler Construction
Compiler Construction
Bottom-up parsing is also known as shift-reduce parsing
Compiler Construction
4d Bottom Up Parsing.
Compiler Construction
4d Bottom Up Parsing.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

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

Bottom-up Parsing Bottom-up parsing is more general than top-down parsing Bottom-up parsers handle a large class of grammars. Preferred method in practice This is a note compiler: Bottom-up Parsing

Bottom-up Parsing Bottom-up parsing is more general than top-down parsing Bottom-up parsers handle a large class of grammars. Preferred method in practice This is a note compiler: Bottom-up Parsing

Bottom-up Parsing Bottom-up parsing is more general than top-down parsing Bottom-up parsers handle a large class of grammars. Preferred method in practice This is a note compiler: Bottom-up Parsing

Bottom-up Parsing Also called LR parsing L means that tokens are read left to right R means that the parser constructs a rightmost derivation.

Bottom-up Parsing Also called LR parsing L means that tokens are read left to right R means that the parser constructs a rightmost derivation.

Bottom-up Parsing Also called LR parsing L means that tokens are read left to right R means that the parser constructs a rightmost derivation.

Bottom-up Parsing LR parsers donot need left-factored grammars LR parsers can handle left-recursive grammars

Bottom-up Parsing LR parsers donot need left-factored grammars LR parsers can handle left-recursive grammars

Bottom-up Parsing LR parsing reduces a string to the start symbol by inverting productions.

Bottom-up Parsing A derivation consists of a series of rewrite steps S  g0  g1  ...  gn-1  gn  sentence

Bottom-up Parsing S  g0  ...  gn  sentence Each gi is a sentential form if g contains only terminals, g is a sentence in L(G) If g contains  1 nonterminals, g is a sentential form

Bottom-up Parsing S  g0  ...  gn  sentence Each gi is a sentential form if g contains only terminals, g is a sentence in L(G) If g contains  1 nonterminals, g is a sentential form

Bottom-up Parsing S  g0  ...  gn  sentence Each gi is a sentential form if g contains only terminals, g is a sentence in L(G) If g contains  1 nonterminals, g is a sentential form

Bottom-up Parsing A bottom-up parser builds a derivation by working from input sentence back towards the start symbol S S  g0  ...  gn  sentence

Bottom-up Parsing Consider the grammar S → aABe A → Abc | b B → d

Bottom-up Parsing The sentence abbcde can be reduced to S: abbcde aAbcde aAde aABe S

Bottom-up Parsing The sentence abbcde can be reduced to S: abbcde aAbcde aAde aABe S

Bottom-up Parsing The sentence abbcde can be reduced to S: abbcde aAbcde aAde aABe S

Bottom-up Parsing The sentence abbcde can be reduced to S: abbcde aAbcde aAde aABe S

Bottom-up Parsing The sentence abbcde can be reduced to S: abbcde aAbcde aAde aABe S

Bottom-up Parsing These reductions, in fact, trace out the following right-most derivation in reverse: S  aABe  aAde  aAbcde  abbcde

S  aBy  agy  xy rm rm rm S rule: B → g B a g x x y Terminals only

Bottom-up Parsing Consider the grammar 1. E → E + (E) 2. | int

Bottom-up Parsing Consider bottom-up parse of the string int + (int) + (int)

int + (int) + (int) int + ( int ) + ( int )

int + (int) + (int) E + (int) + (int) E int + ( int ) + ( int )

int + (int) + (int) E + (int) + (int) E + (E) + (int) E E int + ( int

int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int) E E E

int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int)

int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int) A rightmost derivation in reverse int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int) E + (E) E E E E E int + ( int ) + ( int )

Bottom-up Parsing An LR parser traces a rightmost derivation in reverse

Consequence Let abg be a step of a bottom-up parse Assume that next reduction is A → b Then g is a string of terminals!

Consequence Let abg be a step of a bottom-up parse Assume that next reduction is A → b Then g is a string of terminals!

Consequence Let abg be a step of a bottom-up parse Assume that next reduction is A → b Then g is a string of terminals!

Consequence Why? Because aAg → abg is a step in a rightmost derivation

Notation Idea: Split the string into two substrings

Notation Right substring (a string of terminals) is as yet unexamined by parser Left substring has terminals and non-terminals

Notation Right substring (a string of terminals) is as yet unexamined by parser Left substring has terminals and non-terminals

Notation The dividing point is marked by a ► The ► is not part of the string Initially, all input is unexamined: ►x1 x1 . . . xn

Shift-Reduce Parsing 1. Shift 2. Reduce Bottom-up parsing uses only two kinds of actions: 1. Shift 2. Reduce

Shift Move ► one place to the right shifts a terminal to the left string E + (► int)  E + (int ►)

Reduce Apply an inverse production at the right end of the left string. If E → E + (E) is a production, then E + ( E+(E)►)  E + ( E ►)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example ►int + (int) + (int) $ shift reduce E → int E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E + (E ►) + (int) $ E + (E) ► + (int) $ reduce E → E+(E)

Shift-Reduce Example E ► + (int) $ shift 3 times E + (int ►) $ reduce E → int E + (E ►) $ shift E + (E) ► $ red E → E+(E) E ► $ accept