CSCE 531 Compiler Construction Lecture 10 z Test

  • Slides: 41
Download presentation
CSCE 531 Compiler Construction Lecture 10 z Test 1 Review Topics n Nullable, First,

CSCE 531 Compiler Construction Lecture 10 z Test 1 Review Topics n Nullable, First, Follow n LL (1) Table construction Bottom-up parsing handles n n Readings: February 25, 2018

Overview Last Time n Regular Expressions: L(R) the Language Denoted by R n NFAs

Overview Last Time n Regular Expressions: L(R) the Language Denoted by R n NFAs (Thompson Construction), DFA, minimization? Flex Grammars Recursive descent parsers (with bracktracking) First and Follow LL(1) property Predictive Parsing Tables LR(0) (SLR) sets of items LR(0) (SLR) parse table LR parsing algorithm No Bison !!! No LR(1) sets of items or LR(1) parse table n n n n n References: Chapters 1 -4. 7? , mainly sections 3. 1 -4. – 2– CSCE 531 Spring 2018

Lecture Slides https: //cse. sc. edu/~matthews/Courses/531/Lectures. html 1. Overview 2. Simple Translator 3. Flex,

Lecture Slides https: //cse. sc. edu/~matthews/Courses/531/Lectures. html 1. Overview 2. Simple Translator 3. Flex, RE NFA 4. Subset Construction 5. Grammars 6. Grammar Modifications 7. Predictive Parsing 8. Bottom. Up Parsing 9. SLR Parse Tables 10. YACC BISON introduction – 3– CSCE 531 Spring 2018

Homework 1. HW 1 – 4– CSCE 531 Spring 2018

Homework 1. HW 1 – 4– CSCE 531 Spring 2018

Old Test Overview 1. Regular expressions 2. Reg. Expr NFA (Thompson construction) 3. NFA

Old Test Overview 1. Regular expressions 2. Reg. Expr NFA (Thompson construction) 3. NFA DFA (subset construction) 4. Grammars 5. First and Follow 6. Left Recursion elimination; recursive descent parsing 7. Predictive parsing table 8. LR(0) sets of items 9. SLR parse table 10. LR parsing algorithm (no LR(1) sets of items) 11. … FLEX – 5– CSCE 531 Spring 2018

1. Regular Expressions a. Give LEX regular expressions for: n Strings of zeroes and

1. Regular Expressions a. Give LEX regular expressions for: n Strings of zeroes and ones such that every 1 is followed (immediately) by at least two zeroes. b. – 6– CSCE 531 Spring 2018

1 b Floating point constants that have any number of digits followed by a

1 b Floating point constants that have any number of digits followed by a decimal point and then at least one digit after the decimal. There should be an optional sign preceding the number and an optional exponent specification following where the exponent specification is the letter ‘E’ (or ‘e’) followed by an optional sign and then exactly two digits. – 7– CSCE 531 Spring 2018

2. Reg. Expr NFA n – 8– Convert the regular expression (ab | (ba)*)*

2. Reg. Expr NFA n – 8– Convert the regular expression (ab | (ba)*)* to an NFA using the Thompson construction. CSCE 531 Spring 2018

2 b NFA for reven n Suppose r is a regular expression for which

2 b NFA for reven n Suppose r is a regular expression for which the NFA Mr accepts the language represented by r, i. e. , L(r) = L(Mr) then show and describe how an NFA could be constructed that will accept the language represented by reven which includes strings that match an even power of r. – 9– CSCE 531 Spring 2018

3. Convert the NFA shown below to a DFA. – 10 – CSCE 531

3. Convert the NFA shown below to a DFA. – 10 – CSCE 531 Spring 2018

Grammars, ambiguity, derivations and languages Given the grammar below S 0 S 1 S

Grammars, ambiguity, derivations and languages Given the grammar below S 0 S 1 S | ε n n n Give a rightmost derivation of 001011 Draw the corresponding parse tree How would one prove l that an ambiguous grammar is ambiguous l that an unambiguous grammar is unambiguous? n – 11 – What does it mean to say a language is inherently ambiguous? CSCE 531 Spring 2018

– 12 – CSCE 531 Spring 2018

– 12 – CSCE 531 Spring 2018

5. First and Follow Calculations n Calculate First and Nullable for the Grammar S

5. First and Follow Calculations n Calculate First and Nullable for the Grammar S c B D d B B b | a D E F E x | ε F y | ε – 13 – CSCE 531 Spring 2018

In the Calculation of FOLLOW assume that the value of the First sets and

In the Calculation of FOLLOW assume that the value of the First sets and Nullable are given below: Nonterminal X First(X) Is X nullable? Follow(X) at this point A { a, b} No {x, eof} B {x, y} No Φ C {z} Yes Φ D {w, q} Yes Φ F {r} Yes Φ Show the changes to the follow sets by examining the production “A BCDF. ” Just once through the main loop of the algorithm from here not iteratively – 14 –processing this production over and over. CSCE 531 Spring 2018

6. Grammar modifications For the grammar L L a | L b | c

