Chapter 3 Parsing. time, arrow, banana, flies, like, a, an, the, fruit

Similar documents
Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Conflicts in LR Parsing and More LR Parsing Types

Homework 3 Dragon: 4.6.2, 4.6.3, 4.6.5, 4.6.6, , 4.7.4, , 4.8.2

shift-reduce parsing

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

CS 164 Programming Languages and Compilers Handout 8. Midterm I

Midterm I - Solution CS164, Spring 2014

Lecture 7: Deterministic Bottom-Up Parsing

A simple syntax-directed

Lecture 8: Deterministic Bottom-Up Parsing

CS 314 Principles of Programming Languages

Syntax Analysis. Chapter 4

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

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

15 212: Principles of Programming. Some Notes on Grammars and Parsing

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Wednesday, August 31, Parsers

CSE 401 Midterm Exam Sample Solution 2/11/15

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Recursive Descent Parsers

CS164 Spring 1997 Midterm 1


3.5 Practical Issues PRACTICAL ISSUES Error Recovery

Compiler Construction Using

UNIT III & IV. Bottom up parsing

Exercise 1: Balanced Parentheses

Bottom-Up Parsing. Lecture 11-12

EECS 6083 Intro to Parsing Context Free Grammars

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Principles of Programming Languages

Grammars and Parsing, second week

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

CS606- compiler instruction Solved MCQS From Midterm Papers

ECE251 Midterm practice questions, Fall 2010

Monday, September 13, Parsers

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Lecture Bottom-Up Parsing

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Bottom-Up Parsing. Lecture 11-12

4. Lexical and Syntax Analysis

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

2068 (I) Attempt all questions.

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

Syntax. 2.1 Terminology

Concepts Introduced in Chapter 4

CS153: Compilers Lecture 4: Recursive Parsing

Chapter 3: Lexing and Parsing

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

4. Lexical and Syntax Analysis

Context-free grammars

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Syntax and Parsing COMS W4115. Prof. Stephen A. Edwards Fall 2003 Columbia University Department of Computer Science

CSE 401 Midterm Exam 11/5/10

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Compiler Construction: Parsing

CSE 401/M501 18au Midterm Exam 11/2/18. Name ID #

LR Parsing LALR Parser Generators

CSE P 501 Exam Sample Solution 12/1/11

Compiler Design Aug 1996

Appendix Set Notation and Concepts

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

Midterm I (Solutions) CS164, Spring 2002

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Using an LALR(1) Parser Generator

In One Slide. Outline. LR Parsing. Table Construction

Lecture Notes on Shift-Reduce Parsing

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

LR Parsing LALR Parser Generators

Part 3. Syntax analysis. Syntax analysis 96

Optimizing Finite Automata

CS 4120 Introduction to Compilers

COMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )

3. Parsing. Oscar Nierstrasz

