SECOND PUBLIC EXAMINATION. Compilers

Similar documents
A Simple Syntax-Directed Translator

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Static Type Checking (Introducing Micro-Haskell)

The PCAT Programming Language Reference Manual

2.2 Syntax Definition

Project 2 Interpreter for Snail. 2 The Snail Programming Language

CSE P 501 Exam 8/5/04

A simple syntax-directed

This book is licensed under a Creative Commons Attribution 3.0 License

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

UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. M.Sc. in Computational Science & Engineering

CSE 582 Autumn 2002 Exam 11/26/02

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

1 Lexical Considerations

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

CHAD Language Reference Manual

Typescript on LLVM Language Reference Manual

GAWK Language Reference Manual

CSE P 501 Exam 12/1/11

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

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

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

Writing Evaluators MIF08. Laure Gonnord

Open Book Examination. Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. M.Sc. in Mathematics & Computational Science

CSE 401 Midterm Exam 11/5/10

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Principles of Programming Languages COMP251: Syntax and Grammars

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.

The SPL Programming Language Reference Manual

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

MIDTERM EXAMINATION. CSE 130: Principles of Programming Languages. Professor Goguen. February 16, points total

Programming Languages Third Edition

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

CSCE 531, Spring 2015 Final Exam Answer Key

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 2

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

Lecture 2: Big-Step Semantics

CSE 413 Final Exam. June 7, 2011

G Programming Languages - Fall 2012

Lexical Considerations

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

15. Functional Programming

EECS 6083 Intro to Parsing Context Free Grammars

LECTURE 17. Expressions and Assignment

7. Introduction to Denotational Semantics. Oscar Nierstrasz

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

INSTITUTE OF AERONAUTICAL ENGINEERING

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

Compiler Techniques MN1 The nano-c Language

A Fast Review of C Essentials Part I

CPS 506 Comparative Programming Languages. Programming Language Paradigm

afewadminnotes CSC324 Formal Language Theory Dealing with Ambiguity: Precedence Example Office Hours: (in BA 4237) Monday 3 4pm Wednesdays 1 2pm

Stating the obvious, people and computers do not speak the same language.

9. Elementary Algebraic and Transcendental Scalar Functions

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

Lexical Considerations

CLIP - A Crytographic Language with Irritating Parentheses

Syntax. A. Bellaachia Page: 1

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Stack Abstract Data Type

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

The role of semantic analysis in a compiler

Compiler Code Generation COMP360

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

Imperative Paradigm. Syntax. Role of Programming Languages. Expressions. Expressions. Role of Programming Languages. Symbols.

CST-402(T): Language Processors

Lecture Chapter 6 Recursion as a Problem Solving Technique

Syntax Analysis Check syntax and construct abstract syntax tree

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Draw a diagram of an empty circular queue and describe it to the reader.

Introduction to Programming, Aug-Dec 2008

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

Compiler Design: Lab 3 Fall 2009

Learning the Language - V

UNIVERSITY OF COLUMBIA ADL++ Architecture Description Language. Alan Khara 5/14/2014

MIDTERM EXAMINATION - CS130 - Spring 2003

Chapter 15. Functional Programming Languages

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

CS 11 Ocaml track: lecture 6

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

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

SMURF Language Reference Manual Serial MUsic Represented as Functions

GBIL: Generic Binary Instrumentation Language. Language Reference Manual. By: Andrew Calvano. COMS W4115 Fall 2015 CVN

Question Points Score

Syntax and Grammars 1 / 21

DEMO A Language for Practice Implementation Comp 506, Spring 2018

SMPL - A Simplified Modeling Language for Mathematical Programming

CSE P 501 Exam Sample Solution 12/1/11

Application: Programming Language Semantics

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

Building Compilers with Phoenix

Parsing Scheme (+ (* 2 3) 1) * 1

Transcription:

A10401W1 SECOND PUBLIC EXAMINATION Honour School of Computer Science Honour School of Mathematics and Computer Science Honour School of Computer Science and Philosophy Compilers TRINITY TERM 2016 Thursday 2nd June, 2:30pm 4:00pm Candidates should answer no more than two questions. Please start the answer to each question on a new page. Do not turn over until told that you may do so. 1

