a. Determine the EPS, FIRST and FOLLOWs set for this grammar.

Similar documents
Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Configuration Sets for CSX- Lite. Parser Action Table

QUESTIONS RELATED TO UNIT I, II And III

Defining syntax using CFGs

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

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

Principles of Programming Languages COMP251: Syntax and Grammars

CMPT 755 Compilers. Anoop Sarkar.

Lexical and Syntax Analysis (2)

Compilers. Compiler Construction Tutorial The Front-end

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

Exercise 1: Balanced Parentheses

Context-free grammars

ITEC2620 Introduction to Data Structures

Syntax Analysis, III Comp 412

UNIVERSITY OF CALIFORNIA

Compiler Construction: Parsing

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

ITEC2620 Introduction to Data Structures

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

Syntax Analysis, III Comp 412

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

Computing PREDICT and FOLLOW sets

CS 44 Exam #2 February 14, 2001

CS502: Compilers & Programming Systems

Syntax Analysis Check syntax and construct abstract syntax tree

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

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

Fall Compiler Principles Lecture 3: Parsing part 2. Roman Manevich Ben-Gurion University

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

2068 (I) Attempt all questions.

Conflicts in LR Parsing and More LR Parsing Types

CS143 Handout 20 Summer 2012 July 18 th, 2012 Practice CS143 Midterm Exam. (signed)

Introduction to Bottom-Up Parsing

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Principles of Programming Languages COMP251: Syntax and Grammars

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

LECTURE 7. Lex and Intro to Parsing

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

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

Part 3. Syntax analysis. Syntax analysis 96

LL parsing Nullable, FIRST, and FOLLOW

Project 2 Interpreter for Snail. 2 The Snail Programming Language

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

Parsing II Top-down parsing. Comp 412

Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%

CMPT 379 Compilers. Parse trees

CS 536 Midterm Exam Spring 2013

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

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

BSCS Fall Mid Term Examination December 2012

CMSC 330, Fall 2009, Practice Problem 3 Solutions

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

CSCI Compiler Design

LL(1) predictive parsing

ASTs, Objective CAML, and Ocamlyacc

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

LALR Parsing. What Yacc and most compilers employ.


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

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

CMSC 330 Practice Problem 4 Solutions

Compilers. Predictive Parsing. Alex Aiken

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

CS308 Compiler Principles Syntax Analyzer Li Jiang

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Lexical and Syntax Analysis. Top-Down Parsing

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

CS143 Midterm Fall 2008

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

Context-free grammars (CFG s)

CS 406/534 Compiler Construction Parsing Part I

CS 164 Programming Languages and Compilers Handout 8. Midterm I

UNIT III & IV. Bottom up parsing

Compiler Construction Assignment 3 Spring 2018

CSCE 531 Spring 2009 Final Exam

DVA337 HT17 - LECTURE 4. Languages and regular expressions

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Compiler Construction

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Introduction to Parsing

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

Compiler Construction

Topic 3: Syntax Analysis I

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

LR Parsing Techniques

Question Bank. 10CS63:Compiler Design

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

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

Revisit the example. Transformed DFA 10/1/16 A B C D E. Start

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

CSE302: Compiler Design

Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

Lexical and Syntax Analysis

Transcription:

Concepts of Programming Languages, CSCI 305, Fall 2017 Exercise 11, LL Grammars and Predict Sets, Oct. 30 1. The following grammar defines a subset of Lisp: P E $$ E atom E E // Tick ( ) is a token E ( E Es ) Es E Es Es ε a. Determine the EPS, FIRST and FOLLOWs set for this grammar. EPS FIRST set FOLLLOW - set P False atom,, ( E False atom,, ( $$, atom,, (, ) Es True atom,, ( ) $$ False $$ atom False atom $$, atom,, (, ) False atom,, ( ( False ( atom,, ( ) false ) $$, atom,, (, ) b. Add the predict sets to the grammar above. 1. P E $$ {atom,, (} 2. E atom {atom} 3. E E { } 4. E ( E Es ) {(} 5. Es E Es {atom,, (} 6. Es ε {)} c. Create a parser table atom ( ) $ P 1 1 1 - - E 2 3 4 - - Es 5 5 5 6-1

