CS 321 Programming Languages and Compilers. Bottom Up Parsing

Similar documents
12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins.

ECE 468/573 Midterm 1 September 28, 2012

Compilers Spring 2013 PRACTICE Midterm Exam

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

Midterm I Solutions CS164, Spring 2006

CS 430 Spring Mike Lam, Professor. Parsing

Fig.25: the Role of LEX

CSE 401 Midterm Exam 11/5/10 Sample Solution

Eliminating left recursion grammar transformation. The transformed expression grammar

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22)

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

Top-down vs Bottom-up. Bottom up parsing. Sentential form. Handles. Handles in expression example

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing

Definition of Regular Expression

CMSC 331 First Midterm Exam

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

Lexical Analysis: Constructing a Scanner from Regular Expressions

Assignment 4. Due 09/18/17

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

stack of states and grammar symbols Stack-Bottom marker C. Kessler, IDA, Linköpings universitet. 1. <list> -> <list>, <element> 2.

TO REGULAR EXPRESSIONS

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CSCE 531, Spring 2017, Midterm Exam Answer Key

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

Dr. D.M. Akbar Hussain

CSc 453 Compilers and Systems Software. 6 : Top-Down Parsing I

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

Lexical analysis, scanners. Construction of a scanner

Topic 2: Lexing and Flexing

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

Scanner Termination. Multi Character Lookahead

CMPSC 470: Compiler Construction

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

Reducing a DFA to a Minimal DFA

Principles of Programming Languages

Theory of Computation CSE 105

CS201 Discussion 10 DRAWTREE + TRIES

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or

ASTs, Regex, Parsing, and Pretty Printing

Lexical Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

CS 241 Week 4 Tutorial Solutions

Geometric transformations

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

Example: Source Code. Lexical Analysis. The Lexical Structure. Tokens. What do we really care here? A Sample Toy Program:

2014 Haskell January Test Regular Expressions and Finite Automata

2 Computing all Intersections of a Set of Segments Line Segment Intersection

Principles of Programming Languages

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications

LR Parsing Techniques

COMP 423 lecture 11 Jan. 28, 2008

Unit #9 : Definite Integral Properties, Fundamental Theorem of Calculus

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID:

CS 221: Artificial Intelligence Fall 2011

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Compiler Construction D7011E

Context-Free Grammars

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

Compilation

UNIT-III BOTTOM-UP PARSING

Should be done. Do Soon. Structure of a Typical Compiler. Plan for Today. Lab hours and Office hours. Quiz 1 is due tonight, was posted Tuesday night

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Stack. A list whose end points are pointed by top and bottom

Context-Free Grammars

Today. Search Problems. Uninformed Search Methods. Depth-First Search Breadth-First Search Uniform-Cost Search

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

Data sharing in OpenMP

a < a+ x < a+2 x < < a+n x = b, n A i n f(x i ) x. i=1 i=1

Slides for Data Mining by I. H. Witten and E. Frank

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Regular Expression Matching with Multi-Strings and Intervals. Philip Bille Mikkel Thorup

Chapter Spline Method of Interpolation More Examples Electrical Engineering

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

shift-reduce parsing

Control-Flow Analysis and Loop Detection

CSEP 573 Artificial Intelligence Winter 2016

Presentation Martin Randers

CSCI 446: Artificial Intelligence

Lexical Analysis and Lexical Analyzer Generators

Dynamic Programming. Andreas Klappenecker. [partially based on slides by Prof. Welch] Monday, September 24, 2012

Monday, September 13, Parsers

INDIAN COMMUNITY SCHOOL INFORMATICS PRACTICES 2012

CMSC 430, Practice Problems 1 (Solutions)

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev

Today. CS 188: Artificial Intelligence Fall Recap: Search. Example: Pancake Problem. Example: Pancake Problem. General Tree Search.

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Transcription:

CS 321 Progrmming nguges nd Compilers Bottom Up Prsing

Bottom-up Prsing: Shift-reduce prsing Grmmr H: fi ; fi b Input: ;;b hs prse tree ; ; b 2

Dt for Shift-reduce Prser Input string: sequence of tokens being checked for grmmticl correctness Stck: sententil form representing the input seen so fr Trees Constructed: the prse trees tht hve been constructed so fr t some point in the prsing 3

Opertions of shift-reduce prser Shift: move input token to the set of trees s singleton tree. Reduce: colesce one or more trees into single tree (ccording to some production). Accept: terminte nd ccept the token strem s grmmticlly correct. Reject: Terminte nd reject the token strem. 4

