CSE 401 Midterm Exam 11/5/10

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

CSE 401 Midterm Exam Sample Solution 11/4/11

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

CSE 413 Final Exam. December 13, 2012

CSE 401 Midterm Exam 11/5/10 Sample Solution

CSE 582 Autumn 2002 Exam 11/26/02

A Simple Syntax-Directed Translator

CSE 401 Midterm Exam Sample Solution 2/11/15

CSE au Final Exam Sample Solution

CSE 413 Final Exam. June 7, 2011

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

CSE P 501 Exam 8/5/04

Midterm I (Solutions) CS164, Spring 2002

Part 5 Program Analysis Principles and Techniques

Midterm I - Solution CS164, Spring 2014

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

CSE P 501 Exam 12/1/11

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

Lecture 4: Syntax Specification

Lexical Analysis. Introduction

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Lexical and Syntax Analysis

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COP 3402 Systems Software Syntax Analysis (Parser)

Non-deterministic Finite Automata (NFA)

CSE 582 Autumn 2002 Exam Sample Solution

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

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

2.2 Syntax Definition

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

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

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

Question Points Score

Syntax Analysis Check syntax and construct abstract syntax tree

Assignment 1. Due 08/28/17

Optimizing Finite Automata

Principles of Programming Languages COMP251: Syntax and Grammars

CSE 413 Final Exam Spring 2011 Sample Solution. Strings of alternating 0 s and 1 s that begin and end with the same character, either 0 or 1.

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

Defining syntax using CFGs

CS 536 Midterm Exam Spring 2013

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Interpreter. Scanner. Parser. Tree Walker. read. request token. send token. send AST I/O. Console

Chapter Seven: Regular Expressions

CS143 Handout 20 Summer 2012 July 18 th, 2012 Practice CS143 Midterm Exam. (signed)

CS164: Midterm I. Fall 2003

B The SLLGEN Parsing System

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

LECTURE 17. Expressions and Assignment

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Left to right design 1

Languages, Automata, Regular Expressions & Scanners. Winter /8/ Hal Perkins & UW CSE B-1

CMSC 330: Organization of Programming Languages

Languages and Compilers

Question Bank. 10CS63:Compiler Design

Building Compilers with Phoenix

Chapter 2 :: Programming Language Syntax

2068 (I) Attempt all questions.

CPS 506 Comparative Programming Languages. Syntax Specification

Properties of Regular Expressions and Finite Automata

Context-Free Grammars

Formal Languages. Formal Languages

A simple syntax-directed

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

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

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

MIDTERM EXAMINATION - CS130 - Spring 2003

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

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Programming Lecture 3

CS 314 Principles of Programming Languages

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

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Chapter 2 - Programming Language Syntax. September 20, 2017

Compiler Design Aug 1996

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

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

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

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

1 Lexical Considerations

CS143 Midterm Fall 2008

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

This book is licensed under a Creative Commons Attribution 3.0 License

CSE P 501 Exam 11/17/05 Sample Solution

CSCE 531 Spring 2009 Final Exam

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

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

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

CSE P 501 Exam Sample Solution 12/1/11

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

ECS 120 Lesson 7 Regular Expressions, Pt. 1

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Figure 2.1: Role of Lexical Analyzer

CMSC 330: Organization of Programming Languages

COMPILER DESIGN UNIT I LEXICAL ANALYSIS. Translator: It is a program that translates one language to another Language.

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

Transcription:

Name There are 5 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed books, closed notes, closed electronics; please turn off all cell phones, personal electronics, alarm watches, and pagers; no telepathy or smoke signals, etc., except that you may have one sheet of paper with handwritten notes on it. Please wait to turn the page until everyone is told to begin. Score 1 / 20 2 / 14 3 / 30 4 / 20 5 / 16 Page 1 of 9

Question 1. Regular expressions (20 points) In the Ada Programming language an integer constant contains one or more digits, but it may also contain embedded underscores. Any underscores must be preceded and followed by at least one digit, which implies that a number may not begin or end with an underscore, and numbers may not contain two or more adjacent underscores. Underscores have no significance other than readability. Examples: 0, 17, 017, 0_01_7, 1_048_576. Integers may also be written in bases other than 10 by preceding the number with the base and a # character. For example, all of these constants represent the decimal number 15231: 15231, 10#15231, 16#3B7F, 16#03B_7F, 2#00111011_0111111, and 8#35577. Other bases besides powers of 2 are also allowed, for example, 2#00101, 7#5, and 5#10 all represent the decimal number 5. The base of a number (preceding the # sign) must be a number that is at least 2 and at most 16, and is written in decimal. The digits in a number consist of the decimal digits 0 through 9, and the letters A, B, C, D, E, F, written only in upper case. The language specifies that the digits must be less than the base if one is given, but for this problem we ll ignore that requirement and allow any string of digits, including A through F, to appear following a base if one is present. If no base is present, the digits may only be 0 through 9. (a) (10 points) Give a regular expression for Ada integer constants as described above. You may only use basic regular expression operators (concatenation rs, choice r s, Kleene star r*) and the additional operators r+ and r?. You may also specify character sets using the notation [abw-z], and sets excluding specified characters [^aeiou]. Finally, you can name parts of the regular expression, like vowel=[aeiou]. (continued on next page) Page 2 of 9

