CS 314 Principles of Programming Languages

Similar documents
Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

CS 314 Principles of Programming Languages

FABER Formal Languages, Automata and Models of Computation

CS 275 Automata and Formal Language Theory

CS 275 Automata and Formal Language Theory

CS 275 Automata and Formal Language Theory

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

The transformation to right derivation is called the canonical reduction sequence. Bottom-up analysis

Closure Properties of Regular Languages

Normal Forms for Context-free Grammars

a b b a pop push read unread

Formal languages, automata, and theory of computation

Convert the NFA into DFA

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Nondeterminism and Nodeterministic Automata

For convenience, we rewrite m2 s m2 = m m m ; where m is repeted m times. Since xyz = m m m nd jxyj»m, we hve tht the string y is substring of the fir

First Midterm Examination

1.4 Nonregular Languages

Formal Languages Simplifications of CFGs

Review for the Midterm

Parsing and Pattern Recognition

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CS 330 Formal Methods and Models Dana Richards, George Mason University, Spring 2016 Quiz Solutions

Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem 2/16/15

First Midterm Examination

Harvard University Computer Science 121 Midterm October 23, 2012

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

5.1 Definitions and Examples 5.2 Deterministic Pushdown Automata

More on automata. Michael George. March 24 April 7, 2014

Lexical Analysis Part III

General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

Lexical Analysis Finite Automate

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

CSC 473 Automata, Grammars & Languages 11/9/10

Chapter 4 Regular Grammar and Regular Sets. (Solutions / Hints)

Exercises Chapter 1. Exercise 1.1. Let Σ be an alphabet. Prove wv = w + v for all strings w and v.

CS 311 Homework 3 due 16:30, Thursday, 14 th October 2010

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Lecture 08: Feb. 08, 2019

Lecture 09: Myhill-Nerode Theorem

Semantic Analysis. CSCI 3136 Principles of Programming Languages. Faculty of Computer Science Dalhousie University. Winter Reading: Chapter 4

SEMANTIC ANALYSIS PRINCIPLES OF PROGRAMMING LANGUAGES. Norbert Zeh Winter Dalhousie University 1/28

Context-Free Grammars and Languages

Minimal DFA. minimal DFA for L starting from any other

CMSC 330: Organization of Programming Languages

Let's start with an example:

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

Parse trees, ambiguity, and Chomsky normal form

1 Nondeterministic Finite Automata

1 From NFA to regular expression

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

Part 5 out of 5. Automata & languages. A primer on the Theory of Computation. Last week was all about. a superset of Regular Languages

Lecture 6 Regular Grammars

I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines

q 2 in δ 2. [4: 1 mark q 1 in δ 1 and q 2 and (q 1, q 2 ) (q 1, q 2) whenever q 1 for each component] (b) The resulting DFA:

Designing finite automata II

Chapter 2 Finite Automata

Homework Solution - Set 5 Due: Friday 10/03/08

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

a,b a 1 a 2 a 3 a,b 1 a,b a,b 2 3 a,b a,b a 2 a,b CS Determinisitic Finite Automata 1

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

CHAPTER 1 Regular Languages. Contents

Lecture 9: LTL and Büchi Automata

1.3 Regular Expressions

Non Deterministic Automata. Formal Languages and Automata - Yonsei CS 1

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

Tutorial Automata and formal Languages

Formal Languages and Automata

Non-Deterministic Finite Automata

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

This lecture covers Chapter 8 of HMU: Properties of CFLs

Thoery of Automata CS402

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

Lecture 3: Equivalence Relations

Formal Languages and Automata Theory. D. Goswami and K. V. Krishna

CS 330 Formal Methods and Models

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

CS 330 Formal Methods and Models

CS103 Handout 32 Fall 2016 November 11, 2016 Problem Set 7

Fundamentals of Computer Science

Non-deterministic Finite Automata

PART 2. REGULAR LANGUAGES, GRAMMARS AND AUTOMATA

ɛ-closure, Kleene s Theorem,

Chapter 1, Part 1. Regular Languages. CSC527, Chapter 1, Part 1 c 2012 Mitsunori Ogihara 1

Informatics 2A: Lecture 20. Shay Cohen. 31 October 2017

CSE : Exam 3-ANSWERS, Spring 2011 Time: 50 minutes

Formal Language and Automata Theory (CS21004)

Finite Automata-cont d

CS 267: Automated Verification. Lecture 8: Automata Theoretic Model Checking. Instructor: Tevfik Bultan

2. Lexical Analysis. Oscar Nierstrasz

CISC 4090 Theory of Computation

Transcription:

C 314 Principles of Progrmming Lnguges Lecture 6: LL(1) Prsing Zheng (Eddy) Zhng Rutgers University Ferury 5, 2018

Clss Informtion Homework 2 due tomorrow. Homework 3 will e posted erly next week. 2

