CMSC 330, Fall 2009, Practice Problem 3 Solutions

Similar documents
CMSC 330 Practice Problem 4 Solutions

Midterm I (Solutions) CS164, Spring 2002

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

CSE431 Translation of Computer Languages

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

COP 3402 Systems Software Syntax Analysis (Parser)

CMSC 330: Organization of Programming Languages

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

AUBER (Models of Computation, Languages and Automata) EXERCISES

Syntax Analysis Check syntax and construct abstract syntax tree

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

CS375: Logic and Theory of Computing

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

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

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

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

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

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

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

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

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

ITEC2620 Introduction to Data Structures

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

JNTUWORLD. Code No: R

ITEC2620 Introduction to Data Structures

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

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

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

Building Compilers with Phoenix

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

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

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

Compiler Construction Using

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

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

Parsing II Top-down parsing. Comp 412

Compiler Construction

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

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

CMSC 330: Organization of Programming Languages

Principles of Programming Languages COMP251: Syntax and Grammars

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

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

Compila(on (Semester A, 2013/14)

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

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

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages

CSE 3302 Programming Languages Lecture 2: Syntax

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

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

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

Introduction to Syntax Analysis

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

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

Context-Free Grammar (CFG)

Principles of Programming Languages COMP251: Syntax and Grammars

QUESTION BANK. Unit 1. Introduction to Finite Automata

CMSC330 Spring 2014 Midterm 2 Solutions

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

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

Introduction to Parsing. Comp 412

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

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.

Chapter 3. Parsing #1

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

Chapter 4: Syntax Analyzer

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

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

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Part 3. Syntax analysis. Syntax analysis 96

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

Optimizing Finite Automata

CS143 Midterm Fall 2008

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

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

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

CSE 401 Midterm Exam Sample Solution 2/11/15

CMSC330 Spring 2017 Midterm 2

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

List of Figures. About the Authors. Acknowledgments

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

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

Homework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half.

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

Lecture Notes on Shift-Reduce Parsing

CS/B.Tech/CSE/IT/EVEN/SEM-4/CS-402/ ItIauIafIaAblll~AladUnrtel1ity

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

CMSC 330: Organization of Programming Languages

Parsing Expression Grammars and Packrat Parsing. Aaron Moss

Ambiguous Grammars and Compactification

Chapter 3. Describing Syntax and Semantics ISBN

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

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Multiple Choice Questions

CMSC330 Fall 2014 Midterm 2 Solutions

Transcription:

CMC 330, Fall 2009, Practice Problem 3 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 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 & eliminating left recursion Rewrite the following grammars so they can be parsed by a predicative parser by eliminating left recursion applying left factoring where necessary a. + a b b L L + a L b. + a + b c c L L + a L + b L c L L + M M a L b L c. a b c a c a L L b c c d. a a a b a a L L a b e. a c a b b a L b L c 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 d. Describe an abstract syntax tree (AT) Compact representations of parse trees with only essential parts 7. Automata a. Compare finite automata pushdown automata Pushdown automata are finite automata that can use 1 stack. Pushdown automata > finite automata in terms of computing power.