CMSC 330 Practice Problem 4 Solutions

Similar documents
CMSC 330, Fall 2009, Practice Problem 3 Solutions

CMSC 330: Organization of Programming Languages

Midterm I (Solutions) CS164, Spring 2002

CSE431 Translation of Computer Languages

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

ITEC2620 Introduction to Data Structures

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

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

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

Roll No. :... Invigilator's Signature :. CS/B.Tech(CSE)/SEM-7/CS-701/ LANGUAGE PROCESSOR. Time Allotted : 3 Hours Full Marks : 70

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

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CMSC 330: Organization of Programming Languages

ITEC2620 Introduction to Data Structures

CSE 3302 Programming Languages Lecture 2: Syntax

AUBER (Models of Computation, Languages and Automata) EXERCISES

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

Parsing Part II (Top-down parsing, left-recursion removal)

COP 3402 Systems Software Syntax Analysis (Parser)

Chapter 4: Syntax Analyzer

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

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

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

CMSC 330: Organization of Programming Languages

Compiler Construction

Introduction to Syntax Analysis

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

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

Syntax Analysis Check syntax and construct abstract syntax tree

Parsing Expression Grammars and Packrat Parsing. Aaron Moss

JNTUWORLD. Code No: R

CS 314 Principles of Programming Languages

CMSC 330: Organization of Programming Languages

Building Compilers with Phoenix

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CMSC330 Spring 2014 Midterm 2 Solutions

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

Context-Free Grammar (CFG)

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Parsing II Top-down parsing. Comp 412

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

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.

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

A programming language requires two major definitions A simple one pass compiler

Principles of Programming Languages COMP251: Syntax and Grammars

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

Introduction to Parsing. Comp 412

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

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

CSE 401 Midterm Exam Sample Solution 2/11/15

CMSC 330: Organization of Programming Languages. Context Free Grammars

CS375: Logic and Theory of Computing

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

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

Introduction to Syntax Analysis. The Second Phase of Front-End

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

CMSC330 Spring 2017 Midterm 2

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

Compilation Lecture 3: Syntax Analysis: Top-Down parsing. Noam Rinetzky

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

Concepts Introduced in Chapter 4

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

Chapter 3. Describing Syntax and Semantics ISBN

CS308 Compiler Principles Syntax Analyzer Li Jiang