Top - Down Prsing - LL(1) <> <A> α β Bsic Ide: x The prse tree is constructed from the root, expnding non-terminl nodes on the tree s frontier following leftmost derivtion. The input progrm is red from left to right, nd input tokens re red (consumed) s the progrm is prsed. The next non-terminl symol is replced using one of its rules. The prticulr choice hs to e unique nd uses prts of the input (prtilly prsed progrm), for instnce the first token of the remining input. y 3

Top - Down Prsing - LL(1) (cont.) Exmple: ::= ε How cn we prse (utomticlly construct leftmost derivtion) the input string using PDA (push-down utomton) nd only the first symol of the remining input? INPUT: eof 4

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: 5

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: ::= 6

LL(1) Prsing Exmple ::= ε Remining Input: Mtch! ententil Form: 7

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: 8

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: ::= 9

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: Mtch! 10

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: 11

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: ::= 12

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: Mtch! 13

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: 14

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: ::= ε ε 15

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: Mtch! ε 16

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: ε 17

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: Mtch! ε 18

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: ε 19

LL(1) Prsing Exmple ::= ε Remining Input: Mtch! ententil Form: ε 20

LL(1) Prsing Exmple ::= ε Remining Input: ententil Form: ε 21

Another LL(1) Prsing Exmple Consider this exmple grmmr: id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; How to prse the following input string? A, B, C; 22

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: A, B, C; id_list ententil Form: id_list 23

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: A, B, C; id_list id(a) id_list_til ententil Form: id(a) id_list_til id_list ::= id id_list_til 24

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: A, B, C ; id_list id(a) Mtch! id_list_til ententil Form: id(a) id_list_til 25

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id_list id(a) id_list_til ententil Form: id(a) id_list_til 26

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b) id_list_til id_list_til ::=, id id_list_til 27

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id_list id(a) id_list_til, id(b) id_list_til Mtch! ententil Form: id(a), id(b) id_list_til 28

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: B, C ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b) id_list_til 29

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: B, C ; id(a) id_list id_list_til, id(b) id_list_til Mtch! ententil Form: id(a), id(b) id_list_til 30

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, C ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b) id_list_til 31

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, C ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b), id(c) id_list_til, id(c) id_list_til id_list_til ::=, id id_list_til 32

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, C ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b), id(c) id_list_til, id(c) id_list_til Mtch! 33

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: C ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b), id(c) id_list_til, id(c) id_list_til 34

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: C ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b), id(c) id_list_til, id(c) id_list_til Mtch! 35

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b), id(c) id_list_til, id(c) id_list_til 36

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b), id(c) ;, id(c) id_list_til id_list_til ::= ; ; 37

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: ; id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b), id(c) ;, id(c) id_list_til ; Mtch! 38

Another LL(1) Prsing Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input: id(a) id_list id_list_til, id(b) id_list_til ententil Form: id(a), id(b), id(c) ;, id(c) id_list_til ; 39

Predictive Prsing Bsic ide: For ny two productions A ::= α β, we would like distinct wy of choosing the correct production to expnd. For some rhs α G, define FIRT(α) s the set of tokens tht pper s the first symol in some string derived from α. Tht is x FIRT(α) iff α xγ for some γ, nd 40

Revisiting the id_list Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id_list id(a) id_list_til 41

Revisiting the id_list Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id_list id(a) id_list_til, id(b) id_list_til id_list_til ::=, id id_list_til 42

Revisiting the id_list Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id_list id(a) id_list_til, id(b) id_list_til Mtch! id_list_til ::=, id id_list_til 43

Revisiting the id_list Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id_list id(a) id_list_til 44

Revisiting the id_list Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id_list id(a) id_list_til ; Mismtch! id_list_til ::= ; 45

Revisiting the id_list Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id_list id(a) id_list_til FIRT(, id id_list_til ) = {, } FIRT( ; ) = { ; } Given id_list_til s the first non-terminl to expnd in the tree: If the first token of remining input is, we choose the rue id_list_til ::=, id id_list_til If the first token of remining input is ; we choose the rule id_list_til ::= ; 46

Predictive Prsing Key Property: Whenever two productions A ::= α nd A ::= β oth pper in the grmmr, we would like FIRT (α) FIRT (β) =, nd if α * ε, then FIRT (β) FOLLOW (A) = Anlogue cse for β * ε. Note: due to first condition, t most one of α nd β cn derive ε. 47

