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

Similar documents
Lexical Considerations

1 Lexical Considerations

Lexical Considerations

CS 415 Midterm Exam Spring SOLUTION

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

The SPL Programming Language Reference Manual

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

CSE 401 Midterm Exam 11/5/10

CS 415 Midterm Exam Fall 2003

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Semantic Analysis. Lecture 9. February 7, 2018

The PCAT Programming Language Reference Manual

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

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

2.2 Syntax Definition

Compiler Construction

Compiler Construction

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

IC Language Specification

SMURF Language Reference Manual Serial MUsic Represented as Functions

A Simple Syntax-Directed Translator

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

CSC 467 Lecture 3: Regular Expressions

Lexical Analysis. Introduction

Part 5 Program Analysis Principles and Techniques

COP4020 Spring 2011 Midterm Exam

MIDTERM EXAMINATION - CS130 - Spring 2003

CS 415 Midterm Exam Spring 2002

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

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

Differentiate Between Keywords and Identifiers

Decaf Language Reference

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

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

CSE 413 Final Exam. June 7, 2011

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

The role of semantic analysis in a compiler

YOLOP Language Reference Manual

CSCE 531 Spring 2009 Final Exam

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

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

Petros: A Multi-purpose Text File Manipulation Language

CHIL CSS HTML Integrated Language

CHAD Language Reference Manual

CSE 401 Midterm Exam Sample Solution 2/11/15

Topic 1: Introduction

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

Programming Language Concepts, cs2104 Lecture 04 ( )

Chapter 3:: Names, Scopes, and Bindings

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

COMP 524 Spring 2018 Midterm Thursday, March 1

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

FRAC: Language Reference Manual

Introduction to Parsing. Lecture 5

Typescript on LLVM Language Reference Manual

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

ASML Language Reference Manual

CPS 506 Comparative Programming Languages. Syntax Specification

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

CSCI Compiler Design

QUESTIONS RELATED TO UNIT I, II And III

The Decaf Language. 1 Lexical considerations

d-file Language Reference Manual

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

Introduction to Parsing. Lecture 5

LCSL Reference Manual

ECE251 Midterm practice questions, Fall 2010

A simple syntax-directed

The Decaf language 1

Programming Languages and Compilers (CS 421)

Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

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

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Lecture 4: Syntax Specification

CSE 413 Final Exam. December 13, 2012

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

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 2016

Project 1: Scheme Pretty-Printer

Chapter 2. Lexical Elements & Operators

CSE au Final Exam Sample Solution

CMSC 331 Final Exam Section 0201 December 18, 2000

MIDTERM EXAM (Solutions)

CS 403: Scanning and Parsing

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

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

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Figure 2.1: Role of Lexical Analyzer

Transcription:

Name: Midterm Exam PID: This is a closed-book exam; you may not use any tools besides a pen. You have 75 minutes to answer all questions. There are a total of 75 points available. Please write legibly; answers that I cannot decipher will receive no credit. Raise your hand if you have a question. Problem 1 Indicate for each of the following statements whether it is true or false. (Correct answer: 1 point; incorrect answer: -0.5 points; in total no less than 0 points.) [7 points] A. Functional programming is focused on message passing. True or false: B. It is generally easier to compile declarative languages into efficient machine code than to do so with imperative languages. True or false: C. Virtual machines are a popular implementation strategy because they do not require compilation. True or false: D. Writing code in a programming language should be easier than reading it since programmers spend a lot of time writing code. True or false: E. Assembly languages are procedural in nature. True or false: F. Java is a procedural language. True or false: G. It is always correct for an optimizer to remove arithmetic expressions that include divisions if the computed result is not used. True or false: E is correct; the rest is incorrect. Problem 2 Name an advantage and disadvantage of separate compilation. [2 points] Pro: enables code reuse/libraries, parallel development and collaboration. Con: difficult to maintain, version mismatch bugs, some optimizations not possible. Problem 3 Briefly describe the purpose of the lexical and syntax analysis phases in a compiler. [2 points] Lexical analysis: discover tokens, i.e., group individual input characters that together have atomic meaning; reject programs with invalid/unexpected characters. Syntax analysis: discover the structure of the program; reject programs with invalid/ unexpected structure. COMP 524 Midterm Exam 1/8

Problem 4 Indicate for each of the following statements whether it is true or false. (Correct answer: 1 point; incorrect answer: -0.5 points; in total no less than 0 points.) A. There exist regular grammars that no DFA can recognize. True or false: [5 points] B. The Kleene Star is notational sugar and can be expressed in terms of concatenation and alternation. True or false: C. NFAs and DFAs are equivalent, i.e., there exists a corresponding DFA for every NFA. True or false: D. One can construct a DFA that ensures that some string is enclosed by exactly three pairs of matching parenthesis (e.g., (((abc))) ). True or false: E. There exist context-free grammars that no NFA can recognize. True or false: Problem 5 C, D, and E are correct. [3 points] Based on the below grammar, perform a left-most derivation of a program fragment using assignment as the start symbol. Your derivation should involve at least five intermediate steps. Show all intermediate steps. assignment qname := expr ; qname id qname id. qname expr qname number expr op expr op + - * / (Terminal symbols are underlined.) assignment qname := expr ; id. qname := expr ; id. id := expr ; id. id := expr op expr ; id. id := number op expr ; id. id := number + expr ; id. id := number + number ; Problem 6 Give two reasons why the above grammar is not a LL(1) grammar. [2 points] The qname productions have a common prefix. One of the expr productions is left-recursive. COMP 524 Midterm Exam 2/8

