CS 536 Midterm Exam Spring 2013

Similar documents
Question Points Score

Last Time. What do we want? When do we want it? An AST. Now!

Defining syntax using CFGs

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

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

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).

CSCI Compiler Design

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

A Simple Syntax-Directed Translator

CSCI312 Principles of Programming Languages!

CS164: Midterm I. Fall 2003

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

Lexical and Syntax Analysis

Compilers. Predictive Parsing. Alex Aiken

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

LL(1) predictive parsing

CSE 401 Midterm Exam 11/5/10

Top down vs. bottom up parsing

Syntax Analysis Check syntax and construct abstract syntax tree

Topic 3: Syntax Analysis I

Lexical and Syntax Analysis. Top-Down Parsing

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

Lexical and Syntax Analysis

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

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

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

CS 321 Programming Languages and Compilers. VI. Parsing

Midterm I (Solutions) CS164, Spring 2002

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

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

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

Syntax Analysis Part I

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

CSE 5317 Midterm Examination 4 March Solutions

Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

LL parsing Nullable, FIRST, and FOLLOW

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

Compilers. Compiler Construction Tutorial The Front-end

Problem Score Max Score 1 Syntax directed translation & type

CS502: Compilers & Programming Systems

Syntax-Directed Translation. Lecture 14

2.2 Syntax Definition

Automatic generation of LL(1) parsers

Chapter 3. Parsing #1

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

Context-Free Grammars

ECE251 Midterm practice questions, Fall 2010

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Compiler Design Concepts. Syntax Analysis

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

Abstract Syntax Trees & Top-Down Parsing

CS 415 Midterm Exam Spring SOLUTION

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

CSE 401 Midterm Exam Sample Solution 11/4/11

Principles of Programming Languages COMP251: Syntax and Grammars

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

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

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Context-free grammars (CFG s)

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

CS 314 Principles of Programming Languages

CS 415 Midterm Exam Spring 2002

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

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

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

A simple syntax-directed

Defining syntax using CFGs

Eng. Maha Talaat Page 1

LL(1) predictive parsing

Midterm II CS164, Spring 2006

KEY. A 1. The action of a grammar when a derivation can be found for a sentence. Y 2. program written in a High Level Language

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

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

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

More Bottom-Up Parsing

Bottom-Up Parsing. Lecture 11-12

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Midterm I - Solution CS164, Spring 2014

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

Parsing II Top-down parsing. Comp 412

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lexical and Syntax Analysis (2)

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

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

Principles of Programming Languages COMP251: Syntax and Grammars

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

4. Lexical and Syntax Analysis

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

WARNING for Autumn 2004:

Transcription:

