INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Procedures and Control Flow CS351 – Programming Paradigms.
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
Compilation Encapsulation Or: Why Every Component Should Just Do Its Damn Job.
CS 536 Spring Run-time organization Lecture 19.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Run-time Environment and Program Organization
Templates. Class templates – why? Writing programs we often use abstract data types such as stack, queue or tree. Implementations of these types may be.
EE694v-Verification-Lect5-1- Lecture 5 - Verification Tools Automation improves the efficiency and reliability of the verification process Some tools,
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
INF5110: Mandatory Exercise 1 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
1 Project 7: Huffman Code. 2 Extend the most recent version of the Huffman Code program to include decode information in the binary output file and use.
Prof. Bodik CS 164 Lecture 51 Building a Parser I CS164 3:30-5:00 TT 10 Evans.
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Programming Languages
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
COMPILER CONSTRUCTION Lesson 1 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker (Erik Hansson.
CMSC 330: Organization of Programming Languages Java Generics.
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Compiler (scalac, gcc) Compiler (scalac, gcc) I = 0 while (i < 10) { i = i + 1 } I = 0 while (i < 10) { i = i + 1 } source code i d 3 = 0 LF w i d 3 =
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
CMSC 330: Organization of Programming Languages Operational Semantics.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Language Implementation Overview John Keyser Spring 2016.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
Java Programming Language
CS 326 Programming Languages, Concepts and Implementation
Semantic Analysis with Emphasis on Name Analysis
Overview of Compilation The Compiler BACK End
Systems Programming 3rd laboratory Test_driven Development
INF3110: Exercises Part 6 Object Orientation Comments and solutions
INF3110: Exercises Part 3 Comments and Solutions
Stack Data Structure, Reverse Polish Notation, Homework 7
Mini Language Interpreter Programming Languages (CS 550)
CMPE 152: Compiler Design October 4 Class Meeting
Overview of Compilation The Compiler BACK End
CMPE 152: Compiler Design October 4 Class Meeting
Eyvind W. Axelsen INF3110: Solutions to Problem Set 2 Scoping, Runtime Organisation, Parameter Passing Methods Eyvind W. Axelsen.
Types and Type Checking (What is it good for?)
CS 432: Compiler Construction Lecture 11
Presentation transcript:

INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on material from previous years, made by Henning Berg, Fredrik Sørensen, and others.

Main goals Determine if programs written in the language Compila15 are semantically valid – I.e. are they type-correct? (static semantics) Generate byte-code for Compila15(-ish) programs – Write a code generator

Last time You made – a Lexer – a Parser – an Abstract Syntax Tree This time we expand on this – Use your previous delivery!

Learning outcomes Understand how type checking can be done Understand what bytecode is, and how it can be generated from source code Extend an existing compiler code base with new functionality

Semantic Analysis/Type checking A parser cannot check all the properties of the language specification – Context-free grammars are not powerful enough Thus, we shall extend our compiler with a type checker – Use the AST classes you defined last time – Add type-checking code – You are allow to make any changes you want to your previous delivery

The Compila15 language at a(nother) glance program { class Complex { var Real : float; var Imag : float; } proc Add : Complex (a : Complex, b : Complex) { var retval : Complex; retval := new Complex; retval.Real := a.Real + b.Real; retval.Imag := a.Imag + b.Imag; return retval; } proc Main() { var c1 : Complex; var c2 : Complex; var result : Complex; … result := Add ( c1, c2 ); … return; } } Real and Imag are of the (built-in) float type Check that the + operator is compatible with it operands’ types, and that the assignment is legal. Check that the actual parameters to Add(…) are of the correct type, according to the formal parameters, and that the assignment to result is legal.

Type checking – example class IfStatement extends Statement { … public void typeCheck() { String condType = condition.getType(); if(condType != “bool”) { throw new TypeException(“condition in if- statement must be of type bool”); } Implement such a method in e.g. the various Expression classes

Type checking – example class Assignment extends Statement { … public void typeCheck() { String varType = var.getType(); String expType = exp.getType(); if(varType != expType && !isAssignmentCompatible(varType, expType)) { throw new TypeException(“cannot assign “ + vartype + “ from “ + expType); } Check supported type conversions, e.g. flpat to int

Code generation The lecture about code generation is not until April 20 th, – So, if this looks difficult now, don’t worry! Byte code API and operations are described in the document “Interpreter and bytecode for INF5110” – Available on the course page Add bytecode generation methods to your AST classes – Again, any changes you want to make to the structure is OK

Code generation - limitations The interpreter and bytecode library are somewhat limited – Cannot express full Compila15 – No block structures (only global and local variables) – No reference parameters You delivery should support generating correct bytecode for the Compila15 source code file RunMe.cmp – Available from the material on the course webpage

Code generation – creating a procedure CodeFile codeFile = new CodeFile(); // add the procedure by name first codeFile.addProcedure(”Main”); // then define it CodeProcedure main = new CodeProcedure( ”Main”, VoidType.TYPE, codeFile ); main.addInstruction( new RETURN() ); // then update it in the code file codeFile.updateProcedure( main );

Code generation - assignment //1: proc add : int (a: int, b : int ) { //2: var res : int; //3: res := a + b; // only bytecode for this line //4: return res; //5: } // push a onto the stack proc.addInstruction(new LOADLOCAL(proc.variableNumber("a"))); // push b onto the stack proc.addInstruction(new LOADLOCAL(proc.variableNumber("b"))); // perform addition with arguments on the stack proc.addInstruction(new ADD()); // pop result from stack, and store it in variable res proc.addInstruction(new STORELOCAL(proc.variableNumber("res")));

Code generation – writing to file String filename = “myfile.bin”; byte[] bytecode = codeFile.getBytecode(); DataOutputStream stream = new DataOutputStream( new FileOutputStream (filename)); stream.write(bytecode); stream.close();

Testing 42 supplied tests in test folder, for testing the type checker – Note: there was an error in one test, see (and thanks to sveinuf for finding the error!) Run tests with “ant test” Tests ending with “fail” are supposed to fail (i.e., they contain an erroneous program) – Compiler returns error code 2 for semantic failure 32 of the 42 tests must pass for the delivery to be successful

Provided source code Three example programs, including RunMe.cmp, that you’re going to compile Revised version of Compila.cmp (not really needed for this exercise) Revised source code, see next slide 42 test programs. Use these to verify your type checking implementation (and hand in a printout of the results with your delivery) NOTE: the error mentioned earlier is in this folder

Provided source code (the src folder)

DEADLINE May 3rd, 23:59 Don’t miss the deadline! – Extensions are only possible if you have an agreement with the student administration (studadm) – This time we must be stricter, because of deadlines for exam lists etc – Contact them if you are sick, etc. Even if you are not 100% finished, deliver what you have before the deadline

Deliverables Working type checker for Compila15 – Run the supplied tests Code generator for (a subset of) Compila15 – Test with RunMe.cmp Report – Front page with your name(s) and UiO user name(s) Work alone or in pairs. Groups of three can be allowed after an application. – Discussion of your solution, choices you’ve made and assumptions that you depend on – Printout of test run – Printout of bytecode from RunMe.cmp The code you supply must build with “ant” – Test your delivery on a UiO computer Deliver a zipped folder by to – Feel free to send questions at any time! – Read the exercise description thoroughly!