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

Similar documents
ECE251 Midterm practice questions, Fall 2010

A simple syntax-directed

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

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

Question Points Score

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

CSE P 501 Exam 8/5/04

CMSC 330: Organization of Programming Languages

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSCI Compiler Design

CSE 401 Midterm Exam Sample Solution 2/11/15

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

COP4020 Spring 2011 Midterm Exam

CS 314 Principles of Programming Languages

A Simple Syntax-Directed Translator

CSE 401 Midterm Exam Sample Solution 11/4/11

Midterm I (Solutions) CS164, Spring 2002

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

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Compilers. Compiler Construction Tutorial The Front-end

Language Reference Manual simplicity

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

CPS 506 Comparative Programming Languages. Syntax Specification

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

Compiler Construction

Lecture 8: Deterministic Bottom-Up Parsing

1 Lexical Considerations

COP5621 Exam 3 - Spring 2005

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Lecture 7: Deterministic Bottom-Up Parsing

Compiler Theory. (Semantic Analysis and Run-Time Environments)

MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Syntax-Directed Translation. Lecture 14

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

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

Bottom-Up Parsing. Lecture 11-12

Compiler Construction

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS 403: Scanning and Parsing

Midterm I - Solution CS164, Spring 2014

CSE 401 Midterm Exam 11/5/10

Conflicts in LR Parsing and More LR Parsing Types

Syntax Analysis Check syntax and construct abstract syntax tree

Bottom-Up Parsing. Lecture 11-12

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

CSE 3302 Programming Languages Lecture 2: Syntax

CSCE 531 Spring 2009 Final Exam

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

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

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

Lecture 14 Sections Mon, Mar 2, 2009

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

Chapter 2 - Programming Language Syntax. September 20, 2017

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Building Compilers with Phoenix

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

Using an LALR(1) Parser Generator

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

QUESTIONS RELATED TO UNIT I, II And III

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

CSCE 531, Spring 2015 Final Exam Answer Key

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

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

Chapter 3: Lexing and Parsing

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Functional Programming. Pure Functional Programming

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Abstract Syntax Trees & Top-Down Parsing

UNIVERSITY OF CALIFORNIA

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Summary: Semantic Analysis

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

CS 536 Midterm Exam Spring 2013

CS5363 Final Review. cs5363 1

Syntax and Grammars 1 / 21

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

Chapter 4: Syntax Analyzer

Lexical Considerations

CMSC 330, Fall 2018 Midterm 2

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

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

Decaf Language Reference

CMSC330 Spring 2017 Midterm 2

LECTURE 3. Compiler Phases

More Assigned Reading and Exercises on Syntax (for Exam 2)

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

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

Compiler Construction Using

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

Lexical and Syntax Analysis

COP4020 Programming Assignment 2 - Fall 2016

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

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

Question Bank. 10CS63:Compiler Design

Transcription:

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.035 Spring 2013 Test I Solutions Mean 83 Median 87 Std. dev 13.8203 14 12 10 8 6 4 2 0 0 10 20 30 40 50 60 70 80 90 100 1

I DFAs, NFAs, Regular Expressions and Context Free Grammars For Questions 1 and 2, if a regular expression or context-free grammar can describe the language then provide one. Otherwise, write N/A. 1. [4 points]: The language of matched parentheses. Solution: CFG: S ɛ (1) S (S)S (2) The grammar with S (S) was also accepted since the question wasn t specific. 2. [4 points]: The language of even length strings over the alphabet 0, 1. Solution: Regular Expression: ((01) (10))* 3. [4 points]: True or false: NFAs are more powerful (can recognize more languages) than DFAs. If false, explain. If true, give an example of a language that an NFA can parse that a DFA cannot. Solution: False. Any NFA can be transformed into a DFA, although the DFA make take exponentially more space. 2

4. [8 points]: Give a regular expression for the following NFA: 0,1 start q a 0 0,1 q b 0 1 q d 1 1 q c q e Solution: (0 1)*00*11((0 1)0*11)*1 (Other valid regexes accepted) 3

II Hacking the Grammar For Questions 5 through 7, consider the following grammar for a language with expressions: E E + E E E E E c Where c is a number token. 5. [11 points]: Hack the grammar to give + higher precedence than, to make + left associative, and to make right associative. The grammar should produce a parse tree for the string 1+2+3 4 5 6 that reflects the evaluation order (((1+2)+3) (4 (5 6))). This evaluation order is also reflected in the following abstract syntax tree: + + 3 4 1 2 5 6 Solution: E F E F F F + c c 4

