Mandatory Exercise 1 INF3110

Similar documents
Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Lexical Considerations

1 Lexical Considerations

CA4003 Compiler Construction Assignment Language Definition

Lexical Considerations

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

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Lecture 12: Conditional Expressions and Local Binding

September 18, B Math Test Chapter 1 Name: x can be expressed as: {y y 0, y R}.

Computational Expression

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

Mandatory Assignment 1, INF 4130, 2017

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Fall 2017 CISC124 9/16/2017

Introduction to Computer Science Unit 2. Exercises

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

Compilers Project 3: Semantic Analyzer

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

Syntax Errors; Static Semantics

Syntax and Grammars 1 / 21

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Array. Prepared By - Rifat Shahriyar

LECTURE 3. Compiler Phases

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Decaf Language Reference Manual

3. Java - Language Constructs I

Assignment 4: Dodo gets smarter

Compiler construction

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

Time : 1 Hour Max Marks : 30

Program Representations

Intermediate Code Generation

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Lecture 16: Static Semantics Overview 1

CSE P 501 Exam 8/5/04

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Compiler construction

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

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

DM550 Introduction to Programming part 2. Jan Baumbach.

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

Building an IDE with Rascal

Model Solutions. COMP 102: Test March, 2015

A Short Summary of Javali

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

Object Oriented Programming. Java-Lecture 6 - Arrays

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

Principles of Programming Languages

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

Chapter 3. Describing Syntax and Semantics

A simple syntax-directed

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Following is the general form of a typical decision making structure found in most of the programming languages:

Controls Structure for Repetition

Programming with Java

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Semantic Analysis. Compiler Architecture

Lesson 12: The Graph of the Equation y = f(x)

Sprite an animation manipulation language Language Reference Manual

CSE-304 Programming Assignment #4

Flang typechecker Due: February 27, 2015

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

REFERENCE MATERIALS. Assignment, Display, and Input Evaluates expression and assigns the result to the variable a.

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Before submitting the file project5.py, check carefully that the header above is correctly completed:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Lecture 15 CIS 341: COMPILERS

The SPL Programming Language Reference Manual

3.7 Denotational Semantics

Foundations: Syntax, Semantics, and Graphs

Context-free grammars (CFG s)

COMP 202 Java in one week

CSE 3302 Programming Languages Lecture 2: Syntax

Introduction to Programming Using Java (98-388)

Name Homework Packet Week #12

COMP520 - GoLite Type Checking Specification

ASML Language Reference Manual

CSE 401 Final Exam. December 16, 2010

A Simple Syntax-Directed Translator

February 13, notebook

2.2 Syntax Definition

String Computation Program

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

BFH/HTA Biel/DUE/Course 355/ Software Engineering 2

CSE 5317 Midterm Examination 4 March Solutions

Tree-based abstract syntax

Functional Programming. Pure Functional Programming

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Full file at

CS 320: Concepts of Programming Languages

Operational Semantics. One-Slide Summary. Lecture Outline

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

CS115 - Module 10 - General Trees

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN

Transcription:

Mandatory Exercise 1 INF3110 In this exercise, you are going to write a small interpreter for a simple language for controlling a robot on a 2-dimensional grid. The language is called ROBOL, a clever acronym for ROBOT LANGUAGE, and its grammar is defined below. The grid on which the robot can move about is defined by its x and y bounds, for instance: 0,6 7,6 0,0 7,0 The grid above is defined by the bound (7, 6), and the robot is currently located at position (3, 3). Moving the robot 1 step north would put it at (3,4). Moving it one step east would put it at (4,3), etc. Assignment Make an interpreter for the ROBOL language in your object-oriented language of choice (e.g. Java or Python. If you want to chose another language, please ask the teaching assistants). The interpreter shall operate on an abstract syntax tree (AST) representing a ROBOL program. You do not need to write a scanner or a parser for the language (you can assume that some benevolent entity has already written those for you). You can design the classes for the AST as you like, but they should provide a somewhat faithful representation of the grammar listed below. The outermost element program from the grammar should be represented by a class Program, that provides an interpret-method which, when called, will interpret the entire program.

