Programming Assignment in Semantics of Programming Languages

Size: px
Start display at page:

Download "Programming Assignment in Semantics of Programming Languages"

Transcription

1 Programming Assignment in Semantics of Programming Languages The programming assignment ( lab ) is to implement a byte code interpreter and verifier for a simple object oriented byte code language which could be used as the target for a compiler of an object oriented language. 1 The assignment is inspired by the article Typing a Multi-Language Intermediate Code by Andrew D. Gordon and Don Syme about the byte code verifier for the intermediate language in Microsoft s.net framework. The purpose of the assignment is to confront you with a formal description of a language whose semantics and type system is not immediately obvious from the syntax. It is assumed that you have knowledge of grammars (to understand the specification of the syntax) and operational semantics definitions (to understand the semantics of the machine). The assignment is divided into two parts. Part I is the interpreter and part II is the byte-code verifier. 1 A Byte Code Interpreter 1.1 The Language The language is a simple object oriented language. A program consists of a set of declarations which in the concrete syntax are separated by semicolons. Programs P ::= D 1 ;... ; D n Each declaration D i is either a type signature or a class definition. The type signatures have no effect whatsoever on the execution of the program and they are there only to make it easier for you to implement the byte code verifier in part II. We will thus ignore them for the moment and move on to the class definitions. The syntax of the type signatures is given in Appendix.1 for those of you who wants to implement your own parsers. A class definition has the following syntax. Class class ::= class cname = fname 1 ;... ; fname n ; mname 1 = A 1 ;... ; mname n = A n where cname, fname and mname ranges over names of classes, fields and methods. The class definition specifies that an object of this class will have the fields fname 1 ;... ; fname n. Each of these fields can hold a 32-bit word which may be considered as an integer or as an address. The class definition also specifies that an object of this class will have the methods mname 1 ;... ; mname n with method bodies A 1,..., A n respectively. All the names of classes in a program must be distinct and the field names and the method names must be distinct within each class. However, the same method names and field names may occur in several classes. Every program must have a special class called Main with a field io and a method main. When a program is run an object of class Main will be created with the field io set to some input and the method main will be called on this object. When a program terminates the value of io will be considered as the result of the program. Based on a lab assignment originally written by Jörgen Gustavsson, 2002 for the course Semantics of Programming Languages. Modified by David Sands, A realistic byte code language for this purpose would have to be more complex. 1

2 The body of a method consists of a sequence of byte code instructions. The form of these instructions is given by the following grammar. Instr. seq. A, B ::= I 1... I n Instructions I ::= l : δ δ ldc n discard n m ldstack n ststack n ldfld fname stfld fname add sub equal leq jump l brtrue l brfalse l newobj cname call mname Note that instructions are very simple and that no instructions have sub instructions in contrast to commands and expressions which usually have sub commands and sub expressions. This makes it easy to code a sequence of instructions as a byte array which is the reason for why it is called a byte code language. It also makes it easy to write a parser for the language and the concrete syntax is the same as the abstract syntax. Note that there is no return instruction. Instead a method returns when it reaches the end of the instruction sequence. Instructions of the form l : δ δ are called labels and they are not instructions in the normal sense when execution passes a label nothing happens. Labels are instead used to mark points in the instruction sequence which you can jump to with a jump or branch instruction. All the labels within a method must be distinct and it is not possible to jump to a label which occurs in another method. All labels are annotated with a type δ δ which is there to make it easier for you to implement the byte code verifier. They play no rôle in the execution of the program so you may ignore them for the moment. Most instructions operate on the stack. For example the instruction add will take two elements from the stack, add them and store the result on top of the stack. The stack is also used for local variables, for parameters to methods and for results from methods. We will use S to range over stacks, ɛ to denote an empty stack and n S to mean that n is on top of S. The stack contains 32-bit words, which may be considered as integers or as addresses. We will write S for the number of elements in a stack and S(n) for the n th element on the stack. We number the elements from the top and downwards starting from 0. So S(0) is the top of the stack, S(1) is element below and so on. Similarly we write S[n m] for updating the n th element of S with m. Conceptually, an object of a class is a record with fields and methods as specified by the class definition. The fields of an object may be updated but the methods may not be changed by the execution so we can represent an object as a pair of the name of its class and the fields as follows. Objects O ::= (cname, fields) When a method is called on an object we use the class name to get hold of the right method body. We model the fields of an object by a partial function from the appropriate field names to 32-bit words. When an object is created space is allocated on the heap which we model as a partial function from 32-bit words to objects. We use H to range over heaps. Objects are passed by reference, i.e., if we for instance want to pass an object to a method we store the 32-bit address of the object on the stack before we call the method. Configurations are triples A, S, H of an instruction sequence, a stack and a heap. We will use C to range over configurations. The judgements of our big-step semantics is of the form P, B C C where P is the program (i.e., a set of declarations), B is the body of the method that is currently executing, C is the initial configuration and C is the terminal configuration. The program P is used to access the class definitions which is needed when a new object is created and when a method is called. The body B of the currently executing method is used when a jump or branch instruction is executed. Then we use the notation B(l) to denote the instruction sequence starting directly after the label l. The initial configuration when we run a program with input n is ldc n newobj Main call main, ɛ,. 2