6. [11 points]: Remove left recursion from your answer to Question 5 to make the language parseable by a recursive descent parser with one token of lookahead. Do not worry about maintaining associativity. Solution: E F E F F c + F c 7. [6 points]: Removing left recursion from your grammar leads to weird parse trees. Draw the parse tree (not AST) your grammar from Question 6 would produce for the string 1 + 2 + 3 4 5 6. Solution: E F E c + F F E 1 c + F c F E 2 c 4 c F 3 5 c 6 5

8. [6 points]: Eliminating Shift-Reduce Conflicts: Consider the language defined by the following grammar (where S is the only nonterminal): S if a b S if a b else c If you give this grammar to a parser generator that produces a shift-reduce parser with no lookahead, then the parser generator will say that there is a shift-reduce conflict. Rewrite the grammar to eliminate the conflict. Solution: Oops. Not possible. Points for everyone. 6

III Implementing Object-Orientation: Descriptors and Symbol Tables Use the diagram on the next page to answer the following three questions about this fragment of an expression interpreter. class Environment... abstract class Expression abstract int eval(environment env); abstract class BinaryExpression extends Expression Expression op1, op2; class Plus extends BinaryExpression int eval(environment env) return op1.eval(env) + op2.eval(env); 9. [5 points]: Complete the entries of the class descriptors for each class. Use an arrow to connect the entry to a descriptor or symbol table where appropriate. 10. [5 points]: Complete the entries of the field symbol tables for each class. Use an arrow to connect the entry to a descriptor or symbol table where appropriate. 11. [5 points]: Complete the entries of the method symbol tables for each class. Use an arrow to connect the entry to a descriptor or symbol table where appropriate. 12. [5 points]: How does the method descriptor for a method with an abstract modifier differ from that of a method without the modifier? 7

Class Descriptors Expression fields methods BinaryExpression parent fields methods Plus parent fields methods Field Symbol Tables Expression BinaryExpression parent op1 op1 Plus parent Method Symbol Tables Expression eval BinaryExpression parent Plus parent eval

IV Semantic Analysis For this problem, you will write a semantic analyzer for the following simple language: P Decls E Decls D, Decls ɛ D ID = read int ID = read float ID = read string E INT FLOAT STRING ID concat(e, E) E + E The language consists of a sequence of variable declarations and a single expression consisting of constants (integer, float, and string), variable references, string concatenation, and addition. The keywords read_int, read_float, and read_string read a value of the given type from the user. Implement a semantic analyzer in pseudo-code for the program element specified in each question. Your implementation should compute the type attribute of the given production. For example, the implementations for P and Decls are as follows: P Decls E P.type = (Decls.type == "void")? E.type : Decls.type; Decls D, Decls 1 Decls.type = (D.type == "void")? Decls1.type : D.type; Decls ɛ Decls.type = "void"; Use the types int, float, str, and void. Use the type err when the program has a semantic error. Do not throw an exception. Use a global symbol table that you can manipulate and access with the functions void add(string name, string type) and string lookup(string name). lookup returns null if the symbol hasn t been defined. 9

13. [6 points]: Variable Declaration. Goal: set D.type appropriately. Semantic Rules: Each variable is declared at most once. The type of a variable is the type of the value assigned to it from the input. Semantically correct declarations have type void. Assume: ID.value contains the name of the variable. D ID = read int string t = lookup(id.value); if (t == null) add(id.value, "int"); D.type = "void"; else D.Type = "err"; 14. [2 points]: Constant Expression. Goal: set E.type appropriately. Semantic Rule: a constant has its given type (e.g., an integer has type int ). E STRING E.type = "str"; 15. [4 points]: Variable Reference Expression. Goal: set E.type appropriately. Semantic Rules: A referenced variable must be declared. The type of a variable reference is the declared type of the variable. Assume: ID.value contains the name of the variable. E ID 10

string t = lookup(id.value); E.type = (t!= null) t : "err"; 11

16. [6 points]: String Concatenation Expression. Goal: set E.type appropriately. Semantic Rule: string concatentation operates only on string operands. Assume: E1.type and E2.type have already been recursively computed by the analyzer. E concat(e 1, E 2 ) if (E1.type == "str" && E2.type == "str") E.type = "str"; else E.type = "err"; 17. [8 points]: Addition Expression. Goal: set E.type appropriately. Semantic Rules: Addition operates only on integer and float operands. If one operand is a float, then the result of the addition is a float. Assume: E1.type and E2.type have already been recursively computed by the analyzer. E E 1 + E 2 if (E1.type == "err" E2.type == "err") E.type = "err"; else if (E1.type == "str" E2.type == "str") E.type = "err"; else if (E1.type == "float" E2.type == "float") E.type = "float"; else E.type = "int"; 12