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

Similar documents
Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing Ambiguity and Syntax Errors

Lecture 4: Syntax Specification

CMSC 330: Organization of Programming Languages

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

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

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

CSCI312 Principles of Programming Languages!

QUESTIONS RELATED TO UNIT I, II And III

Introduction to Parsing. Lecture 5

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

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

BSCS Fall Mid Term Examination December 2012

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

CSE 401 Midterm Exam 11/5/10

Compiler Construction

CS164: Midterm I. Fall 2003

LR Parsing LALR Parser Generators

Introduction to Parsing. Lecture 8

Compilers Course Lecture 4: Context Free Grammars

Midterm I (Solutions) CS164, Spring 2002

CS 411 Midterm Feb 2008

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

In One Slide. Outline. LR Parsing. Table Construction

Midterm I - Solution CS164, Spring 2014

Syntax-Directed Translation. Lecture 14

Introduction to Parsing. Lecture 5

Compiler Design Concepts. Syntax Analysis

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

CSE 401 Midterm Exam Sample Solution 2/11/15

Syntax Analysis. Chapter 4

HW 3: Bottom-Up Parsing Techniques

LR Parsing. The first L means the input string is processed from left to right.

Lecture 8: Deterministic Bottom-Up Parsing

CS143 Midterm Fall 2008

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

Chapter 4: Syntax Analyzer

Compiler Construction

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

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

LR Parsing E T + E T 1 T

Context-free grammars

Lecture 7: Deterministic Bottom-Up Parsing

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

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

UVa ID: NAME (print): CS 4501 LDI Midterm 1

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Syntax Analysis Part I

More Bottom-Up Parsing

LR Parsing - The Items

CS502: Compilers & Programming Systems

Chapter 2 :: Programming Language Syntax

LR Parsing LALR Parser Generators

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

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

CS 536 Midterm Exam Spring 2013

Compiler Construction: Parsing

Regular Expressions. Regular Expressions. Regular Languages. Specifying Languages. Regular Expressions. Kleene Star Operation

Optimizing Finite Automata

1. Consider the following program in a PCAT-like language.

Course Project 2 Regular Expressions

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

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Conflicts in LR Parsing and More LR Parsing Types

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

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.

Lexical Analysis. Chapter 2

CSCI Compiler Design

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

Part I: Multiple Choice Questions (40 points)

CMSC 330 Practice Problem 4 Solutions

Error Handling Syntax-Directed Translation Recursive Descent Parsing

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

Chapter 2 - Programming Language Syntax. September 20, 2017

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

Exercise 1: Balanced Parentheses

shift-reduce parsing

Compiler Design Aug 1996

CS 4120 Introduction to Compilers

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

ECE251 Midterm practice questions, Fall 2010

LL(1) predictive parsing

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Context Free Languages

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

Compilers. Predictive Parsing. Alex Aiken

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

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

Context-Free Grammars

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

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

CMSC 330, Fall 2018 Midterm 2

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

Transcription:

COL728 Minor1 Exam Compiler Design Sem II, 2016-17 Answer all 5 questions Max. Marks: 20 1. Short questions a. Show that every regular language is also a context-free language [2] We know that every regular language can be represented by a regular expression which can have terminal symbols and union, concatenation and kleene operators. If we can show that all regular expressions can be written in form of CFG grammer rules which have only a non terminal on LHS, then we can say that every regular language is also a CF Language. So if a regular expression has only a terminal symbol a, then it can be represented by grammar rule S -> a Any concatenation between two regular expressions S1 and S2 (S1.S2) can be represented by grammar rule S -> S1.S2 Any union between two regular expressions S1 and S2 (S1+S2) can be represented by grammar rule S -> S1 S2 And kleene star on a regular expression S (S*) can be represented by grammar rule S -> S.S ε Thus we can represent any regular expression in form of CFG rules. So we can say that every regular language is also a context-free language. [Full marks for translating regular grammar or DFA to Contect free grammar.]

b. Why does the lexer use regular languages but the parser use context-free languages? Explain both reasons briefly and clearly, i.e., (1) why does the lexer use regular languages, and (2) why does the parser use context-free languages. [2] (1) Typically we want to design the language for the tokens/identifiers so that it is easy to identify them. Regular languages is perhaps the simplest class of languages that are easy to identify using a deterministic finite automaton. (2) Certain intuitive programming patterns, like balanced parentheses, are commoly required in programming languages. These constructs are not expressible through regular languages. Hence, typically the language of valid programs is based on a more general class of languages, namely context-free languages. [1 Mark for each reason, reason should be something similar to this. It is important to say that regular languages are the simplest class of languages and hence most programming languages rely on it. It is also important to say that regular languages do not capture certain common and intuitive programming patterns.]