Compila(on (Semester A, 2013/14)

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

Ambiguous Grammars and Compactification

Chapter 3. Parsing #1

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

Parsing III. (Top-down parsing: recursive descent & LL(1) )

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

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Part 5 Program Analysis Principles and Techniques

([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.

CS143 Midterm Fall 2008

Compiler Construction Using

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

Principles of Programming Languages COMP251: Syntax and Grammars

CSE 401 Midterm Exam 11/5/10

Midterm Solutions COMS W4115 Programming Languages and Translators Wednesday, March 25, :10-5:25pm, 309 Havemeyer


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 Part I

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

1. [5 points each] True or False. If the question is currently open, write O or Open.

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

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Transcription:

CMC 330 Practice Problem 4 olutions 1. Context Free Grammars a. List the 4 components of a context free grammar. Terminals, non-terminals, productions, start symbol b. Describe the relationship between terminals, non-terminals, productions. Productions are rules for replacing a single non-terminal with a string of terminals non-terminals c. Define ambiguity. Multiple left-most (or right-most) derivations for the same string d. Describe the difference between scanning & parsing. canning matches input to regular expressions to produce terminals, parsing matches terminals to grammars to create parse trees e. Describe an abstract syntax tree (AT) Compact representations of parse trees with only essential parts 2. Describing Grammars a. Describe the language accepted by the following grammar: ab a (ab)*a b. Describe the language accepted by the following grammar: ab ε a n b n, n 0 c. Describe the language accepted by the following grammar: bb A A aa ε b n a * b n, n 0 d. Describe the language accepted by the following grammar: A B A aac Aa ε B bbb ε trings of a & c with same or fewer c s than a s no prefix has more c s than a s, followed by an even number of b s e. Describe the language accepted by the following grammar: or () false Boolean expressions of & false separated by & or, with some expressions enclosed in parentheses f. Which of the previous grammars are left recursive? 2d, 2e g. Which of the previous grammars are right recursive? 2a, 2c, 2d, 2e h. Which of the previous grammars are ambiguous? Provide proof. Examples of multiple left-most derivations for the same string 2d: => A => Aa => a => ab => a => A => => A => Aa => a => ab => a 2e: => => => => => => => => => =>

3. Creating Grammars a. Write a grammar for a x b y, where x = y ab ε b. Write a grammar for a x b y, where x > y al L al alb ε c. Write a grammar for a x b y, where x = 2y aab ε d. Write a grammar for a x b y a z, where z = x+y aa L L bla ε e. Write a grammar for a x b y a z, where z = x-y aa L L alb ε f. Write a grammar for all strings of a b that are palindromes. aa bb L L a b ε g. Write a grammar for all strings of a b that include the substring baa. LbaaL L al bl ε // L = any h. Write a grammar for all strings of a b with an odd number of a s an odd number of b s. EaEbE EbEaE E EaEaE EbEbE ε // E = even #s i. Write a grammar for the if statement in OCaml if E then E else E if E then E E expr j. Write a grammar for all lists in OCaml [] [E] E:: E elem // Ignores types, allows lists of lists k. Which of your grammars are ambiguous? Can you come up with an unambiguous grammar that accepts the same language? Grammar for 3h is ambiguous. An unambiguous grammar must exist since the language can be recognized by a deterministic finite automaton, DFA -> RE -> Regular Grammar. Grammar for 3i is ambiguous. Multiple derivations for if expr then if expr then expr else expr. It is possible to write an unambiguous grammar by restricting some so that no unbalanced if statement can be produced. 4. Derivations, Parse Trees, Precedence Associativity For the following grammar: a. List 4 derivations for the string. i. => => => => => ii. => => => => => iii. => => => => => iv. => => => => => v. => => => => =>

vi. => => => => => vii. => => => => => viii. => => => => => ix. => => => => => x. => => => => => xi. => => => => => xii. => => => => => xiii. => => => => => xiv. => => => => => xv. => => => => => xvi. => => => => => b. Label each derivation as left-most, right-most, or neither. i ii are left-most derivations, iii iv are right-most derivations, remaining derivations are neither c. List the parse tree for each derivation Tree 1 = ii, iii, x, xi, Tree 2 = rest Tree 1 Tree 2 d. What is implied about the associativity of for each parse tree? Tree 1 => is right-associative, Tree 2 => is left-associative For the following grammar: or e. List all parse trees for the string or

Tree 1 Tree 2 or or f. What is implied about the precedence/associativity of or for each parse tree? Tree 1 => or has higher precedence than Tree 2 => has higher precedence than or g. Rewrite the grammar so that has higher precedence than or is right associative or L // op closer to tart = lower precedence op L L // right recursive = right associative 5. Left factoring Rewrite the following grammars so they can be parsed by a predicative parser by applying left factoring where necessary a. a b c a c a L L b c c b. a a a b a a L L a b ε c. a b A c a b B a a b L L A c B a d. a a A a a a B a c a L L a A a a B c a L L a M c M A a B

6. Parsing For the problem, assume the term predictive parser refers to a top-down, recursive descent, non-backtracking predictive parser. a. Consider the following grammar: or () false i. Compute First sets for each production nonterminal First() = { First(false) = { false First( () ) = { ( First( ) = First( or ) = First( ) = { (,, false ii. Explain why the grammar cannot be parsed by a predictive parser First sets of productions intersect, grammar is left recursive b. Consider the following grammar: ab ac c i. Compute First sets for each production nonterminal First(ab) = { a First(ac) = { a First(c) = { c First() = { a, c ii. how why the grammar cannot be parsed by a predictive parser. First sets of productions overlap First(ab) First(ac) = { a { a = { a iii. Rewrite the grammar so it can be parsed by a predictive parser. al c L b c iv. Write a predictive parser for the rewritten grammar. parse_( ) { if (lookahead == a ) { match( a ); // al else if (lookahead == c ) match( c ); // c else error( ); parse_l( ) { if (lookahead == b ) { match( b ); // L b parse_( ); else if (lookahead == c ) { match( c ); // L c parse_( ); else error( );

c. Consider the following grammar: a c c i. how why the grammar cannot be parsed by a predictive parser. First sets of productions intersect, grammar is left recursive ii. Rewrite the grammar so it can be parsed by a predictive parser. c L L al cl ε iii. Write a recursive descent parser for your new grammar parse_( ) { if (lookahead == c ) { match( c ); // cl else error( ); parse_l( ) { if (lookahead == a ) { match( a ); // L al else if (lookahead == c ) { match( c ); // L cl else ; // L ε