3 ldc P, B A, n S, H ɛ, S, H P, B ldc n A, S, H ɛ, S, H discard P, B A, n 1... n i S, H ɛ, S, H P, B discard i j A, n 1... n i m 1... m j S, H ɛ, S, H P, B A, n S, H ɛ, S, H ldstack P, B ldstack i A, S, H ɛ, S, H if S > i and S(i) = n P, B A, S[i n], H ɛ, S, H ststack P, B ststack i A, n S, H ɛ, S, H P, B A, n 2 S, H ɛ, S, H add P, B add A, n 1 n 0 S, H ɛ, S, H if S > i if n 2 = n 0 + n 1 P, B A, n 2 S, H ɛ, S, H sub P, B sub A, n 1 n 0 S, H ɛ, S, H P, B A, n 2 S, H ɛ, S, H equal P, B equal A, n 1 n 0 S, H ɛ, S, H P, B A, n 2 S, H ɛ, S, H leq P, B leq A, n 1 n 0 S, H ɛ, S, H if n 2 = n 0 n 1 where n 2 = where n 2 = { 1 if n 0 = n 1 0 otherwise { 1 if n 0 n 1 0 otherwise Figure 1: Evaluation rules for stack manipulation. The instructions will create an object in class Main and then call the method main. The rules defining P, B C C are given in Figure 4, 5 and 6. 2 A Byte Code Verifier Your task in part II of the computer assignment is to implement a byte code verifier. The language in question is the very same as in part I. The verifier is based on a simple type system for the byte code. The verifier should accept a program if and only if it is well typed. If a program is ill typed then the verifier should give an appropriate error message. If a program passes the verifier then it is guaranteed (unless I have made some mistake) to not cause certain run time errors. Among other things it is supposed to guarantee that an integer is never used as a reference to an object in the heap, references to objects are not used in arithmetic operations, and there are always enough elements on the stack to execute instructions that manipulate the stack. However even if a program passes the verifier it may happen that the instruction call mname causes a runtime error because there is no method with the name mname in the object that the instruction operates on, and 3

4 ɛ P, B ɛ, S, H ɛ, S, H label P, B A, S, H ɛ, S, H P, B l : δ δ A, S, H ɛ, S, H P, B A, S, H ɛ, S, H jump P, B jump l A, S, H ɛ, S, H P, B A, S, H ɛ, S, H brtrue P, B brtrue l A, n S, H ɛ, S, H P, B A, S, H ɛ, S, H brfalse P, B brfalse l A, n S, H ɛ, S, H if l dom(b) and B(l) = A if l dom(b) and A = if l dom(b) and A = { B(l) if n 0 A otherwise { B(l) if n = 0 A otherwise P, A j A j, n S, H ɛ, S, H P, B A, S, H ɛ, S, H call P, B call mname A, n S, H ɛ, S, H if n dom(h), H(n) = (cname, fields), class cname = fnames; mname 1 = A 1 ;... ; mname i = A i P and mname = mname j Figure 2: Evaluation rules for control flow. P, B A, m S, H ɛ, S, H if n dom(h), ldfld H(n) = (cname, fields), P, B ldfld fname A, n S, H ɛ, S, H and fields(fname) = m P, B A, S, H[m (cname, fields[fname n])] ɛ, S, H stfld P, B stfld fname A, m n S, H ɛ, S, H if m dom(h) and H(m) = (cname, fields) P, B A, m S, H[m (cname, fields)] ɛ, S, H newobj P, B newobj cname A, n 1... n i S, H ɛ, S, H ( ) ( ) if m dom(h), class cname = fname 1 ;... ; fname i ; methods P and fields = [fname 1 n 1,..., fname i n i ] Figure 3: Evaluation rules for object manipulation. 4

5 the instructions ldfld fname and stfld fname cause a runtime error because there is no field with the name fname in the the object that the instructions operate on. If you want an additional challenge you may extend the type system such that it can prevent the second kind of error. If you want to do that then pass by my office and I will give you a hint on how to do it. Contact me when you are ready with the assignment so that we can agree on the time for a meeting where you can demonstrate to me what you have done. I have made some test cases available from the home page which you can use to test your verifier. I would like you to finish both parts of the computer assignment at the latest in the first week of the next study period. However, as you might guess, I would recommend you to do it earlier. 2.1 The Type System 2.2 Types At the core of the type system are the two base types which are int and object. These are used to give a type to the individual fields in an object and to the values stored on to the stack. The type of a stack take the following form. The idea is that a stack with the type Base Types ρ ::= int object Stack Types δ ::= ρ 1... ρ n where n 0 ρ 1... ρ n has at least n elements, that the top of the stack has base type ρ 1, that the second element has base type ρ 2, and so on. We will write δ 0 δ 1 for the concatenation of two stack types, δ for the length of the stack type, δ(i) for the i th element and δ[i ρ] for the stack type where the i th base type of δ has been updated with ρ. The operations are defined as follows. (ρ 1... ρ n ) (ρ n+1... ρ n+m ) = ρ 1... ρ n+m ρ 1... ρ n = n (ρ 1... ρ n )(i) = ρ i+1 if 0 i n 1 (ρ 1... ρ i+1... ρ n )[i ρ] = (ρ 1... ρ... ρ n ) if 0 i n 1 Note that δ(0) refers to the first base type, δ(1) to the second and so on. We give a sequence of byte code instructions a type of the form δ δ. The intuitive meaning of the type is that the instruction sequence requires that the stack before the sequence is executed has type δ. And, if that is the case, then the stack after the sequence has been executed has the type δ. For example the instruction sequence has the type add add int int int nil int nil because it requires that there are at least three integers on top of the stack and after its execution the three integers have been replaced with one integer. 2.3 Type Signatures and Annotations The byte code is augmented with type signatures and type annotations to make it easier for you to implement the verifier. If you want a challenge then you may try to ignore them and infer them automatically. 5

6 There are two kinds of type signatures. Field signatures take the form field fname : ρ and specifies that the field fname has the base type ρ. I.e., it specifies whether the field stores an integer or a reference to an object. The field signatures is a top level declaration and it applies to all classes. Method signatures take the form method mname : τ and specifies that the method mname has the method type τ. Method signatures are also top level declarations and apply to all classes. Method types are of the following form. Method Types τ ::= ρ τ (ρ 1,..., ρ n ) where n 0 In the concrete syntax one may omit the parentheses in (ρ 1,..., ρ n ) if n = 1. I.e, one may write ρ instead of (ρ). Finally labels in the instruction sequence are annotated with a type δ δ which is the type of the instruction sequence following the label. The entire syntax of the language is given for reference in Appendix Typing Rules The type system have two kind of typing judgements, one for typing instruction sequences and one for typing methods. Typing Instruction Sequences The typing judgements for instruction sequences take the form Γ, A : δ δ and should be read out as the instruction sequence A has the type δ δ in the context of Γ,. The context Γ contains global typing information about the type of all fields and methods. For each class in the program there is also a tuple of base types which are the types of its fields. Formally we model Γ as a partial functions which maps field names to base types, method names to method types and class names to tuples of base types. The context contains local typing information about the labels in the method currently typed. Formally it is a partial function from label names to types of the form δ δ. The information in origins from the type annotations on labels in the input program. The typing rules for instruction sequences are given in Figure 4, Figure 5 and Figure 6 and we leave them without further comments. Typing Methods The second kind of typing judgements are for typing methods. The judgement take the form Γ mname = A wt and should be read as the method mname with the body A is well typed in the context Γ. The only rule (: method ) for typing methods is as follows. Γ, A : object ρ 1... ρ n ρ 1... ρ m l dom(a).γ, A(l) : (l) Γ mname = A wt if Γ(mname) = ρ 1 ρ n (ρ 1,..., ρ m) and l dom(a). (l) = typeat(a, l) The first premise of the rule specifies that for a method to be well typed its body naturally has to have a body whose type correspond to the type that has been declared for the body. I.e., if the type of the method is declared to be ρ 1 ρ n (ρ 1,..., ρ m) then the instruction sequence in the body has to have the type object ρ 1... ρ n ρ 1... ρ m. Note that we require that the body expects an object on top of the stack. This is because when a method is called there is always a reference to the concerned object on top of the stack. The second premise of the rule specifies that for each label in the method it should be the case that the instruction sequence following the label has the type that it is annotated with. There we 6

7 : ldc Γ, ldc n A : δ δ : discard Γ, A : ρ 1... ρ i δ δ Γ, discard i j A : ρ 1... ρ i ρ i+1... ρ i+j δ δ : ldstack Γ, A : ρ δ δ if δ > i and δ(i) = ρ Γ, ldstack i A : δ δ : ststack Γ, A : δ[i ρ] δ if δ > i Γ, ststack i A : ρ δ δ : add Γ, add A : int int δ δ : sub Γ, sub A : int int δ δ : equal Γ, equal A : int int δ δ : leq Γ, leq A : int int δ δ Figure 4: Typing rules for stack manipulating instructions. 7

8 : ɛ Γ, ɛ : δ δ : label if (l) = δ δ Γ, l : δ δ A : δ δ : jump if (l) = δ δ Γ, jump l A : δ δ : brtrue Γ, A : δ δ if (l) = δ δ Γ, brtrue l A : int δ δ : brfalse Γ, A : δ δ if (l) = δ δ Γ, brtrue l A : int δ δ : call Γ, A : ρ 1... ρ m δ δ Γ, call mname A : object ρ 1... ρ n δ δ if Γ(mname) = ρ 1 ρ n (ρ 1,..., ρ m) Figure 5: Typing rules for control flow instructions. : ldfld Γ, A : ρ δ δ if Γ(fname) = ρ Γ, ldfld fname A : object δ δ : stfld Γ, A : δ δ if Γ(fname) = ρ Γ, stfld fname A : object ρ δ δ : newobj Γ, A : object δ δ if Γ(cname) = (ρ 1,..., ρ n ) Γ, newobj cname A : ρ 1... ρ n δ δ Figure 6: Typing rules for object manipulating instructions. 8

9 use the notation typeat(a, l) to denote the type annotation on l. We also use dom(a) for the set of labels in A and A(l) for the instruction sequence that follows after l. We are now ready to specify what is required for a whole program to be well typed. Definition 1 A program P is well typed if and only if the field io is declared to have type int, the method main is declared to have type (), the class Main is declared to have exactly one field io and exactly one method main, and all methods in all classes are well typed in a context Γ such that Γ(fname) = ρ if and only if field fname : ρ P Γ(mname) = τ if and only if method mname : τ P Γ(cname) = (ρ 1,..., ρ n ) if and only if classcname = fname 1 ;... fname n ; methods P and i {1,..., n}.field fname i : ρ i P..1 Full Syntax The entire syntax of the language, including type signatures, is given in this appendix. The rôle of the type signatures will be explained in the instructions for part II of the computer assignment. What is given below is the abstract syntax but the concrete syntax of the language only differs in two aspects. Firstly, the symbol in the abstract syntax is replaced with a. in the concrete syntax. Secondly, the syntax specifies that a method type may be of the form (ρ 1,..., ρ n ). If n = 1 then the concrete syntax allows us to omit the parentheses and write ρ instead of (ρ). Base Types ρ ::= int object Method Types τ ::= ρ τ (ρ 1,..., ρ n ) where n 0 Stack Types δ ::= ρ 1... ρ n where n 0 Programs P ::= D 1 ;... ; D n Declarations D ::= method mname : τ field fname : ρ class cname = fname 1 ;... ; fname n ; mname 1 = A 1 ;... ; mname n = A n Instr. seq. A, B ::= I 1... I n Instructions I ::= l : δ δ ldc n discard n m ldstack n ststack n ldfld fname stfld fname add sub equal leq jump l brtrue l brfalse l newobj cname call mname 9

Compilers and computer architecture: A realistic compiler to MIPS

Compilers and computer architecture: A realistic compiler to MIPS 1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

Project 5 Due 11:59:59pm Tue, Dec 11, 2012

Project 5 Due 11:59:59pm Tue, Dec 11, 2012 Project 5 Due 11:59:59pm Tue, Dec 11, 2012 Updates Dec 10 - Fixed X.new typos (should have been new X ). Changed error message for instantiation Bot; either the old or new error message is acceptable.

More information

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

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design i About the Tutorial A compiler translates the codes written in one language to some other language without changing the meaning of the program. It is also expected that a compiler should make the target

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

Lecture Notes on Static Semantics

Lecture Notes on Static Semantics Lecture Notes on Static Semantics 15-411: Compiler Design Frank Pfenning Lecture 12 October 8, 2015 1 Introduction After lexing and parsing, a compiler will usually apply elaboration to translate the parse

More information

COS 320. Compiling Techniques

COS 320. Compiling Techniques Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly

More information

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions)

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions) Introduction Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions) In this project, you will write a compiler for a programming language called Rube, which is a small objectoriented programming

More information

Symbol Tables Symbol Table: In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is

More information

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m. CS 6110 S18 Lecture 8 Structural Operational Semantics and IMP Today we introduce a very simple imperative language, IMP, along with two systems of rules for evaluation called small-step and big-step semantics.

More information

Sémantique des Langages de Programmation (SemLP) DM : Region Types

Sémantique des Langages de Programmation (SemLP) DM : Region Types Sémantique des Langages de Programmation (SemLP) DM : Region Types I) Submission Submission Date : 21/05/2017 Submission Format : Submit a virtual machine (.ova) 1 with 1. an executable of the interpreter,