Quesiton 1. (cont.) (b) (10 points) Draw a DFA (Deterministic Finite Automata) that recognizes Ada integer constants as described in the problem and generated by the regular expression in your answer to part (a). Page 3 of 9

Question 2. Ambiguity (14 points) The syntax used to specify regular expressions can itself be defined by a context-free grammar. Here is one possible grammar for regular expressions with the operators concatenation, choice ( ), Kleene star ( * ), and parenthesized subexpressions over the alphabet { a, b }. R::= R R R ::= R R R ::= R * R ::= ( R ) R ::= a R ::= b (concatenation) (the here is the literal regular expression choice operator) (Kleene star) Show that this grammar for specifying the syntax of regular expressions is ambiguous. Page 4 of 9

Question 3. (30 points) The you re-probably-not-surprised-to-see-it LR-parsing question. Here is a tiny grammar. 0. S ::= S $ 1. S ::= x S S 2. S ::= y (a) (12 points) Draw the LR(0) state machine for this grammar. (continued on next page) Page 5 of 9

Question 3. (cont.) Grammar repeated from previous page for reference. 0. S ::= S $ 1. S ::= x S S 2. S ::= y (b) (12 points) Construct the LR(0) parse table for this grammar based on the state machine in your answer to part (a). (b) (3 points) Is this grammar LR(0)? Why or why not? (c) (3 points) Is this grammar SLR? Why or why not? Page 6 of 9

Question 4. First/Follow/Nullable (20 points) Consider the following grammar: S ::= A C B A ::= B C B a A B ::= b C C ::= C b C c ε Complete the following table to give the FIRST and FOLLOW sets and NULLABLE attribute for each of the non-terminals in the grammar. Symbol NULLABLE FIRST FOLLOW S A B C Page 7 of 9

Question 5. Parsing tools (16 points) For this problem we would like to translate arithmetic expressions from ordinary infix notation to postfix using CUP. In postfix notation a binary expression like a*b is written as ab*, with the operands appearing in the same order as they did originally followed by the operator. Operators act on the two expressions or subexpressions immediately to their left and operators are executed as soon as they are reached scanning left to right. Postfix expressions are unambiguous and do not require parentheses or operator precedence rules. Here are several examples: Infix Postfix Evaluation order a-b+c ab-c+ a-b is computed, then c is added to the result a-(b+c) abc+- a, b, and c are scanned, then b+c is computed first, then that result is the right operand of the subtraction from a a+b*c abc*+ multiply b*c, then add that result to a (a+b)*c ab+c* add a+b first, then multiply by c a*b+c ab*c+ multiply a*b first, then add c (a*b)+c ab*c+ same a*b+c*d ab*cd*+ multiply a*b, multiply c*d, then add the two products On the next page, complete the CUP specification so the resulting parser will accept infix arithmetic expressions involving identifiers; the operators +, -, and *; and parenthesized subexpressions, and print the corresponding postfix expression. (Note: you are not building a tree as you did for the parser part of the project. Just print things at appropriate places.) You should assume that the semantic actions can call a function print(s) that will print the contents of string s to the output immediately following any previous output. The semantic action for identifier is supplied as an example. (We re taking some liberties with types here and are assuming that when an identifier token is printed the corresponding identifier string appears in the output. That is not true for the operators PLUS, TIMES, etc. You need to print appropriate literal strings in the right places.) You will also need to add precedence declarations to handle the ambiguities in the given grammar. Write your answer on the (you can detach this page if you like) next page => Page 8 of 9

Question 5 (cont.) Complete the CUP specification below to read infix expressions and print the corresponding postfix. (Hint: the answers may be quite short and/or simple don t be alarmed if that happens.) /* Terminals (tokens returned by the scanner) */ terminal PLUS, MINUS, TIMES, LPAREN, RPAREN, IDENTIFIER; /* Nonterminals */ nonterminal void Expr; /* Precedence declarations add anything appropriate here */ /* Productions */ Expr ::= IDENTIFIER:id {: print(id) :} Expr:exp1 PLUS Expr:exp2 {: :} Expr:exp1 MINUS Expr:exp2 {: :} Expr:exp1 TIMES Expr:exp2 {: :} LPAREN Expr:exp RPAREN {: :} ; Page 9 of 9