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

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 get

Project 2 Interpreter for Snail. 2 The Snail Programming Language

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

A Simple Syntax-Directed Translator

Syntax. A. Bellaachia Page: 1

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

Chapter 3. Describing Syntax and Semantics ISBN

Introduction to Parsing Ambiguity and Syntax Errors

2.2 Syntax Definition

Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing. Lecture 8

A programming language requires two major definitions A simple one pass compiler

An algorithm may be expressed in a number of ways:

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

CSE 340 Fall 2014 Project 4

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

Syntax Analysis Check syntax and construct abstract syntax tree

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

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

Context-free grammars (CFG s)

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

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Syntax-Directed Translation

YOLOP Language Reference Manual

The SPL Programming Language Reference Manual

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

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

Syntax. In Text: Chapter 3

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

CPS 506 Comparative Programming Languages. Syntax Specification

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

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

A simple syntax-directed

Class Information ANNOUCEMENTS

Context-Free Grammars

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

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

Programming Language Syntax and Analysis

A Short Summary of Javali

Scripting languages. shell programs are good for personal tools

Yacc: A Syntactic Analysers Generator

Compiler Techniques MN1 The nano-c Language

Tree visitors. Context classes. CS664 Compiler Theory and Design LIU 1 of 11. Christopher League* 24 February 2016

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

Programming Language Definition. Regular Expressions

Syntax Analysis Part IV

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

EECS483 D1: Project 1 Overview

The Decaf Language. 1 Lexical considerations

ICOM 4036 Spring 2004

Yacc Yet Another Compiler Compiler

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

TML Language Reference Manual

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Scripting languages. shell programs are good for personal tools

Over-simplified history of programming languages

CMSC 330: Organization of Programming Languages

Abstract Syntax Trees

CSCI312 Principles of Programming Languages!

Chapter 3. Describing Syntax and Semantics

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

The Decaf language 1

Principles of Programming Languages COMP251: Syntax and Grammars

YAGL: Yet Another Graphing Language Language Reference Manual

Over-simplified history of programming languages

Wednesday, August 31, Parsers

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

CMSC 330: Organization of Programming Languages. Context Free Grammars

Syntax Errors; Static Semantics

LECTURE 11. Semantic Analysis and Yacc

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

CMSC 330: Organization of Programming Languages

Lex & Yacc. by H. Altay Güvenir. A compiler or an interpreter performs its task in 3 stages:

Lexical and Syntax Analysis (2)

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

Wednesday, September 9, 15. Parsers

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

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

Intro To Parsing. Step By Step

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

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

CSE 3302 Programming Languages Lecture 2: Syntax

Last Time. What do we want? When do we want it? An AST. Now!

CMSC 330: Organization of Programming Languages

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

CSE 582 Autumn 2002 Exam 11/26/02

Exercise 1: Balanced Parentheses

Using an LALR(1) Parser Generator

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

CMSC 330: Organization of Programming Languages. Context Free Grammars

CSCI 2041: Advanced Language Processing

CS 403: Scanning and Parsing

Decaf Language Reference Manual

Principles of Programming Languages COMP251: Syntax and Grammars

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

Transcription:

CSCI 2400 Models of Computation Project 2 Yacc Return Day: Friday, November 30. Teams: You must form teams of 2 or 3 persons. 1 Overview In this assignment you will use the parser generator yacc to construct an interpreter for the Snail programming language which we describe below. The interpreter executes the s of a Snail program in sequence as they appear in the program. In Section 2 we describe the Snail programming language. In Section 3 we give the grammar of the Snail language. In Section 4 we describe what your yacc code will do. In Section 5 are instructions to hand-in your assignment. 2 Snail Programming Language Snail is a very simple programming language. The body of a Snail program consists from a sequence of s. There are four kinds of s: assign, print, if, and while. A basic component for all kinds of s is the expression. The expression and the s are described below. ffl 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

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 the value of the expression. As an example, in the above assign the new value of variable var1 is 14. ffl 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 ffl if An if has two forms: if expression then //if-then... //more s endif if expression then //if-then-else... //more s else... //more s endif 2