Compilers. Bottom-up Parsing. (original slides by Sam

Properties of Regular Expressions and Finite Automata

CSE P 501 Exam 12/1/11

COP 3402 Systems Software Syntax Analysis (Parser)

S Y N T A X A N A L Y S I S LR

LALR Parsing. What Yacc and most compilers employ.

Chapter 4. Lexical and Syntax Analysis

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Question Bank. 10CS63:Compiler Design

Transcription:

Chapter 3 Parsing EXERCISES 3.1 Translate each of these regular expressions into a context-free grammar. a. ((xy*x) (yx*y))? b. ((0 1) + "."(0 1)*) ((0 1)*"."(0 1) + ) *3.2 Write a grammar for English sentences using the words time, arrow, banana, flies, like, a, an, the, fruit and the semicolon. Be sure to include all the senses (noun, verb, etc.) of each word. Then show that this grammar is ambiguous by exhibiting more than one parse tree for "time flies like an arrow; fruit flies like a banana." 3.3 Write an unambiguous grammar for each of the following languages. Hint: One way of verifying that a grammar is unambiguous is to run it through Yacc and get no conflicts. a. Palindromes over the alphabet {a, b} (strings that are the same backward and forward). b. Strings that match the regular expression a*b* and have more a's than b's. c. Balanced parentheses and square brackets. Example: ([[](()[()][])]) *d. Balanced parentheses and brackets, where a closing bracket also closes any outstanding open parentheses (up to the previous open bracket). Example: [([](()[(][])]. Hint: First, make the language of balanced parentheses and brackets, where extra open parentheses are allowed; then make sure this nonterminal must appear within brackets. e. All subsets and permutations (without repetition) of the keywords public final static synchronized transient. (Then comment on how best to handle this situation in a real compiler.) f. Statement blocks in Pascal or ML where the semicolons separate the statements: ( statement ; ( statement ; statement ) ; statement ) g. Statement blocks in C where the semicolons terminatethe statements: { expression; { expression; expression; } expression; } 3.4 Write a grammar that accepts the same language as Grammar 3.1, but that is suitable for LL(1) parsing. That is, eliminate the ambiguity, eliminate the left recursion, and (if necessary) left-factor.

============================================= GRAMMAR 3.1: A syntax for straight-line programs. 1. S S; S 2. S id := E 3. S print (L) 4. E id 5. E num 6. E E + E 7. E (S, E) 8. L E 9. L L, E =============================== 3.5 Find nullable, FIRST, and FOLLOW sets for this grammar; then construct the LL(1) parsing table. 0. S S $ 1. S 2. S XS 3. B \ begin { WORD } 4. E \ end { WORD } 5. X BSE 6. X { S } 7. X WORD 8. X begin 9. X end 10. X \ WORD 3.6 a. Calculate nullable, FIRST, and FOLLOW for this grammar: S u B D z B B v B w D E F E y E F x F b. Construct the LL(1) parsing table. c. Give evidence that this grammar is not LL(1). d. Modify the grammar as little as possible to make an LL(1) grammar that accepts the same language.

*3.7 a. Left-factor this grammar. 0. S G $ 1. G P 2. G PG 3. P id : R 4. R 5. R id R b. Show that the resulting grammar is LL(2). You can do this by constructing FIRST sets (etc.) containing two-symbol strings; but it is simpler to construct an LL(1) parsing table and then argue convincingly that any conflicts can be resolved by looking ahead one more symbol. c. Show how the tok variable and advance function should be altered for recursive-descent parsing with two-symbol lookahead. d. Use the grammar class hierarchy (Figure 3.29) to show that the (left factored) grammar is LR(2). e. Prove that no string has two parse trees according to this (left-factored) grammar. Figure 3.29 A hierarchy of Grammar classes 3.8 Make up a tiny grammar containing left recursion, and use it to demonstrate that left recursion is not a problem for LR parsing. Then show a small example comparing growth of the LR parse stack with left recursion versus right recursion.

3.9 Diagram the LR(0) states for Grammar 3.26, build the SLR parsing table, and identify the conflicts. ================================================================== GRAMMAR 3.26: A grammar capturing the essence of expressions, variables, and pointer-dereference (by the *) operator in the C language. 1. S S $ 2. S V = E 3. S E 4. E V 5. V x 6. V * E ================================================================== 3.10 Diagram the LR(1) states for the grammar of Exercise 3.7 (without left-factoring), and construct the LR(1) parsing table. Indicate clearly any conflicts. 3.11 Construct the LR(0) states for this grammar, and then determine whether it is an SLR grammar. 6. S B $ 7. B id P 8. B id *(E ] 9. P 10. P (E) 11. E B 12. E B, E 3.12 a. Build the LR(0) DFA for this grammar: 0. S E $ 1. E id 2. E id (E) 3. E E + id b. Is this an LR(0) grammar? Give evidence. c. Is this an SLR grammar? Give evidence. d. Is this an LR(1) grammar? Give evidence.

3.13 Show that this grammar is LALR(1) but not SLR: 13. S X $ 14. X Ma 15. X bmc 16. X dc 17. X bda 18. M d 3.14 Show that this grammar is LL(1) but not LALR(1): 19. S (X 20. S E] 21. S F) 22. X E) 23. X F] 24. E A 25. F A 26. A *3.15 Feed this grammar to Yacc; from the output description file, construct the LALR(1) parsing table for this grammar, with duplicate entries where there are conflicts. For each conflict, show whether shifting or reducing should be chosen so that the different kinds of expressions have "conventional" precedence. Then show the Yacc-style precedence directives that resolve the conflicts this way. 27. S E $ 28. E while E do E 29. E id := E 30. E E + E 31. E id *3.16 Explain how to resolve the conflicts in this grammar, using precedence directives, or grammar transformations, or both. Use Yacc, Java Cup or SableCC as a tool in your investigations, if you like. 32. E id 33. E EBE 34. B + 35. B 36. B 37. B /

=================================== GRAMMAR 3.8 E E + T E E T E T T T * F T T / F T F F id F num F (E) ================================== *3.17 Prove that Grammar 3.8 cannot generate parse trees of the form shown in Figure 3.9. Hint: What nonterminals could possibly be where the?x is shown? What does that tell us about what could be where the?y is shown? Figure 3.9: Parse trees that Grammar 3.8 will never produce.