Problem 7 Create regular expressions that specify the following (simplified) Prolog token types (adapted from the Siemens IF/Prolog manual): - Integers are sequences of digits, possibly preceded by the minus sign (-). [12 points] - Floating-point numbers are sequences of digits containing a decimal point, possibly preceded by the minus sign. - Octal numbers consist of a prefix 0o (Zero-o), and a sequence of octal digits (from 0 to 7), the whole possibly preceded by the minus sign. - Hexadecimal numbers consist of a prefix 0x (Zero-x), and a sequence of hexadecimal digits (from 0 to 9, from a to f and from A to F), the whole possibly preceded by the minus sign. - Atoms are character sequences (i) beginning with a lowercase letter and consisting only of letters, digits and underscores, or (ii) zero or more letters, digits and underscores enclosed in single quotes. - Variables are also sequences of letters, digits and underscores that begin with an uppercase letter or with an underscore to distinguish them from atoms. You may use the Kleene Star (*), alternation ( ), concatenation, and grouping with parentheses. Further, for conciseness, you may use character ranges, e.g., you may use [X0-9x-z] to represent (X 0 1 2 3 4 5 6 7 8 9 x y z). Do not use any other constructs. int: (- )[0-9][0-9]* float: (- )([0-9]*.[0-9][0-9]* [0-9][0-9]*.[0-9]*) oct: (- )0o[0-7][0-7]* hex: (- )0x[0-9a-fA-F][0-9a-fA-F]* atom: [a-z][a-za-z0-9_]* [a-za-z0-9_]* var: [A-Z_][a-zA-Z0-9_]* COMP 524 Midterm Exam 3/8

This problem pertains to Prolog structures based on the the following (simplified) description: a structure is an atom followed by an opening parenthesis, followed by one or more comma-separated terms, followed by a closing parenthesis. Recall that a Prolog term is either an atom, a variable, a numeric literal, or a structure. Problem 8 Create a context-free grammar that defines Prolog terms. Problem 9 The grammar for Problem 8 is an LL(1) grammar. [8 points] [4 points] (Use the token types from Problem 7 and left-paren, right-paren, and comma as the terminal symbols in your grammar. Only one correct answer is required to get full credit for both Problems 8 and 9.) term int float hex oct var term atom tuple tuple ε tuple left-paren term term-list right-paren term-list ε term-list comma term term-list COMP 524 Midterm Exam 4/8

Recall that we used two clauses, mother/2 and father/2, to describe a royal family tree in class. For this problem, assume that a couple has kids if and only if they are married (since this is, after all, a very honorable and royal family). Problem 10 Write a Prolog rule that expresses the mother in law relationship. [4 points] Problem 11 [4 points] Write a Prolog rule generations that counts how many generations two persons are apart. E.g., if Sam is the father of Bill, and Bill is the father of Sue, then Sam is two generations apart from Sue. mother_in_law(x, Y) :-! mother(x, A), father(a, B), mother(y, B). mother_in_law(x, Y) :-! mother(x, A), mother(a, B), father(y, B). parent(x, Y) :- father(x, Y). parent(x, Y) :- mother(x, Y). generations(x, Y, Num) :- parent(x, Y), Num = 1,!. generations(x, Y, Num) :- parent(x, Z), generations(z, Y, Gen), Num is Gen + 1. Note: the generations solution only takes direct inheritance into account, and only works from top to bottom. I gave full credit for anything that remotely approached this simplified solution. COMP 524 Midterm Exam 5/8

Problem 12 Convert the below NFA to an equivalent DFA using the sets of states construction as discussed in class. Indicate the set of states that each DFA state corresponds to. [5 points] Problem 13 Create an NFA that is equivalent to the regular expression xy(y )z*. [5 points] COMP 524 Midterm Exam 6/8

Problem 14 Find a DFA with a minimal number of states that is equivalent to the below DFA by partitioning equivalent states into equivalence classes. Indicate all partitions by drawing one or more lines (as we did in class). [5 points] COMP 524 Midterm Exam 7/8

Problem 15 [3 points] Name the three principle memory allocation classes and briefly describe the corresponding object lifetimes. Static: objects exist throughout program runtime; corresponding memory is allocated by OS before program execution starts. Runtime stack: objects exist for the duration of one subroutine execution. Heap: objects can have arbitrary lifetimes; allocation and deallocation is possible at any point in time. Problem 16 [4 points] What is the output of the below pseudo code assuming bindings are resolved using the closest nested scope rule and (respectively) A. dynamic scoping; and B. lexical (i.e., static) scoping? VAR count: INTEGER; PROCEDURE procx IS BEGIN VAR count: INTEGER; SET count TO 100; CALL report END PROCEDURE procy IS BEGIN SET count TO 200; CALL report END PROCEDURE report IS BEGIN PRINT count = + count END A) count = 100 count = 200 B) count = 300 count = 200 MAIN PROGRAM IS BEGIN SET count TO 300; CALL procx; CALL procy END COMP 524 Midterm Exam 8/8