Docsity

Mini Language Interpreter: Building a Programming Language from Scratch (CS 550) - Prof. J, Assignments of Programming Languages

Partial preview of the text

Download Mini Language Interpreter: Building a Programming Language from Scratch (CS 550) - Prof. J and more Assignments Programming Languages in PDF only on Docsity!

Programming Languages

(CS 550)

Lecture 1 Summary^ Mini Language Interpreter^ Jeremy R. Johnson

Theme

™This lecture builds an interpreter for themini language from chapter 13 of the text.A parser is written that translates the inputprogram into a data structure that can easilybe interpreted. The language is extended tosupport procedures.

Outline

™Adding user defined functions to the minilanguage^ ¾^ parameter passing^ ¾^ local variables (local environment)^ ¾^ function application^ ƒ^ Execute procedure body in local environment with formalparameters bound to actual argument values^ ¾^ return value^ ¾^ recursion ™Exercise 2: Modify the extended Mini Languageand interpreter to use an explicit return statement

Outline

™Discuss assignment 2^ ¾^ Add lists to the mini language^ ¾^ Values are now lists or ints (modify Environment)^ ¾^ Support list constants and built-in list processingfunctions^ ƒ^ cons( e, L ) - appends element e to the front of list^ ƒ^ car( L ) - returns the first element in the list^ ƒ^ cdr( L ) - returns the rest of the list (minus the first element)^ ƒ^ nullp( L ) - returns 1 if L is null, 0 otherwise^ ƒ^ intp( e ) - returns 1 if e is an integer, 0 otherwise^ ƒ^ listp( e ) - returns 1 if e is a list, 0 otherwise to allowconstruction and access to lists.

Operational Semantics ™The meaning of a program is obtained byinterpreting the result of each statement insome model of computation. ™We use C++ to interpret execution of eachstatement ™Chapter 13 uses a reduction machine

Environments

™Let an Environment be a map from indentifiers tovalues = integers

∪^ undefined ™Mini language programs can be thought of as amap from an initial Environment to a finalEnvironment (assuming it terminates) ™The initial environment maps all identifiers to anundefined ™Each statement is defined in terms of what it doesto the current environment (another mapping)

Example Mini Language Program 1. n := 0 -

2. if n then i := n else i := 0 -

n fi;

3. fact := 1; 4. while i do fact := fact * i; i := i -

1 od

What is the final environment?

Implementing the Interpreter ™Syntax directed semantics ™The interpreter is implemented by creating a class,with an evaluate method, for each syntacticcategory. Use inheritance to derive specializedstatements from more general categories ofstatements. When the parser detects a syntacticcategory the corresponding constructor is called.A map is used to store the environment and theprogram is executed by calling all of the evaluatemethods of the statements in the program.

Example Mini Language Procedure define add proc(n) i := n; s := 0; while i do s := s + i; i := i-1 od; return := s end; n := 5; s := add(n) What is the final environment?

Example Recursive Mini Language

Procedure

define addr proc(n)^ if n then return := n + addr(n-1) else return := 0 fi end; n := 5; s := addr(n) What is the final environment?