Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
IntroductionExisting Interpreters
Additional Enhancements
CS 550 Project 1 Part 3: Expanding MiniLanguages
Justin Buckley: Group 2
May 31, 2016
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Layout
1 Introduction
2 Existing Interpreters
3 Additional Enhancements
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
The Mini Language
A simple language which allowing:
Arithmetic on integersAssignment of values to variablesSimple conditional statementsSimple looping (while loop)
Some extensions and modifications exist as described inassignments from previous courses
The addition of definable functional proceduresBoolean operatorsThe addition of list structuresA Scheme based interpreter using S-expressions and prefixoperations
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
EBNF-ish Grammar
Mini Language as defined in lecture on Mini Language fromCS 550 Spring 2013:
program → stmt-liststmt-list → stmt ; stmt-list | stmtstmt → assign-stmt | if -stmt | while-stmtassign-stmt → identifier := exprif -stmt → if expr then stmt-list else stmt-list fiwhile-stmt → while expr do stmt-list odexpr → expr + term | expr − term | termterm → term + factor | factorfactor → (expr) | number | identifiernumber → number digit | digitdigit → 0|1|2|3|4|5|6|7|8|9identifier → identifier letter | letterletter → a|b|c| . . . |z
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Syntax and Semantic notes
In the if statement, the expression is considered:True if eval(expr) > 0False otherwise
This is an explicit deviation from the behavior of manylanguages
Some have a distinct boolean type and require that ifstatement expressions evaluate to a boolean valueC and C++ assign the meaning of false to a specific value ofvarious types, such as 0 for integers while other values areconsidered to be trueMini’s unique mapping of integers to boolean values may causeissues when translating Mini programs to other languagesMini’s unique mapping of integers may cause issues whentrying to define functions or operators intended to work onboolean values
Conditional and looping structures have endcap tokens toprevent ambiguities
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Sources of Mini Language Information and Exercises
Code for parsers and interpreters, as well as languagedefinition available on previous course websites
CS 550 Spring 2013 has C++ bison and flex sources forstandard Mini and Mini with proceduresCS 360 Spring 2015 has ply code for a Mini interpreter withProcedures
Exercises without provided solutions:
Adding boolean operators (Python)Adding lists to the mini language (Python)Implementing Scheme based Mini language interpreterImplementing Mini language interpreter with procedures withalternative scoping rules
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Layout
1 Introduction
2 Existing Interpreters
3 Additional Enhancements
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
C++ Interpreter for Standard Mini
Yacc and lex grammar provided in notes for CS 550 Spring2013
Uses C++ Yacc options
Major syntactic components represented by classesEach of these classes contains an eval methodClasses allow for encapsulation of language semantics.
Identifiers referenced before assignment evaluate to 0
Bison grammar allows for 5 types:
int (used for numbers)char* (used for identifiers)Expr* (expressions)Stmt* (statements)StmtList* (statement lists)
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
C++ Interpreter for Standard Mini (Cont.)
Expr* type is used for:
exprfactorterm
Stmt* type is used for:
assignment statementsif statementswhile statements
StmtList* is used for stmt list
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
C++ Interpreter for Standard Mini (Cont.)
Each production in the bison grammar helps build a treestructure using C++ classes
Some of the classes assigned to tokens/symbols in the bisongrammar have subclasses which actually generated by the rules
The semantic definition is probably closer to action routinesthan to a formal attribute grammar
The routine for the program production constructs a programobject and evaluates the program
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
C++ Interpreter for Mini with Proc Extension
Yacc and lex grammar provided in notes for CS 550 Spring2013
Adds types to those associated bison symbols:
list<string>*list<Expr*>*
Creates a new name table when the proc is run and uses it forevaluation (Enclosing environment is ignored)
Adds symbols to the grammar:
func calldefine stmtparam listexpr list
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
C++ Interpreter for Mini with Proc Extension
Additional productions:stmt → define stmtdefine stmt → define identifier proc ( param list ) stmt list endparam list → identifier , param list | identifierfactor → funcallfuncall → identifier ( expr list )expr list → expr , expr list | expr
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Python interpreter for Mini
Covers the Proc extension to the Mini language
Uses ply for both scanning and parsing
Ply defines tokens with semantic value outside their functionin the syntax with functions defined with a prefix ‘t ’ whichtake an argument ‘t’ to which attributes can be added, noneed to define types
Productions in the yacc grammar are defined as functionswhose name starts with the prefix ‘p ’ which takes a parameterwhich allows us to add semantic meaning to the grammarClasses have been defined to wrap certain semantic ideas:
ExprNumberIdentTimesetc.
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Python interpreter for Mini (Cont.)
Syntax is identical to C++ version
Throws an error when an undefined identifier is referenced
This includes when an identifier defined outside a procedure isreferenced inside it
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Layout
1 Introduction
2 Existing Interpreters
3 Additional Enhancements
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Scheme flavored Mini Language
Mini language adjusted for Scheme
Would utilize the skeleton for the Metacircular evaluator.
as previously mentioned, would use prefix notation.
Not achieved as part of this project
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Mini language with Proc Scoping rules
Proc extended Mini language interpreter with alternativescoping
Assuming the extended mini language interpreters provideduse static scoping, this would have dynamic scoping.
Would have the semantic meaning of every procedure call alsotaking an environment which would evolve as executionprogresses
This would allow for access to global variables in the procedure
Could also explore other ways to ‘import’ enclosingenvironment
Embed into procedure a copy of the enclosing environment atthe time of definition.Implement a ‘capture’ syntax as in C++11
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Mini language with Proc Scoping rules (Cont.)
Accessing variables in enclosing scope in C++ can be done byaltering the apply method of the Proc class.
This allows for global variables, but is less true to the‘functional’ nature of the procedures in this language
Another alternative scoping rule would be if a procedure coulduse the values of identifiers at the time the procedure isdefined
This would require some local state stored for proceduresThe name table can be copied when the define statement isevaluated
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Boolean operators for Mini language
Boolean operators for Mini language.
Adding an additional operator precidence level would requireadditions to the yacc grammar operator precedence
Important to note how truth and falsehood are represented inMini (different from most modern programming languages)
Resultant grammar probably too large for a single slide.
Issues
Boolean encoding problem (eg. what := (3 < 4)− 2 == −1)
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Boolean operators for Mini language (Implementation)
Boolean operators will result in the following values
1 if True-1 if False
Boolean operator precedence is identical to addition andsubtraction
Using the python version of the Mini interpreter as a base.
A new class for each operator was needed for evaluation
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
Boolean operators for Mini language (Results)
The Python interpreter’s test cases include a file which uses aless than operator within a procedure as the test for a whileloop
An additional test was run using a program which assignsvalues to variables based on the results of several booleantests, then uses one in an if statement, just for good measure.
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
List Structures and Operations for Mini
List operations will be the ones described in the assignment
Starting point will be the python implementation of the Minilanguage
Arithmetic operators will not be defined on lists
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
List Structures and Operations for Mini (Implementation)
Starting point was the unaltered python implementation ofthe Mini language
This caused problems, as will be explained shortly
Various list operations (cons, car, cdr, null) basically performevaluate their arguments then return the operation applied tothe results
This would have unexpected effects if expressions could haveside effects
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages
IntroductionExisting Interpreters
Additional Enhancements
List Structures and Operations for Mini (Results)
Tested primarily using the file ‘simple list.p’
Utilzed boolean operators in a while loop
Due to choice of starting points, boolean operators did notexistUsing the boolean operators described previously would nothave worked, since it assumed that false statement wouldresult in 0, not −1
Justin Buckley: Group 2 CS 550 Project 1 Part 3: Expanding Mini Languages