Project 2 Interpreter for Snail. 2 The Snail Programming Language

Similar documents
ffl assign The assign has the form: identifier = expression; For example, this is a valid assign : var1 = 20-3*2; In the assign the identifier gets th

ffl assign : The assign has the form: identifier = expression ; For example, this is a valid assign : var1 = 20-3*2 ; In the assign the identifier get

Syntax. A. Bellaachia Page: 1

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

Context-free grammars (CFG s)

Chapter 3. Describing Syntax and Semantics ISBN

A Simple Syntax-Directed Translator

CSE 340 Fall 2014 Project 4

SOFTWARE ARCHITECTURE 5. COMPILER

2 Input and Output The input of your program is any file with text. The output of your program will be a description of the strings that the program r

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

Yacc: A Syntactic Analysers Generator

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

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

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

The SPL Programming Language Reference Manual

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Programming Language Syntax and Analysis

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

CS 6353 Compiler Construction Project Assignments

Using an LALR(1) Parser Generator

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

GAWK Language Reference Manual

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Syntax. In Text: Chapter 3

COP 3402 Systems Software Syntax Analysis (Parser)

CPS 506 Comparative Programming Languages. Syntax Specification

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

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

Lexical and Syntax Analysis (2)

SECOND PUBLIC EXAMINATION. Compilers

DEMO A Language for Practice Implementation Comp 506, Spring 2018

Syntax Errors; Static Semantics

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

COP4020 Programming Assignment 1 CALC Interpreter/Translator Due March 4, 2015

Syntax Analysis Check syntax and construct abstract syntax tree

Parser Generators. Mark Boady. August 14, 2013

Compiler Techniques MN1 The nano-c Language

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

Compiler Construction Assignment 3 Spring 2018

CHAD Language Reference Manual

Preparing for the ACW Languages & Compilers

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

Class Information ANNOUCEMENTS

YOLOP Language Reference Manual

Exercise ANTLRv4. Patryk Kiepas. March 25, 2017

CSE P 501 Exam 11/17/05 Sample Solution

Programming Language Definition. Regular Expressions

2.2 Syntax Definition

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

The PCAT Programming Language Reference Manual

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Monday, September 13, Parsers

Problem Score Max Score 1 Syntax directed translation & type

Introduction to Parsing Ambiguity and Syntax Errors

Syntax Analysis Part I

Yacc Yet Another Compiler Compiler

YAGL: Yet Another Graphing Language Language Reference Manual

Introduction to Lexing and Parsing

The Decaf Language. 1 Lexical considerations

Introduction to Parsing Ambiguity and Syntax Errors

4. Lexical and Syntax Analysis

CS 6353 Compiler Construction Project Assignments

CS360: Mini Language. Michael Conway Original slides by Mark Boady. March 11, 2015

ICOM 4036 Spring 2004

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

Chapter 4. Lexical and Syntax Analysis

Introduction to Parsing. Lecture 8

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

COMPILERS BASIC COMPILER FUNCTIONS

EECS483 D1: Project 1 Overview

4. Lexical and Syntax Analysis

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


A Short Summary of Javali

LL(k) Compiler Construction. Choice points in EBNF grammar. Left recursive grammar

SFU CMPT 379 Compilers Spring 2018 Milestone 1. Milestone due Friday, January 26, by 11:59 pm.

Wednesday, August 31, Parsers

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

LL(k) Compiler Construction. Top-down Parsing. LL(1) parsing engine. LL engine ID, $ S 0 E 1 T 2 3

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Part 5 Program Analysis Principles and Techniques

The Decaf language 1

1 Lexical Considerations

THE PROGRAMMING LANGUAGE NINC PARSER PROJECT. In this project, you will write a parser for the NINC programming language. 1.

COP4020 Programming Assignment 2 - Fall 2016

Syntax-Directed Translation

Parser and syntax analyzer. Context-Free Grammar Definition. Scanning and parsing. How bottom-up parsing works: Shift/Reduce tecnique.

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Transcription:

CSCI 2400 Models of Computation Project 2 Interpreter for Snail 1 Overview In this assignment you will use the parser generator yacc to construct an interpreter for a language called Snail containing the set of s listed below. The interpreter executes the s in a Snail program, one after the other in the order which they appear in the Snail program. In Section 2 we describe the various s. In Section 3 we give the grammar of the Simple language. In Section 4 we describe what your yacc code will do. In Section 5 are instructions to hand-in your assignment. 2 The Snail Programming Language Snail is a very simple programming language. The body of a Snail program consists of a sequence of s. There are three kinds of s: assign, print, and if. A basic component for all kinds of s is the expression. The expression and the s are described below. expression An expression is any mathematical expression made from identifiers, integers, parenthesis, the arithmetic operators + - * / and the comparison operators < > <= >= ==!= For example, this is a valid expression: 10 + 20 * (10 < 3) The value of an expression is obtained by executing all the arithmetic operations in the expression. The result of a comparison operation is 1 if the comparison result is true, and 0 otherwise. For example, the above expression has value 10 (since, 10 < 3 = 0). The value of an identifier is the last value assigned to it in an assign. An identifier which hasn t been assigned a value before cannot be used inside an expression and in this case you should report an error message. 1

assign The assign has the form: identifier = expression; For example, this is a valid assign : var1 = 20-3*2; In the assign the identifier gets the value of the expression. As an example, in the above assign the new value of variable var1 is 14. print The print print messages on the screen. The print has one of following forms: print string ; print newline; print expression; //prints the string //prints a newline character //prints the expression value For example, the execution of the following s print The value of 10*5 is ; print 10*5; produces the output: The value of 10*5 is 50 if An if has one form: if expression then //if-then-... //more s... //more s The if-then- means that if the expression value is not 0 then the s between then and will be executed, and otherwise, if the expression value is 0, the s between and will be executed. For example, the following is a valid if : 2

if (x < 10) then print x is smaller than ten ; x = x - y + 20; x = 10* y; A Snail program is a sequence of s and has the following general form:... We can have comments in a Snail program right after // (as in a C++ program). An simple example Snail program is the following: x = 10; y = 20; print the value of x is ; print x; print newline; print the value of y is ; print y; print newline; print the sum of x and y is ; print x + y; print newline; if x < y then print x is smaller than y ; print x is bigger or equal than y //test x < y // x is smaller // x is not smaller The output of the program is: the value of x is 10 the value of y is 20 the sum of x and y is 30; x is smaller than y; 3 Snail Grammar All the Snail programs can be described by the context-free grammar given below. The start variable is program, the grammar variables are in small letters, and the terminals in capital letters. Notice that this grammar is ambiguous in the expr variable; however, all the ambiguities can be removed using the precedence rules of yacc. program -> stmt_list 3

stmt_list -> stmt_list stmt stmt stmt -> assign_stmt print_stmt if_stmt assign_stmt -> ID = expr ; print_stmt -> PRINT expr ; PRINT STRING ; PRINT NEWLINE ; if_stmt -> IF expr THEN stmt_list ELSE stmt_list ENDIF expr -> ( expr ) expr + expr expr - expr expr * expr expr / expr expr < expr expr > expr expr <= expr expr >= expr expr == expr expr!= expr - expr INT ID 4 Yacc Code You will write a yacc code which implements the interpreter for Snail programs. The main part of your yacc code will consist of the snail grammar augmented with actions. expression Whenever you find an expression, you need to evaluate the expression. This can be done in a bottom up fashion with actions such as: expr : expr + expr {$$ = $1 + $3;} assign The value of an ID (identifier) node is stored in the symbol table. For an assign node you need to update the value of the variable to be equal to the expr of the right hand side of the assign. 4

print For a print node you just print on the output the contents of the STRING stripped from quotes, or the value of the expr, or a newline character. if For the if you first evaluate the expr. If the value of expr is true (not zero), then you execute the if part of the, otherwise you execute the part of the. You need to be careful not to execute the part of the if you don t want to execute. In the following example, when expression is true then only 1 will be executed, while when expression is false then only 2 will be executed. if expression then 1 2 In order to achieve this, you need to have a stack which will control whether or not to execute particular s. We push true for the part of the if which will be executed; for the other part we push false. The stack is needed in order to handle nested if s. The code to handle this is as follows: if_stmt : IF expr THEN {push($2!= 0);} stmt_list {pop();} ELSE {push($2 == 0);} stmt_list {pop();} ENDIF If the top of the stack contains true then s can be executed and expressions can be evaluated (these s and expressions are inside the part of an if that will be executed). For example, the actions of the plus expression should be modified as: expr : expr + expr {if (top() == 1) $$ = $1 + $3;} In order to handle s and expressions in the main body, outside from if s, you need to push true at the beginning of the execution of the interpreter. If you find a syntax error in the input program then report an error message with the line number where you found the error and abort the program. Your yacc program will use parts of the the lexical analyzer you built in the first project and for this you need to modify appropriately the lex code. 5

5 Hand-In You need to submit your lex and yacc program through cs web submission. In the course web page you can also find example yacc programs (together with lex programs) that will help you to get started with your project. 6