The if-then" means that if the expression value is not 0 then the sequence of s between then and endif will be executed. The if-then-else" means that if the expression value is not 0 then the s between then and else will be executed, and otherwise, if the expression value is 0, the s between else and endif will be executed. For example, the following is a valid if : if (x < 10) then print ``x is smaller than ten''; x = x - y + 20; else x = 10* y; endif ffl while A while has the following form: while expression do... endwhile The while implements a loop which executes the s between do and endwhile for as long as the expression value is not 0. As an example the following while will iterate for five times: v = 1; while v <= 5 do print v; // print the value of v print newline; v = v + 1; // increase v by 1 endwhile 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: 3

v = 10; i = 0; while i <= v do print i*i; // print the square of i if i == v/2 then // is i the half of v? print newline; //yes else print ``--''; //no endif i = i + 1; endwhile print newline; print ``end of execution''; print newline; The output of the program is: 0--1--4--9--16--25 36--49--64--81--100-- end of execution 3 Snail Grammar All the Snail programs can be described by the the context-free grammar of Figure 1. The start variable is program, the grammar variables are in small letters, and the terminals in capital letters. Notice that although this grammar is ambiguous in the expr variable, all the ambiguities can be removed using the precedence rules of yacc. 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 from the snail grammar. You will add actions to the grammar so that your interpreter does the following for any input Snail program: 1. builds the derivation tree of the program, and then 2. executes" the derivation tree. The derivation tree (see Chapter 5 in Book) has a node for each variable and terminal. At the root of the tree is the variable program. An example Snail program and derivation tree is shown in Figure 2. 4

program -> stmt_list stmt_list -> stmt_list stmt stmt stmt -> assign_stmt print_stmt if_stmt while_stmt assign_stmt -> ID = expr ; print_stmt -> PRINT expr ; PRINT string ; PRINT NEWLINE ; if_stmt -> IF expr THEN stmt_list ENDIF IF expr THEN stmt_list ELSE stmt_list ENDIF while_stmt -> WHILE expr DO stmt_list ENDWHILE 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 Figure 1: The snail grammar 5

Program: print 10+5; derivation tree program stmt_list stmt print_stmt print expr ; expr + expr 10 5 Figure 2: A small Snail program and its derivation tree 6

You will build the tree bottom up starting from the leaves. (The reason of the bottom up construction is that yacc gives a bottom up parser.) To build the derivation tree you need a special routine, e.g. add node, which you will invoke at each production of your grammar and will add a node (or nodes) in the derivation tree. Your nodes of your tree must be general enough to accommodate all the different kinds of productions, variables and terminals in the grammar. You need a mechanism to distinguish between the various kinds of nodes. (for example, you can have avariable kind inside each node). You don't need to have a node for each terminal in your grammar, e.g. you don't need nodes for semicolumns ';'. By executing the tree" we mean that we traverse the tree recursively from the root to the leaves and execute the code that corresponds to each nodeof the tree. For this you will need to write a special routine, e.g. execute tree, which you will invoke after you build the derivation tree. (Normally you will invoke the execute tree routine at an action of the program variable of your grammar.) The main part of execute tree is a big switch for the various kinds of nodes. The pseudo-code for execute tree is as follows: // tree is the derivation tree execute_tree(tree) root = root(tree); left_child = root.left_child; middle_child = root.middle_child right_child = root.right_child; switch (root.kind) case expr_plus: execute_tree(left_child); execute_tree(right_child); root.value = left_child.value + right_child.value; case expr_times:...... case print_string: execute_tree(middle_child); printf(``%s'', middle_child.value); case print_expr : execute_tree(middle_child); printf(``%d'', middle_child.value);... } } 7

Each expr node must have a value variable which holds the current value of the expression. The execute tree routine computes the expr values recursively, by computing the values of the children expr first. The value of an ID (identifier) node can be stored in the symbol table. For an assign node we update the value of the ID child in the symbol table to be equal to the value of the expr child. For a print node we just print the contents (or value) of the middle child, which can be either a STRING, a expr or NEWLINE. For an if node, we first execute the expr child and then if the value of expr is not 0 we execute the if-then stmt list child. Otherwise, we execute the if-else stmt list child. For a while node, we repeatedly do the following: first we execute the expr child and if the value of expr is not 0 we execute the stmt list node. When the value of expr is zero the execution of the while node has finished. 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 the lexical analyzer of the first project and for this you need to modify appropriately the lex code. 5 Hand-In You should hand-in in paper your lex and yacc code. Also you should hand-in the output of your interpreter for five Snail programs which are given in the course web page. In the course web page you can also find example yacc programs (together with lex programs) that may help you to get started with your project. 8