More information

Fall 2016 CSE Qualifying Exam CSCE 531, Compilers

Fall 2016 CSE Qualifying Exam CSCE 531, Compilers Fall 2016 CSE Qualifying Exam CSCE 531, Compilers 1. LR-Parsing (a) Give definitions of FIRST( ) and FOLLOW(X). (b) Consider the following augmented grammar G with start symbol S 0 : S 0! S S! V = E S!

More information

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d) CMSC 330: Organization of Programming Languages Tail Calls A tail call is a function call that is the last thing a function does before it returns let add x y = x + y let f z = add z z (* tail call *)

More information

Project 4 Due 11:59:59pm Thu, May 10, 2012

Project 4 Due 11:59:59pm Thu, May 10, 2012 Project 4 Due 11:59:59pm Thu, May 10, 2012 Updates Apr 30. Moved print() method from Object to String. Apr 27. Added a missing case to assembler.ml/build constants to handle constant arguments to Cmp.

More information

In Our Last Exciting Episode

In Our Last Exciting Episode In Our Last Exciting Episode #1 Lessons From Model Checking To find bugs, we need specifications What are some good specifications? To convert a program into a model, we need predicates/invariants and

More information

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]

More information

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

Flang typechecker Due: February 27, 2015

Flang typechecker Due: February 27, 2015 CMSC 22610 Winter 2015 Implementation of Computer Languages I Flang typechecker Due: February 27, 2015 Project 3 February 9, 2015 1 Introduction The third project is to implement a type checker for Flang,