A prse for grmmr H Input Stck Action Trees ;;b Shift ;;b Reduce fi ;;b Shift ;b ; Shift ; 5

A prse for grmmr H Input Stck Action Trees ;b ; Reduce fi ; ;b ; Shift ; b ;; Shift ; ; 6

A prse for grmmr H Input Stck Action Trees $ ;;b Reduce fib ; ; b $ ;; Reduce fi ; ; $ ;; Reduce fi; b ; ; b 7

A prse for grmmr H Input Stck Action Trees $ ; Reduce fi; ; ; $ Acc b ; ; b 8

Bottom-up Prsing Chrcteristic utomt for n R grmmr: tells when to shift/reduce/ccept or reject hndles vible prefixes 9

A prse for grmmr H Input Stck Action Stck+Input ;;b Shift ;;b ;;b Reduce fi ;;b ;;b Shift ;;b ;b ; Shift ;;b ;b ; Reduce fi ;;b ;b ; Shift ;;b b ;; Shift ;;b $ ;;b Reduce fib ;;b $ ;; Reduce fi ;; $ ;; Reduce fi; ;; $ ; Reduce fi; ; $ Accept 10

Bottom-up prsing? Stck+Input ; ;; ;; ;;b ;;b ;;b ;;b ;;b ;;b ;;b ;;b ; ; b ; ; b 11

Hndles nd vible prefixes Stck+remining input = sententil form Hndle = the prt of the sententil form tht is reduced in ech step Vible prefix = the prefix of the sententil form in right-most derivtion tht do not extend beyond the end of the hndle.g. vible prefixes for H: (;)*( b) Vible prefixes form regulr set. 12

Chrcteristic Finite Stte Mchine (CFSM) Vible prefixes of H re recognized by this CFSM: 0 1 2 ; 5 6 b 3 b 4 13

How Bottom-Up Prser Works Run the CFSM on symbols in the stck If trnsition possible on the incoming input symbol, then shift, else reduce. Still need to decide which rule to use for the reduction. 14

Chrcteristic utomton strt 0 1 2 ; 5 6 Vible Prefixes b 3 4 b ;;b leds to stte 3 fter ;;b leds to stte 3 fter ; ;;b leds to stte 4 fter ;;b ;; leds to stte 2 fter ;; ;; leds to stte 6 fter ;; ; leds to stte 6 fter ; 15

Chrcteristic utomton strt 0 b 1 2 3 4 b ; 5 6 Stte Action 0,5 shift (if possible) 1 ccept 2 reduce, if OF shift otherwise 3 reduce 4 reduce b 6 reduce ; 16

xmple: expression grmmr fi +T T T fi T*P P P fi id id+id+id+id hs prse tree: T P + + T P id + T P id T P id id 17

A prse in this grmmr id+id+id+id Shift +id+id+id id Reduce +id+id+id P Reduce +id+id+id T Reduce +id+id+id Shift id+id+id + Shift +id+id +id Reduce +id+id +P Reduce +id+id +T Reduce +id+id Shift id+id + Shift 18

A prse in this grmmr (cont.) +id + id Reduce +id +P Reduce +id +T Reduce +id Shift id + Shift $ +id Reduce $ +P Reduce $ +T Reduce $ Reduce $ Accept 19

Chrcteristic Finite Stte Mchine The CFSM recognizes vible prefixes (strings of grmmr symbols tht cn pper on the stck): 1 + id T 5 8 0 id T P 4 2 * id P * P 7 9 3 20

Definitions Rightmost derivtion Right-sententil form Hndle Vible prefix Chrcteristic utomton 21

Rightmost Derivtion Definition: A rightmost derivtion in G is derivtion: S w1 i wi+1 K w K such tht for ech step i, the rightmost non-terminl in is replced to obtin w i + 1 w i 1. ; ;; ; ; ; ; ;; ;;b is not right-most 2. ; ; b ;; b ; ; b ; ;b ;;b is right-most 22

Right-sententil Forms Definition: A right-sententil form is ny sententil form tht occurs in right-most derivtion. ; ; b ;; b ; ; b ; ;b ;;b.g., ny of these 23

Hndles Definition: Assume the i-th step of rightmost derivtion is: w i =u i Av i u i αv i =w i+1 Then, (α, u i α ) is the hndle of w i+1 In n unmbiguous grmmr, ny sentence hs unique rightmost derivtion, nd so we cn tlk bout the hndle rther thn hndle. 24