2. Exercise 2.11 from the text. Prove that the following grammar is LL(1): decl ID decl_tail decl_tail, decl decl_tail, : ID ; LL(1) means that only 1 symbol (token) is needed to decide which production to apply. That is, no peeking forward is necessary. EPS FIRST set FOLLLOW - set decl False ID decl_tail False, and : ID False ID, False, ; False : Since EPS set is empty, the FOLLOW sets aren t needed. 1. decl ID decl_tail { ID } 2. decl_tail, decl {, } 3. decl_tail, : ID ; { : } ID, : ; decl 1 - - - decl_tail - 2 3 - - There is no ambiguity in the parse table so the grammar is LL(1). Another way to argue this is that since the right side of every production ends with a token, and these aren t duplicated for any variable on the left side of the production, there is no ambiguity of which production to use so the grammar is LL(1). 2

3. Do exercise 2.12 from the text. Consider the following grammar: G S $$ S A M M S ε A a E b A A E a B b A ε B b E a B B a. Describe in English the language that the grammar generates. The grammar generates all strings of a s and b s (terminated by an end marker), in which there are more a s than b s. b. Show a parse tree for the string a b a a. There are two parser trees for the specified string (this means that the grammar is ambiguous). c. Is the grammar LL(1)? If so, show the parse table; if not, identify a prediction conflict. Since the grammar is ambiguous, it can not be LL(1). A top-down parser would be unable to decide whether to predict an epsilon production when E is at the top of the stack. 3

To see the above, could create the EPS, FIRST and FOLLOW sets. Non-terminal EPS FIRST FOLLOW and terminals G false {a, b} Φ S false {a, b} {$$} A false {a, b} {a, b, $$} M true {a, b} {$$} E true {a, b} {a, b, $$} B false {a, b} {a, b, $$} $$ false {$$} Φ a false {a} {a, b, $$} b false {b} {a, b, $$} Add the predict sets to the grammar above. 1. G S $$ {a, b} 2. S AM {a, b} 3. M S {a, b} 4. M ε {$$} 5. A ae {a} 6. A baa {b} 7. E ab {a} 8. E ba {b} 9. E ε {a, b, $$} 10. B be {b} 11. B abb {a} Create a parser table a b $ G 1 1 S 2 2 A 5 6 M 3 3 4 E 7,9 8,9 9 B 11 10 Grammar can t be LL(1) because given an E and the token a, rule 7 or 9 can be applied. Similarly, on an E with the token b, rule 8 or 9 can be applied. 4

4. Do exercise 2.13 from the text. Consider the following grammar: stmt assignment stmt subr_call assignment id := expr subr_call id ( arg_list ) expr primary expr_tail expr_tail op expr expr_tail ε primary id primary subr_call primary ( expr ) op + - * / arg_list expr args_tail args_tail, arg_list args_tail ε a. Construct a parse tree for the input string foo(a, b). b. Give a canonical (right-most) derivation of this same string. 5

c. Prove that the grammar is not LL(1). EPS FIRST set FOLLLOW - set stmt false id empty assignment false id empty subr_call false id empty expr false id, (,, ) expr_tail true +, -, *, /,, ) primary false id, ( +, -, *, / op false +, -, *, / id, ( arg_list false id, ( ) args_tail true, ) 1. stmt assignment { id } 2. stmt subr_call { id } 3. assignment id := expr { id } 4. subr_call id ( arg_list ) { id } 5. expr primary expr_tail { id, ( } 6. expr_tail op expr {+, -, *, / } 7. expr_tail ε {,, ) } 8. primary id { id } 9. primary subr_call { id } 10. primary ( expr ) { ( } 11. op + - * / {+, -, *, / } 12. arg_list expr args_tail { id, ( } 13. args_tail, arg_list {, } 14. args_tail ε { ) } Create a parser table id : = ( + - * /, ) stmt 1,2 assignment 3 subr_call 4 expr 5 5 expr_tail - 6 6 6 6 7 7 primary 8, 9 10 op - 11 11 11 11 arg_list 12 12 args_tail - 13 14 Grammar is not LL(1) due to confusion with stmt and id. Also not due to confusion primary and id. 6

d. Modify the grammar so that it is LL(1). stmt assignment stmt subr_call assignment id := expr subr_call id ( arg_list ) expr primary expr_tail expr_tail op expr expr_tail ε primary id primary subr_call primary ( expr ) op + - * / arg_list expr args_tail args_tail, arg_list args_tail ε Factor out id from productions with stmt and expr. stmt id stmt_tail stmt_tail := expr stmt_tail ( arg_list ) expr primary expr_tail expr_tail op expr expr_tail ε primary id pimary_tail primary_tail ε primary_tail ( arg_list ) primary ( expr ) op + - * / arg_list expr args_tail args_tail, arg_list args_tail ε 7