More information

Lecture 2: Big-Step Semantics

Lecture 2: Big-Step Semantics Lecture 2: Big-Step Semantics 1 Representing Abstract Syntax These are examples of arithmetic expressions: 2 * 4 1 + 2 + 3 5 * 4 * 2 1 + 2 * 3 We all know how to evaluate these expressions in our heads.

More information

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

Compiler Theory. (Semantic Analysis and Run-Time Environments) Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful

More information

Tree Parsing. $Revision: 1.4 $

Tree Parsing. $Revision: 1.4 $ Tree Parsing $Revision: 1.4 $ Compiler Tools Group Department of Electrical and Computer Engineering University of Colorado Boulder, CO, USA 80309-0425 i Table of Contents 1 The Tree To Be Parsed.........................

More information

Application: Programming Language Semantics

Application: Programming Language Semantics Chapter 8 Application: Programming Language Semantics Prof. Dr. K. Madlener: Specification and Verification in Higher Order Logic 527 Introduction to Programming Language Semantics Programming Language

More information

Project 5 Due 11:59:59pm Tuesday, April 25, 2017

Project 5 Due 11:59:59pm Tuesday, April 25, 2017 Project 5 Due 11:59:59pm Tuesday, April 25, 2017 Introduction In this project, you will write a compiler for a programming language called Rube, which is a small objectoriented programming language with