CS 536 Midterm Exam Spring 2013 ID: Exam Instructions: Write your student ID (not your name) in the space provided at the top of each page of the exam. Write all your answers on the exam itself. Feel free to use the backs of the pages for scratch work. If you need more space for scratch work (or if you'd prefer not to use the backs of the pages), you may use a blue book. A copy of the Mini grammar is provided to you for reference. When you are finished, turn in this exam as well as your page of notes (and blue book, if you used one). Make sure your name is on your page of notes. Points Out of Question 1 Part (a) 6 Part (b) 6 Question 2 10 Question 3 Part (a) 15 Part (b) 4 Part (c) 4 Part (d) 4 Question 4 15 Question 5 Part (a) 24 Part (b) 12 Total 100 Page 1 of 12

Question 1 (12 points) This question concerns the language of words. A word contains vowels and consonants and must follow these rules: 1. Every word must have at least one vowel. 2. No word can have two vowels in a row. Part (a) Draw a deterministic finite-state machine for the language of words as described above. Label the start state "S" and use a double circle to indicate the final state(s). You may label the edges vowel or cons (meaning consonant). Part (b) Write a regular expression for the language of words. You may use vowel and cons in your regular expression. Page 2 of 12

Question 2 (10 points) Suppose we extend the Mini language to allow MATLAB-like for loops. A Mini for loop statement has the following structure: for (id = begin : incr : end) { // body of for loop } where id is an identifier and begin, incr, and end are expressions that evaluate to integers. The Mini for loop works the same as the following Java code: for (id = begin; id <= end; id += incr) { // body of for loop } The increment value (incr) is optional (if it is not present, the default increment of the for loop is 1), so the Mini for loop: for (id = begin : end) { // body of for loop } is equivalent to the Java code: for (id = begin; id <= end; id += 1) { // body of for loop } The body of the Mini for loop statement has the same format as the body of a Mini if statement. Suppose we have added the following two tokens to our Mini grammar: Token name JLex Pattern COLON ":" FOR "for" What new productions will need to be added to the Mini grammar to incorporate this extension? A copy of the Mini grammar is provided. Use lower-case names for nonterminals and use the added tokens as well as tokens already given in the grammar. The productions you add may make use of any nonterminals already given in the grammar. Write your productions so they are easy to understand; do not make them LL(1). Note that, consistent with Mini grammar provided, you do not need to deal with issues of operator associativity or precedence. Put your answer on the next page. Page 3 of 12

Put your answer for Question 2 on this page Page 4 of 12

Question 3 (27 points) Part (a) Suppose we have defined two new binary operators? and $. Write an unambiguous context-free grammar for the language of expressions with integer literal operands and two operators? and $. The language should also allow parenthesis, as usual. Here are some examples to let you figure out the precedences and associativities: expression equivalent fully-parenthesized expression 1? 2? 3 (1? 2)? 3 1 $ 2 $ 3 1 $ (2 $ 3) 1 $ 2? 3 (1 $ 2)? 3 1? 2 $ 3 1? (2 $ 3) 1 $ 2? 3 $ 4 (1 $ 2)? (3 $ 4) 1? 2 $ 3? 4 1? (2 $ 3)? 4 Page 5 of 12

Part (b) Using the grammar you wrote for part (a), draw the parse tree for the input sequence: 1? 2 $ (3? 4) Page 6 of 12

Part (c) If the grammar you wrote for part (a) has any immediate left recursion, apply the transformations learned in class to remove it, and write the result below. Give the entire grammar, not just the rules you transformed. Part (d) If the grammar you wrote for part (b) needs left factoring, do that and write the result below. Give the entire grammar, not just the rules you transformed. Page 7 of 12

Question 4 (15 points) Below is a non-ll(1) context-free grammar for a simple language of declarations and assignments: program vardecllist vardecllist vardecllist vardecl vardecl vardecl type ID ; type INT BOOL stmt stmt stmt ID = exp ; IF ( exp ) { vardecllist } exp ID INTLIT exp + exp exp * exp exp == exp One simple optimization a compiler can perform (on languages that don't allow pointers) is to remove code that declares or defines a variable that never gets used in the program. For example, int x; // x is declared int y; // y is declared int z; // z is declared x = 7; // x is defined y = x + 3; // y is defined, x is used z = 2*x + 4; // z is defined, x is used if (z == 3) { // z is used y = 5; // y is defined } // y is never used, so we could remove the declaration of y // and the two statements that define y In order to perform this optimization, the compiler needs to know which variables get used (as opposed to just declared and/or defined) in a program. You are to define a syntax-directed translation rule for each of the CFG's productions so that the translation of a program is a list of the variables used in the program. Assume that the value of an ID token (ID.val) is the name of that identifier and the value of an INTLIT token (INTLIT.val) is the actual integer value. You may use the following List operations (assume that L1 and L2 are lists and N is a name): new List( ) creates a new (empty) list new List(N) creates a new list containing N L1.add(N) adds N to list L1 L1.remove(N) removes N from L1 union(l1, L2) creates a new list containing all the names in L1 and L2 (with duplicates removed) Page 8 of 12

Write your translation rules in the table below. (If there is a nonterminal that is not relevant to the translation, you do not need to write a translation rule for grammar productions with that nonterminal on the left-hand side.) For a grammar production with a left-hand side nonterminal x, your translation rule should be of the form "x.trans = " Grammar rule Translation rule program vardecllist vardecllist vardecllist vardecl vardecllist vardecl vardecl type ID ; type INT type BOOL stmt stmt stmt ID = exp ; stmt IF ( exp ) { vardecllist } exp ID exp INTLIT exp exp + exp exp exp * exp exp exp == exp Page 9 of 12

Question 5 (34 points) Below is a context-free grammar for a language of simple blocks of code: block decllist decl stmt exp LCURLY decllist RCURLY decl decllist ε stmt ε TYPE ID SEMI ID EQUALS exp SEMI IF LPAREN exp RPAREN block ELSE block RETURN exp SEMI ID INT Part (a) Fill in the FIRST and FOLLOW sets for the nonterminals below: Non-terminal X FIRST(X) FOLLOW(X) block decllist decl stmt exp Page 10 of 12

Part (b) Recall that we can think of a predictive parser as building the parse tree for its input topdown. Each time the parser's top-of-stack symbol is a nonterminal X, it pops X and pushes the righthand side of some grammar rule X α. This corresponds to growing the parse tree by giving the leftmost X leaf in the tree one child for each symbol in α. Below and on the next page are three incomplete "snapshots" of moments during a parse of the grammar given on the previous page. Each snapshot includes the input being parsed, the current token, the current symbols in the parser's stack, and the current partial parse tree build by the parser. Your job is to fill in the missing parts of each snapshot. Snapshot 1: input: LCURLY ID EQUALS INT SEMI RETURN ID SEMI RCURLY current token: RETURN partial parse tree current stack block LCURLY decllist RCURLY ε stmt ID EQUALS INT SEMI Fill in the contents of the current stack. Page 11 of 12

Snapshot 2: input: LCURLY TYPE ID SEMI RCURLY current token: ID partial parse tree current stack ID SEMI decllist RCURLY EOF Fill in the partial parse tree. Snapshot 3: input: current token: RCURLY partial parse tree current stack block LCURLY decllist RCURLY ε Fill in the input and the contents of the current stack. Page 12 of 12