Question 1 The following context-free grammar describes expressions expr, built up from identifiers Ident and opening and closing parentheses Lpar and Rpar. expr Ident Lpar expr Rpar expr expr. An example string is the following: equal (fac n) (if (equal n zero) one (times n (fac (minus n one )))) As you can see, this expression must end in exactly four right parentheses. An expression expr 1 expr 2 denotes application of a function expr 1 to an argument expr 2. An implementation of the language uses abstract syntax trees with type tree, defined by type tree = Var of string Apply of tree tree. As in the languages Haskell and OCaml, application associates to the left, so that the subexpression equal n zero corresponds to the abstract syntax tree, Apply (Apply (Var "equal", Var "n"), Var "zero"), the same as the expression (equal n) zero. (a) Show that the grammar given above is ambiguous, and suggest an unambiguous grammar for the same language, respecting the interpretation that application associates to the left. (8 marks) (b) Using your grammar, write rules for an ocamlyacc script that parses expressions and builds the corresponding abstract syntax tree. (5 marks) Experience shows that programmers find it laborious to count the many right parentheses that may be needed at the end of an expression, and a new language feature is proposed that allows an opening parenthesis to be replaced by a left square bracket Lbrack, to be matched by a subsequent right square bracket Rbrack. The closing bracket implies as many closing parentheses as are needed to make the expression balanced. Like parentheses, the square brackets may be nested arbitrarily deep. Thus, the expression shown earlier may be replaced with the following. equal (fac n) (if (equal n zero) one [times n (fac (minus n one ]) The closing bracket implies the two parentheses needed to finish the expressions beginning (fac and (minus; one more closing parenthesis is needed to match (if. (c) Write an unambiguous grammar for this extended language, expressing it in the form of an ocamlyacc script that produces the corresponding abstract syntax tree. (12 marks) A10401W1 2

Question 2 The following program is written in a Pascal-like language, with keywords in upper case. Procedure one has a parameter x that is passed by reference; this parameter is accessed from the body of the nested procedure two. The main program calls procedure one, passing it a global variable y; the output is 37. PROC one(var x: integer); PROC two(); BEGIN x := x+1 END; BEGIN two() END; VAR y: integer; BEGIN (* main program *) y := 36; one(y); print_num(y); newline() END. There are two implementations of the programming language. One of them is based on postfix code for a simple stack machine, where variable access is implemented using arithmetic on addresses. This implementation generates code by a simple syntax-directed translation, and uses little optimisation. The second implementation generates machine code for a RISC machine, and employs Common Subexpression Elimination (CSE) within basic blocks. In both implementations, access to non-local variables uses static links. On the RISC machine, both procedure arguments and static links are passed in registers but saved in the stack frame as part of the procedure prologue. (a) Describe the run-time structures needed to represent activations of the procedures one and two, showing a possible layout of their stack frames, together with sample postfix code for the procedures one and two and for the call one(y) in the main program. (8 marks) (b) Explain how the statement x := x+1 could be represented internally in the second compiler in a form suitable as the input to CSE, and show how the result of CSE would be represented. (9 marks) (c) Suggest rules for selecting instructions for elements of the internal representation used in part (b), and give a translation of the optimised statement into code for a typical RISC machine. (8 marks) In each part of the question, you need not use code that corresponds to any particular machine, provided the meaning of each operation is clear and your assumptions are stated. A10401W1 3 TURN OVER

Question 3 Two robotics researchers, Jack and Tom, have proposed a new control structure, designed to help in the description of finite state machines. An example is shown in Figure 1, with keywords in upper case. This machine prints the number of groups of consecutive 1 bits in the binary representation of a positive integer val; thus if initially val is 819 = 1100110011 2, then the program prints 3. This machine has four states, init, done, flop and flip. It starts in the state that appears first in the construct in this case init. The action of the machine in each state is described by ordinary statements of the language, except that embedded statements of the form NEXT flop are allowed, and cause execution of the code for the state to be abandoned, and the code for the state flop to start running, until another NEXT statement activates a different state, and so on. If the code for any state finishes without the machine encountering a NEXT statement, then the whole construct terminates; in the example, this happens after state done is entered. The construct is to be added to an existing compiler that implements the other statement forms shown in the example. The compiler builds and annotates an abstract syntax tree, then generates code for a typical stack-based abstract machine. (a) Suggest an abstract syntax for this construct. (3 marks) (b) Describe the information that should be associated by the semantic analyser of a compiler with the name of each state, and what annotations need to be added to the abstract syntax tree to support code generation. (5 marks) (c) Identify two errors that could be detected during semantic analysis, and one warning that the semantic analyser could helpfully give. Explain how these conditions can be detected during semantic analysis. (4 marks) (d) Using appropriate conventions, show how the new construct can be implemented in a code generator. (7 marks) (e) A direct translation of the construct may contain many jumps and labels and may be untidy and inefficient. Referring to the example in Figure 1, discuss the sorts of inefficiency that may arise, and the extent to which the code can be improved by a simple optimiser. (6 marks) A10401W1 4

MACHINE STATE init: count := 0; NEXT flop STATE done: print_num(count); newline() STATE flop: IF val MOD 2 = 1 THEN count := count+1; NEXT flip END; val := val DIV 2; IF val = 0 THEN NEXT done ELSE NEXT flop END STATE flip: IF val MOD 2 = 0 THEN NEXT flop END; val := val DIV 2; NEXT flip END Figure 1: Program text for Question 3 A10401W1 5 LAST PAGE