More information

2. Reachability in garbage collection is just an approximation of garbage.

2. Reachability in garbage collection is just an approximation of garbage. symbol tables were on the first exam of this particular year's exam. We did not discuss register allocation in this This exam has questions from previous CISC 471/672. particular year. Not all questions

More information

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS CIS 341 Midterm February 28, 2013 Name (printed): Pennkey (login id): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in completing this

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

Compilers and computer architecture From strings to ASTs (2): context free grammars

Compilers and computer architecture From strings to ASTs (2): context free grammars 1 / 1 Compilers and computer architecture From strings to ASTs (2): context free grammars Martin Berger October 2018 Recall the function of compilers 2 / 1 3 / 1 Recall we are discussing parsing Source

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics. One-Slide Summary. Lecture Outline Operational Semantics #1 One-Slide Summary Operational semantics are a precise way of specifying how to evaluate a program. A formal semantics tells you what each expression means. Meaning depends on context:

More information

Pointers II. Class 31

Pointers II. Class 31 Pointers II Class 31 Compile Time all of the variables we have seen so far have been declared at compile time they are written into the program code you can see by looking at the program how many variables

More information

Running Haskell on the CLR

Running Haskell on the CLR Running Haskell on the CLR but does it run on Windows? Jeroen Leeuwestein, Tom Lokhorst jleeuwes@cs.uu.nl, tom@lokhorst.eu January 29, 2009 Don t get your hopes up! Don t get your hopes up! module Main

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 27 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 21 November, 2011 1 / 19 1 2 3 4 2 / 19 Semantics for programming