The Pln Construct prser by first constructing the CFSM, then constructing GOTO nd ACTION tbles from it. Construction hs two prts: R(0) construction of the CFSM SR(1) construction of tbles 25

Constructing the CFSM: Sttes The sttes in the CFSM re creted by tking the closure of R(0) items Given production ;, these re ll induced R(0) items ; ; ; ; 26

Wht is is? The in ; represents the stte of the prse. Only this prt of the tree is fully developed ; 27

A Stte in the CFSM: Closure of R(0) Item For set I of R(0) items, clculte closure(i): 1. if A α B β is in closure(i), then for every production B γ, B γ is in closure(i) 2. closure(i) is the smllest set with property (1) 28

Closure of R(0) Item: xmple H: ; b closure({ ; }) = { ;, ;,,, b} 29

R(0) Mchine Given grmmr G with gol symbol S, ugment the grmmr by dding new gol symbol S nd production S fi S. Sttes = sets of R(0) items Strt stte = closure({s fi S}) All sttes re considered to be finl (set of vible prefixes closed under prefix) trnsition(i, X) = closure({a fi X b A fi Xb I}). 30

xmple: R(0) CFSM Construction Augment the grmmr: H: ; b Initil Stte is closure of this ugmenting rule : closure({ }) = I 0 : ; b 31

xmple: Trnsitions from I0 trnsition( I0, ) = { } = I1 trnsition( I0, ) = { ;, } = I2 trnsition( I 0, ) = { } = trnsition( I 0, b) = { b } = I 3 I 4 There re no other trnsitions from I 0. There re no trnsitions possible from I 1. Now consider the trnsitions from I 2. 32

Trnsitions from I 22 trnsition( I 2,;) = I 5 : ; ; b I 5 New stte: 6 trnsition(, ) = { ; }= I I2 I 3 trnsition(, ) = 5 trnsition(, ) = 5 I I trnsition(, b) = I5 I4 33

The CFSM Trnsition Digrm for H I 1 : I 0 : ; b b I 2 : I 3 ; : ; b I 5 : ; ; b I 6 : ; I 4 : b 34

Chrcteristic Finite Stte Mchine for H 0 1 2 ; 5 6 b 3 b 4 35

How R(1) prsers work GOTO tble: trnsition function of chrcteristic utomton in tbulr form ACTION tble: Stte Procedure: fiaction Use the GOTO tble to run the CFSM over the stck. Suppose stte reched t top of stck is s. Tke ction given by ACTION(s,), where is incoming input symbol. 36

Action Tble for H 0 1 2 3 4 5 6 b ; $ Shift Shift Accept Shift Reduce Reduce Reduce Reduce b Reduce b Shift Shift Reduce ; 37

SR(1) Prser Construction GOTO tble is the move function from the R(0) CFSM. ACTION tble is constructed from the CFSM s follows: If stte i contins A fi b, then ACTION(i,) = Shift. If stte i contins A fi, then ACTION(i,) = Reduce Afi (But, if A is, ction is Accept.) Otherwise, reject. But,... 38

SR(1) Prser Construction Rules for the ACTION tble cn involve shift/reduce conflicts. So the ctul rule for reduce ctions is: If stte i contins A fi, then ACTION(i,) = Reduce A fi, for ll FOOW(A)..g. stte 2 for grmmr H yields shift/reduce conflict. Nmely, should you shift the ; or reduce by fi. This is resolved by looking t the follow set for. Follow() = {$} 39

FIRST nd FOOW sets FIRST() = { * b} { * } FOOW(A) = { S * Ab} 40

Clculting FIRST sets Crete tble Fi mpping N to {}; initilly, Fi(A) = F for ll A. Repet until Fi does not chnge: For ech A fi P, Fi(A) := Fi(A) FIRST(, Fi) where FIRST(, Fi) is defined s follows: FIRST(, Fi) = {} FIRST(b, Fi) = {} FIRST(Bb, Fi) = Fi(B) FIRST(b,Fi), if Fi(B) Fi(B), o.w. 41

Clculting FOOW sets Clculte FOOW sets Crete tble Fo : N fi {$}; initilly, Fo(A) = F for ll A, except Fo(S) = {$} Repet until Fo does not chnge: For ech production A fi Bb, Fo(B) := Fo(B) FIRST(b) - {} For ech production A fi B, Fo(B) := Fo(B) Fo(A) For ech production A fi Bb, if FIRST(b), Fo(B) := Fo(B) Fo(A) 42