6. Grammar modifications For the grammar L L a | L b | c n What is L(G)? n Rewrite the grammar to eliminate the simple left recursion Write recursive parsing routines for the grammar obtained in part b. Rewrite the grammar “S a S | a b T | a c d, T t T | t” to “left factor” it n n – 15 – CSCE 531 Spring 2018

7. Assuming the following Nonterminal X First(X) Is X nullable? Follow(X) at this point

7. Assuming the following Nonterminal X First(X) Is X nullable? Follow(X) at this point A { a, b} No {x, eof} B {x, y} No Φ C {z} Yes {x, y, q} D {w, q} Yes Φ F {r} Yes Φ Show as much of the LL(1) table as is possible (column and row labels). Now consider the production “A B C D F” and insert it into the table in the appropriate places. – 16 – CSCE 531 Spring 2018

– 17 – CSCE 531 Spring 2018

– 17 – CSCE 531 Spring 2018

a) b) c) – 18 – Now consider the production “A B C D

a) b) c) – 18 – Now consider the production “A B C D F” and insert it into the table in the appropriate places Now consider the production “C ε” and insert it into the appropriate places How do you tell if a grammar is LL(1)? CSCE 531 Spring 2018

8. LR(0) sets of items … – 19 – CSCE 531 Spring 2018

8. LR(0) sets of items … – 19 – CSCE 531 Spring 2018

9. SLR parse table … – 20 – CSCE 531 Spring 2018

9. SLR parse table … – 20 – CSCE 531 Spring 2018

LR(1) sets of items … Not on Test 1! – 21 – CSCE 531

LR(1) sets of items … Not on Test 1! – 21 – CSCE 531 Spring 2018

HW-1 Grammars, parse trees 2. 2. 1 2. 2. 2 a, e – 22

HW-1 Grammars, parse trees 2. 2. 1 2. 2. 2 a, e – 22 – CSCE 531 Spring 2018

HW-2 HW Set 2 Due Sunday Jan 28 (no dropbox set up so open

HW-2 HW Set 2 Due Sunday Jan 28 (no dropbox set up so open till Tuesday night!) 3. 3. 2 a, c 3. 3. 5 a 3. 3. 6 c 3. 3. 7 – 23 – CSCE 531 Spring 2018

– 24 – CSCE 531 Spring 2018

– 24 – CSCE 531 Spring 2018

– 25 – CSCE 531 Spring 2018

– 25 – CSCE 531 Spring 2018

– 26 – CSCE 531 Spring 2018

– 26 – CSCE 531 Spring 2018

HW-3 531 HW Due Tuesday night (enjoy the Super Bowl) Problem 1 _ using

HW-3 531 HW Due Tuesday night (enjoy the Super Bowl) Problem 1 _ using the Thompson construction build an NFA that recognizes the Language denoted by the regular expressions: a) (a | ab)*c b) a(b|c)*(aa)* c) if R = L(r) (the language denoted by r) then design a machine for r+ d) design a machine to recognize r^{odd} 3. 5. 3 3. 6. 3 – 27 – 3. 7. 1 C convert NFA in Figure 3. 30 to DFA CSCE 531 Spring 2018

– 28 – CSCE 531 Spring 2018

– 28 – CSCE 531 Spring 2018

– 29 – CSCE 531 Spring 2018

– 29 – CSCE 531 Spring 2018

3. 7. 1 c convert to DFA – 30 – CSCE 531 Spring 2018

3. 7. 1 c convert to DFA – 30 – CSCE 531 Spring 2018

HW 4 531 HW first part of Chap 04 1) 4. 2. 2 b

HW 4 531 HW first part of Chap 04 1) 4. 2. 2 b 2) 4. 2. 2 d (Note changing + to | makes this a grammar for regular expressions) 3) 4. 2. 3 a, b 4) 4. 3. 2 a – 31 – CSCE 531 Spring 2018

– 32 – CSCE 531 Spring 2018

– 32 – CSCE 531 Spring 2018

– 33 – CSCE 531 Spring 2018

– 33 – CSCE 531 Spring 2018

– 34 – CSCE 531 Spring 2018

– 34 – CSCE 531 Spring 2018

– 35 – CSCE 531 Spring 2018

– 35 – CSCE 531 Spring 2018

HW 5 : 4. 3. 1(a)-(d), 4. 4. 1(a)-(b), 4. 4. 3. – 36

HW 5 : 4. 3. 1(a)-(d), 4. 4. 1(a)-(b), 4. 4. 3. – 36 – CSCE 531 Spring 2018

– 37 – CSCE 531 Spring 2018

– 37 – CSCE 531 Spring 2018

– 38 – CSCE 531 Spring 2018

– 38 – CSCE 531 Spring 2018

– 39 – CSCE 531 Spring 2018

– 39 – CSCE 531 Spring 2018

– 40 – CSCE 531 Spring 2018

– 40 – CSCE 531 Spring 2018

– 41 – CSCE 531 Spring 2018

– 41 – CSCE 531 Spring 2018