More information

Harvard School of Engineering and Applied Sciences Computer Science 152

Harvard School of Engineering and Applied Sciences Computer Science 152 Harvard School of Engineering and Applied Sciences Computer Science 152 Lecture 17 Tuesday, March 30, 2010 1 Polymorph means many forms. Polymorphism is the ability of code to be used on values of different

More information

Running Haskell on the CLR

Running Haskell on the CLR Running Haskell on the CLR but does it run on Windows? Jeroen Leeuwestein, Tom Lokhorst jleeuwes@cs.uu.nl, tom@lokhorst.eu January 29, 2009 Don t get your hopes up! module Main where foreign import ccall

More information

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1 Natural Semantics Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1 1 Natural deduction is an instance of first-order logic; that is, it is the formal

More information

CS4120/4121/5120/5121 Spring 2018 Xi Type System Specification Cornell University Version of February 18, 2018

CS4120/4121/5120/5121 Spring 2018 Xi Type System Specification Cornell University Version of February 18, 2018 CS4120/4121/5120/5121 Spring 2018 Xi Type System Specification Cornell University Version of February 18, 2018 0 Changes 2/16: Added typing rule for empty blocks 1 Types The Xi type system uses a somewhat

More information

2.2 Syntax Definition

2.2 Syntax Definition 42 CHAPTER 2. A SIMPLE SYNTAX-DIRECTED TRANSLATOR sequence of "three-address" instructions; a more complete example appears in Fig. 2.2. This form of intermediate code takes its name from instructions

More information

ECS 120 Lesson 7 Regular Expressions, Pt. 1

ECS 120 Lesson 7 Regular Expressions, Pt. 1 ECS 120 Lesson 7 Regular Expressions, Pt. 1 Oliver Kreylos Friday, April 13th, 2001 1 Outline Thus far, we have been discussing one way to specify a (regular) language: Giving a machine that reads a word

More information

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich From IMP to Java Andreas Lochbihler ETH Zurich parts based on work by Gerwin Klein and Tobias Nipkow 2015-07-14 1 Subtyping 2 Objects and Inheritance 3 Multithreading 1 Subtyping 2 Objects and Inheritance

More information

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

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8] Code No: R05311201 Set No. 1 1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8] 2. Compute the FIRST and FOLLOW sets of each

More information

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1 CSE P 501 Compilers Intermediate Representations Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 G-1 Administrivia Semantics/types/symbol table project due ~2 weeks how goes it? Should be caught up on

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

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

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 27 Alex Simpson School of Informatics University of Edinburgh als@inf.ed.ac.uk 18 November, 2014 1 / 18 Two parallel pipelines A large proportion

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #34 Function with pointer Argument (Refer Slide Time: 00:05) So, here is the stuff that we have seen about pointers.

More information

Program Syntax; Operational Semantics

Program Syntax; Operational Semantics 9/5 Solved Program Syntax; Operational Semantics CS 536: Science of Programming, Fall 2018 A. Why Our simple programming language is a model for the kind of constructs seen in actual languages. Step-by-step

More information

The Design of Core C++ (Notes)

The Design of Core C++ (Notes) The Design of Core C++ (Notes) Uday Reddy May 13, 1994 This note is to define a small formal language called Core C++ which reflects the essential structure of C++. As the name implies, the design only

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 28 Mary Cryan School of Informatics University of Edinburgh mcryan@inf.ed.ac.uk 21 November 2018 1 / 18 Two parallel pipelines A large proportion

More information

The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

More information

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

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013 Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 1 Tuesday, January 29, 2013 1 Intro to semantics What is the meaning of a program? When we write a program, we use

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

Syntax and Grammars 1 / 21

Syntax and Grammars 1 / 21 Syntax and Grammars 1 / 21 Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types What is a language? 2 / 21 What is a language?