Requirements: - The interpreter should be easy to extend (for your own sake too, as you will have to later on). - The interpreter must check that the poor robot does not fall off the edge of the world (i.e., moves beyond the bounds of the grid). - You can display the state of the program in any form you like during execution, but at minimum, the program should, upon termination, print its state in the form of the current location of the robot. - There are some example programs below. You should check that your implementation returns the correct result after running these programs, and include instructions on how to run their AST representations with your implementation. - Write a design document that explains how you have implemented the interpreter, and why you have done it in this way. Furthermore, the document should explain how to run your program. If you have used an IDE, make sure to describe this, and include instructions on how to run your program in this environment. Deliverables - The entire program and the design document should be placed in a single.zip file - The name of the file should be INF3110_Mandatory1_<username>.zip - The submission is done through Devilry: https://devilry.ifi.uio.no/

ROBOL Grammar // a program consists of a robot, and a grid on which it can move around <program> ::= <grid> <robot> // size of the grid given as a bound for the x axis and the y axis; both axes // start at 0, number is a positive integer. <grid> ::= size (<number> * <number>) // the robot has a list of variable declarations, a starting point, and a // a set of statements that control its movement <robot> ::= <var-decl>* <start> <stmt>* // a variable declaration consists of a name and an initial value <var-decl> ::= var <identifier> = <exp> // statements control the robot s movement <stmt> ::= <> <move> <exp> <assignment> <while> // start gives the initial position for the robot <start> ::= start (<exp>, <exp>) <> ::= // on the grid, moving north means up along the y axis, east means to the right // along the x axis, etc. <move> ::= north south east west <assignment> ::= <identifier> = <exp> <while> ::= while <boolean-exp> { <stmt>+ // expressions; number is an integer, identifier is a string of // letters and numbers, starting with a letter <exp> ::= <identifier> <number> (<exp>) <arithmetic-exp> <boolean-exp> //prefix notation. (< 1 2) should be evaluated as 1 < 2 <Boolean-exp> ::= (<operator> <exp> <exp>) <Boolean-op> ::= < > =

//prefix notation. (+ 1 2 3) should be evaluated as 1 + 2 + 3 <arithmetic-exp> ::= Legend: (<arithmetic-op> <args>) <non-terminal> terminal <arithmetic-op> ::= + - * //at least 2 arguments, but could be more <args> ::= <exp> <exp> <exp>* Hints, program sketch and example programs Hints: - You may assume that expressions are type-correct (so you do not have to implement a type checker). You can assume that no-one writes programs that tries to add Booleans and numbers, for instance. - It might simplify things if all expressions can calculate an integer value. Boolean expressions can, for instance, return 1 for true and 0 for false. - The robot probably needs to have a reference to the grid, and the statements probably need to have a reference to the robot. This can be achieved in many ways, choose one that fits with your overall design. Program sketch: Below is a Java sketch of an implementation of the interpreter. You can use this as a starting point for your own implementation, if you like. You may also change all of these definitions if you think that is necessary. public interface Robol { void interpret(); class Program implements Robol { Grid grid; Robot robot; public Program(Grid grid, Robot robot) { this.grid = grid; this.robot = robot; robot.interpret(); class Robot implements Robol { // write interpreter code for the robot here

abstract class Statement implements Robol { public abstract void interpret(); class Assignment extends Statement { // write interpreter code here class While extends Statement { BoolExp condition; List<Statement> statements; // write interpreter code here abstract class Expression { abstract class BoolExp extends Expression { protected Expression first; protected Expression second; Example programs: Testing Code 1: Simple Example size (64*64) start(23,30) west 15 south 15 east (+ 2 3) north (+ 10 27) The result is (13, 52) Testing Code 2: Example with variables size(64*64) var i = 5

var j = 3 start(23,6) north (* 3 i) east 15 south 4 west (+ (* 2 i) (* 3 j) 5) The result is (14, 17) Testing Code 3: Example with while loop and assignment size(64*64) var i = 5 var j = 3 start(23,6) north (* 3 i) west 15 east 4 while(> j 0) { south j j = (- j 1) The result is (12, 15) Testing Code 4: Example with movement over the edge size(64*64) var j = 3 start(1,1) while(> j 0) { north j The result should be an error saying that the bounds of the grid have been overstepped.