Revisiting the id_list Exmple id_list ::= id id_list_til id_list_til ::=, id id_list_til id_list_til ::= ; Remining Input:, B, C ; id_list id(a) id_list_til FIRT(, id id_list_til ) = {, } FIRT( ; ) = { ; } FIRT (, id id_list_til FIRT ( ; ) = Given id_list_til s the first non-terminl to expnd in the tree: If the first token of remining input is, we choose the rue id_list_til ::=, id id_list_til If the first token of remining input is ; we choose the rule id_list_til ::= ; 48

Predictive Prsing Key Property: Whenever two productions A ::= α nd A ::= β oth pper in the grmmr, we would like FIRT (α) FIRT (β) =, nd if α * ε, then FIRT (β) FOLLOW (A) = Anlogue cse for β * ε. Note: due to first condition, t most This one rule of is α intuitive. nd β cn However, derive ε. it is not correct, ecuse it doesn t hndle ε rules. How to hndle ε rules? 49

Revisiting the LL(1) Prsing Exmple ::= ε Remining Input: 50

Revisiting the LL(1) Prsing Exmple ::= ε Remining Input: ::= Mismtch! It only mens ::= is not the right production rule to use! 51

Revisiting the LL(1) Prsing Exmple ::= ε Remining Input: 52

Revisiting the LL(1) Prsing Exmple ::= ε Remining Input: ::= ε ε ::= ε turns out to e the right rule lter. However, t this point: ε does not mtch either! 53

Predictive Prsing For non-terminl A, define FOLLOW(A) s the set of terminls tht cn pper immeditely to the right of A in some sententil form. Thus, non-terminl s FOLLOW set specifies the tokens tht cn leglly pper fter it. A terminl symol hs no FOLLOW set. FIRT nd FOLLOW sets cn e constructed utomticlly 54

Predictive Prsing Key Property: Whenever two productions A ::= α nd A ::= β oth pper in the grmmr, we would like FIRT (α) FIRT (β) =, nd if α * ε, then FIRT (β) FOLLOW (A) = Anlogue cse for β * ε. Note: due to first condition, t most one of α nd β cn derive ε. This would llow the prser to mke correct choice with lookhed of only one symol! 55

Predictive Prsing Key Property: Whenever two productions A ::= α nd A ::= β oth pper in the grmmr, we would like FIRT (α) FIRT (β) =, nd if α * ε, then FIRT (β) FOLLOW (A) = Anlogue cse for β * ε. Note: due to first condition, t most one of α nd β cn derive ε. This would llow the prser to mke correct choice with lookhed of only one symol! 56

LL(1) Grmmr Define FIRT+(A ::= δ) for rule A ::= δ FIRT (δ) - { ε } U Follow (A), if ε FIRT(δ) FIRT (δ) otherwise A Grmmr is LL(1) iff (A ::= α nd A ::= β) implies FIRT + ( A ::= α) FIRT + ( A ::= β) = 57

Bck to Our Exmple trt ::= eof ::= ε FIRT() = {} FIRT(ε) = {ε} Is the grmmr LL(1)? FOLLOW() = {eof, } FIRT + ( ::= ) = {} FIRT + ( ::= ε) = ( FIRT(ε) - {ε} ) FOLLOW() ={eof, } Define FIRT+(δ) for rule A ::= δ FIRT (δ) - { ε } U Follow (A), if ε FIRT(δ) FIRT (δ) otherwise 58

Tle Driven LL(1) Prsing Exmple: ::= ε LL(1) prse tle eof other ε ε error How to prse input? 59

Tle Driven LL(1) Prsing Input: string w nd prsing tle M for G push eof push trt ymol token next_token() X top-of-stck repet if X is terminl then if X == token then pop X token next_token() else error() else /* X is non-terminl */ if M[X, token] == X Y1Y2... Yk then pop X push Yk, Yk 1,..., Y1 else error() X top-of-stck until X = eof if token!= eof then error() 60

Predictive Prsing Now, predictive prser looks like: stck source code scnner prser intermedite representtion prsing tles Rther thn writing code, we uild tles. Building tles cn e utomted! 61

Predictive Prsing Now, predictive prser looks like: stck source code scnner prser intermedite representtion grmmr prser genertor prsing tles Rther thn writing code, we uild tles. Building tles cn e utomted! 62

Recursive Descent Prsing Now, we cn produce recursive descent prser for our LL(1) grmmr. Recursive descent is one of the simplest prsing techniques used in prcticl compilers: Ech non-terminl hs n ssocited prsing procedure tht cn recognize ny sequence of tokens generted y tht non-terminl There is min routine to initilize ll glols (e.g: tokens) nd cll the strt symol. On return, check whether token==eof, nd whether errors occurred Within prsing procedure, oth non-terminls nd terminls cn e mtched: non-terminl A: cll procedure for A token t: compre t with current input token; if mtched, consume input, otherwise, ERROR Prsing procedure my contin code tht performs some useful computtions (syntx directed trnsltion) 63

Recursive Descent Prsing (pseudo code) eof other ε ε error min: { token := next_token( ); if (( ) nd token == eof) print ccept else print error ; } 64

Recursive Descent Prsing (pseudo code) eof other ε ε error ool : { switch token { cse : token := next_token( ); cll ( ); if (token == ) { token := next_token( ); return true; } else return flse; rek; cse : cse eof: return true; rek; defult: return flse; } } 65

Next Lecture Next Time: Review of LL(1) prsing nd syntx directed trnsltion Red cott, Chpter 2.3.1-2.3.2 66