More information

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

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1 Midterm 2 Solutions 1. [20 points] Consider the language that consist of possibly empty lists of the identifier x enclosed by parentheses and separated by commas. The language includes { () (x) (x,x) (x,x,x)

More information

CSCE 531, Spring 2015 Final Exam Answer Key

CSCE 531, Spring 2015 Final Exam Answer Key CSCE 531, Spring 2015 Final Exam Answer Key 1. (40 points total) Consider the following grammar with start symbol S : S S S asb S T T T a T cs T ɛ (a) (10 points) Find FIRST(S), FIRST(T ), FOLLOW(S), and

More information

Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing Ambiguity and Syntax Errors Introduction to Parsing Ambiguity and Syntax rrors Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity Syntax errors Compiler Design 1 (2011) 2 Languages

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

CSE P 501 Exam 8/5/04

CSE P 501 Exam 8/5/04 Name There are 7 questions worth a total of 65 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. You may refer to the following references: Course

More information

Induction and Semantics in Dafny

Induction and Semantics in Dafny 15-414 Lecture 11 1 Instructor: Matt Fredrikson Induction and Semantics in Dafny TA: Ryan Wagner Encoding the syntax of Imp Recall the abstract syntax of Imp: a AExp ::= n Z x Var a 1 + a 2 b BExp ::=

More information

Compiler construction 2009

Compiler construction 2009 Compiler construction 2009 Lecture 2 Code generation 1: Generating Jasmin code JVM and Java bytecode Jasmin Naive code generation The Java Virtual Machine Data types Primitive types, including integer

More information

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

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F). CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

More information

SOFTWARE ENGINEERING DESIGN I

SOFTWARE ENGINEERING DESIGN I 2 SOFTWARE ENGINEERING DESIGN I 3. Schemas and Theories The aim of this course is to learn how to write formal specifications of computer systems, using classical logic. The key descriptional technique

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations

More information

1 Finite Representations of Languages

1 Finite Representations of Languages 1 Finite Representations of Languages Languages may be infinite sets of strings. We need a finite notation for them. There are at least four ways to do this: 1. Language generators. The language can be

More information

Problem with Scanning an Infix Expression

Problem with Scanning an Infix Expression Operator Notation Consider the infix expression (X Y) + (W U), with parentheses added to make the evaluation order perfectly obvious. This is an arithmetic expression written in standard form, called infix

More information

Project 3 Due October 21, 2015, 11:59:59pm

Project 3 Due October 21, 2015, 11:59:59pm Project 3 Due October 21, 2015, 11:59:59pm 1 Introduction In this project, you will implement RubeVM, a virtual machine for a simple bytecode language. Later in the semester, you will compile Rube (a simplified

More information

Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing Ambiguity and Syntax Errors Introduction to Parsing Ambiguity and Syntax rrors Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity Syntax errors 2 Languages and Automata Formal

More information

CS143 Final Fall 2009

CS143 Final Fall 2009 CS143 Final Fall 2009 Please read all instructions (including these) carefully. There are 4 questions on the exam, all with multiple parts. You have 2 hours to work on the exam. The exam is closed book,

More information

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007 CSCI 3155: Principles of Programming Languages Exam preparation #1 2007 Exercise 1. Consider the if-then-else construct of Pascal, as in the following example: IF 1 = 2 THEN PRINT X ELSE PRINT Y (a) Assume

More information

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code Code Generation Intermediate Code? Assembly Code Object Code (Machine Code) 1 Intermediate Code P-Code Three Address Code 2 Compiler Construction F6S 1 Intermediate Representation Abstract Syntax Tree

More information

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

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer. The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees

More information

Lecture 3: Recursion; Structural Induction

Lecture 3: Recursion; Structural Induction 15-150 Lecture 3: Recursion; Structural Induction Lecture by Dan Licata January 24, 2012 Today, we are going to talk about one of the most important ideas in functional programming, structural recursion

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 1994-2016 Values and Types We divide the universe of values according to types A type is a set of values and a

More information

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2 Banner number: Name: Midterm Exam CSCI 336: Principles of Programming Languages February 2, 23 Group Group 2 Group 3 Question. Question 2. Question 3. Question.2 Question 2.2 Question 3.2 Question.3 Question

More information

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

Project 6 Due 11:59:59pm Thu, Dec 10, 2015 Project 6 Due 11:59:59pm Thu, Dec 10, 2015 Updates None yet. Introduction In this project, you will add a static type checking system to the Rube programming language. Recall the formal syntax for Rube

More information

Problem with Scanning an Infix Expression

Problem with Scanning an Infix Expression Operator Notation Consider the infix expression (X Y) + (W U), with parentheses added to make the evaluation order perfectly obvious. This is an arithmetic expression written in standard form, called infix

More information

CS558 Programming Languages. Winter 2013 Lecture 3

CS558 Programming Languages. Winter 2013 Lecture 3 CS558 Programming Languages Winter 2013 Lecture 3 1 NAMES AND BINDING One essential part of being a high-level language is having convenient names for things: variables constants types functions etc. classes

More information

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC 330: Organization of Programming Languages. Operational Semantics CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged

More information

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS 164 Spring 2009 P. N. Hilfinger CS 164: Final Examination (corrected) Name: Login: You have

More information

Writing Evaluators MIF08. Laure Gonnord

Writing Evaluators MIF08. Laure Gonnord Writing Evaluators MIF08 Laure Gonnord Laure.Gonnord@univ-lyon1.fr Evaluators, what for? Outline 1 Evaluators, what for? 2 Implementation Laure Gonnord (Lyon1/FST) Writing Evaluators 2 / 21 Evaluators,

More information

Operational Semantics of Cool

Operational Semantics of Cool Operational Semantics of Cool Key Concepts semantics: the meaning of a program, what does program do? how the code is executed? operational semantics: high level code generation steps of calculating values

More information

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford) Programming Languages & Translators PARSING Baishakhi Ray Fall 2018 These slides are motivated from Prof. Alex Aiken: Compilers (Stanford) Languages and Automata Formal languages are very important in