2. Consider the following grammar: E if E then E E if E then E else E E id a. Show that this grammar is ambiguous [2] Suppose we have a string if id then if id then id else id and we want to make its left most derivation tree. Then there are at least two LMD trees for it as shown below: E -> if E then E E -> if E then E else E if E then E if id then E if id then if E then E else E if id then if id then E else E if id then if id then id else E if id then if id then id else id Another way E -> if E then E else E E -> if E then E if E then E else E if id then E else E if id then if E then E else E if id then if id then E else E if id then if id then id else E if id then if id then id else id Hence there are multiple LMDs for the same string in this grammar, so this grammar is ambiguous. In other words, the grammar is ambiguous because it has two parse trees for some string. [full marks for correct example; 1 mark if only meaning of ambiguity is well understood] b. Write an alternative grammar that accepts the same language but is not ambiguous. Clearly state the rule that you used to resolve the ambiguity (any rule that resolves ambiguity is okay). [3]

Unambiguous grammar accepting the same language is written below. Ambiguity in above grammar was because else could be assigned to any if instead of the nearest unmatched if that came before it. So we created two different non-terminals: one representing the set of balanced strings and another representing the set of unbalanced strings. A string is balanced if every if-then is also matched with an else. A string is unbalanced if some if-then is not matched with an else. An else can appear only if the string inside the then clause is balanced (because otherwise the else should have matched the unblanced clause). E -> E_balanced E_unbalanced E_balanced -> if E then E_balanced else E_balanced id E_unbalanced -> if E then E_balanced else E_unbalanced if E then E [If any grammar is stated that removes ambiguity of to which if, an else belongs, and covers all strings then it gets full marks; missing some strings looses 1 mark, still ambigous looses 2 marks] 3. Consider the following grammar: E id E (E) E E.E

Run the recursive descent parsing algorithm (with backtracking) on the following input: (id)((id)) Clearly and concisely show the steps and the final result. No marks for partial or incorrect execution [4] To start with, we should add a new start symbol which includes the $ character to signify that it matches only if the end of the input has been reached: S E$ Can draw a recursion tree as follows (shown without S here). The answer should clearly point out that the match fails for (id) because the end of input has not been reached. Ideally the student should have used the S E$ production to capture this. 4. Consider the following grammar, and answer the following questions clearly.

E id E (E * E) E (E + E) a. Is this grammar LL(1)? [2] No the grammer is not LL(1) as there are two rules for same non terminal E which start with (, so grammar is not left factored and hence is not LL(1). We can also say that First ( (E+E) ) = ( and First( (E*E) ) = ( So in parsing table, T[E, (] will have both (E+E) and (E*E) causing a conflict. So the grammer is not LL(1). [full marks for correct reason; No marks for just yes or no] b. Is this grammar SLR? [4]

DFA of items for SLR parsing of this grammar is shown below. There are no Shift reduce or reduce reduce conflicts in them, so this is SLR grammar. [full marks for correct reason; No marks for just yes or no; partial marks for partially correct DFA] 5. Give an example of a program (in a programming language of your choice) that is a correct program as far as the parser is concerned (i.e., the parser accepts that program as a valid program), but is not a correct program otherwise (i.e., the semantic analysis step would discard the program as invalid). [1]

Many examples. E.g., a C program with addition operator between a function identifier and an integer etc. Basically any program in which we expect the type checker to fail. [Full marks for correct example] Alternate working for Q3: Functions for this grammer are: Bool token(x) {

return (next++ == x); Bool S() { Return E() token( $ ); Bool E() { TOKEN *save = next; Return ((next=save, E1()) (next=save, E2()) (next=save, E3())) Bool E1() { return token( id ); Bool E2() { Return (token( ( ) E() token( ) )); Bool E3() { Return (E() E()); Input is Tokens : ( id ) ( ( id ) ) END Indexes: 0 1 2 3 4 5 6 7 8 Execution sequence will be: call S 1 terminal id!= ( false 2 terminal ( == ( 1 terminal id == id terminal ) == ) fail as next is not equal to End of Input 3

1 terminal id!= ( false 2 terminal ( == ( 1 terminal id == id terminal ) == ) 1 terminal id!= ( false 2 terminal ( == ( 1 terminal id!= ( false 2 terminal ( == ( 1 terminal id == id terminal ) == ) Note :- Although this sequence will never be able to parse (id id). So it seems wrong somewhere. So final result is parse is successful with LMD :- S -> E$ -> EE$ -> (E)E$ -> (id)e$ ->(id)(e)$ -> (id)((e))$ -> (id)((id))$