More information

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java Chapter 3 Syntax, Errors, and Debugging Objectives Construct and use numeric and string literals. Name and use variables and constants. Create arithmetic expressions. Understand the precedence of different

More information

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far Lecture Outline Operational Semantics of Cool Lecture 13 COOL operational semantics Motivation Notation The rules Prof. Aiken CS 143 Lecture 13 1 Prof. Aiken CS 143 Lecture 13 2 Motivation We must specify

More information

Chapter 2 A Quick Tour

Chapter 2 A Quick Tour Chapter 2 A Quick Tour 2.1 The Compiler Toolchain A compiler is one component in a toolchain of programs used to create executables from source code. Typically, when you invoke a single command to compile

More information

Homework 3 COSE212, Fall 2018

Homework 3 COSE212, Fall 2018 Homework 3 COSE212, Fall 2018 Hakjoo Oh Due: 10/28, 24:00 Problem 1 (100pts) Let us design and implement a programming language called ML. ML is a small yet Turing-complete functional language that supports

More information

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1 Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1 1. Introduction Parsing is the task of Syntax Analysis Determining the syntax, or structure, of a program. The syntax is defined by the grammar rules

More information

MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011

MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011 MA53: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 8 Date: September 2, 20 xercise: Define a context-free grammar that represents (a simplification of) expressions

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

Assignment 4: Semantics

Assignment 4: Semantics Assignment 4: Semantics 15-411: Compiler Design Jan Hoffmann Jonathan Burns, DeeDee Han, Anatol Liu, Alice Rao Due Thursday, November 3, 2016 (9:00am) Reminder: Assignments are individual assignments,

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1 CS321 Languages and Compiler Design I Winter 2012 Lecture 1 1 COURSE GOALS Improve understanding of languages and machines. Learn practicalities of translation. Learn anatomy of programming languages.

More information

[0569] p 0318 garbage

[0569] p 0318 garbage A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the

More information

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

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4020 July 5, 2007 14.00-15.30 This exam (8 pages) consists of 60 True/False

More information

UNIT-4 (COMPILER DESIGN)

UNIT-4 (COMPILER DESIGN) UNIT-4 (COMPILER DESIGN) An important part of any compiler is the construction and maintenance of a dictionary containing names and their associated values, such type of dictionary is called a symbol table.

More information

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI UNIT I - LEXICAL ANALYSIS 1. What is the role of Lexical Analyzer? [NOV 2014] 2. Write

More information

Programming Languages and Compilers Qualifying Examination. Answer 4 of 6 questions.

Programming Languages and Compilers Qualifying Examination. Answer 4 of 6 questions. Programming Languages and Compilers Qualifying Examination Fall 2017 Answer 4 of 6 questions. GENERAL INSTRUCTIONS 1. Answer each question in a separate book. 2. Indicate on the cover of each book the

More information

CIS 341 Final Examination 4 May 2017

CIS 341 Final Examination 4 May 2017 CIS 341 Final Examination 4 May 2017 1 /14 2 /15 3 /12 4 /14 5 /34 6 /21 7 /10 Total /120 Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam. There are 14 pages

More information

Qualifying Exam in Programming Languages and Compilers

Qualifying Exam in Programming Languages and Compilers Qualifying Exam in Programming Languages and Compilers University of Wisconsin Fall 1991 Instructions This exam contains nine questions, divided into two parts. All students taking the exam should answer

More information

Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta

Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta Topic 6: Types COS 320 Compiling Techniques Princeton University Spring 2015 Prof. David August 1 Adapted from slides by Aarne Ranta Types What is a type? Type Checking: Helps find language-level errors:

More information

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1 Defining Program Syntax Chapter Two Modern Programming Languages, 2nd ed. 1 Syntax And Semantics Programming language syntax: how programs look, their form and structure Syntax is defined using a kind

More information

A Simple Syntax-Directed Translator

A Simple Syntax-Directed Translator Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called

More information