Interpretation Environments and Evaluation. CS 354 Spring 20052 Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.

Slides:



Advertisements
Similar presentations
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Semantics Static semantics Dynamic semantics attribute grammars
1 Compiler Construction Intermediate Code Generation.
Exercise: Balanced Parentheses
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
CPSC Compiler Tutorial 9 Review of Compiler.
ISBN Chapter 10 Implementing Subprograms.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
16-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Context-Free Grammars Lecture 7
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
Environments and Evaluation
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Compiler Summary Mooly Sagiv html://
CS 280 Data Structures Professor John Peterson. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project.
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Language Translators - Lee McCluskey LANGUAGE TRANSLATORS: WEEK 21 LECTURE: Using JavaCup to create simple interpreters
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CPS 506 Comparative Programming Languages Syntax Specification.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Semantic Analysis. Find 6 problems with this code. These issues go beyond syntax.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
CS 153: Concepts of Compiler Design September 14 Class Meeting
CS 3304 Comparative Languages
A Simple Syntax-Directed Translator
Constructing Precedence Table
Compiler Construction (CS-636)
CS 153: Concepts of Compiler Design October 17 Class Meeting
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
PROGRAMMING LANGUAGES
-by Nisarg Vasavada (Compiled*)
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
ADTs, Grammars, Parsing, Tree traversals
CMPE 152: Compiler Design August 21/23 Lab
The Recursive Descent Algorithm
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

Interpretation Environments and Evaluation

CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree Evaluation/Code generation

CS 354 Spring Interpreter The interpreter operates in a read-eval-print loop. –The Parser gets a statement worth of lexemes from the Scanner (which gets them from the input) and builds them into a parse tree. –eval is a method that evaluates the parse tree. –The result of each statement is printed.

CS 354 Spring The Interpreter while (! lexer.endOfInput) { pt = parser.next(); result = evaluator.eval( pt); print( result); }

CS 354 Spring Evaluating a Parse Tree The eval method traverses the parse tree starting at the root The order is determined by the tokens of the lexemes in the tree.

CS 354 Spring Storing Variable Values In order to evaluate an expression containing variables, there needs to be a way to store and look up the current values of each variable. A compiler uses a symbol table to store information about variables and inserts the appropriate address into the executable code. In an interpreter, we need a run-time data structure for storing and looking up variables. This run-time structure is often called an environment.

CS 354 Spring Finite Functions One way to think of environments is as a finite function. A finite function associates a value with each of a finite set of symbols.

CS 354 Spring The Global Environment In the initial version of the interpreter, all variables have the same scope. –We need a single data structure for storing variables and their values in a way that makes it easy to find them. –The hash table ADT fills this need. Later, we will need to be able to support different scopes. –Use a stack.

CS 354 Spring Environment Operations create an empty environment look up a variable and get the associated value update a binding in an environment –this is what happens when you make an assignment has to take care of insert a variable into an environment display all the variables in the environment –useful for debugging Later, we'll want to extend an environment to allow for nested scopes

CS 354 Spring The Evaluation Process The evaluator will be a big selection statement similar to the one in your parse function. What eval does depends on the token of the root The eval method is recursive –The return type needs to be independent of the token –Use a Lexeme to store the value

CS 354 Spring Evaluation Process Numbers are self-evaluating - just return the Lexeme Variables need to be looked up - a NUMBER Lexeme containing their value should be returned Assignment requires evaluating the right child of the ASSIGN Lexeme and storing the value of the left child (a VAR) in the environment.

CS 354 Spring Expression Evaluation For binary operators (PLUS, MINUS, TIMES, DIVIDE) –Evaluate the left child –Evaluate the right child –Perform the specified operation on the values –Return a Lexeme containing the result For unary operations, there is only one child –Evaluate that child –Perform the operation –Return a Lexeme containing the result

CS 354 Spring Evaluating an expression From top –Evaluate left child of + (recursive call to eval) Evaluate left child of / (look up b) Evaluate right child of / –Evaluate left child of - (look up a) –Evaluate right child of - (look up b) –Subtract b from a Divide b by rhs value –Evaluate right child of + to get 1 –Add lhs and rhs to get result

CS 354 Spring Built-In Functions You can add functions like sqrt or pow to the interpreter –Create a new token –Check for the function name in your keyword method –Add the syntax for calling the functions to the primary rule in the grammar –Add a case to eval for each function Use the Java equivalent in the evaluation Built-in functions have the same syntax as user-defined functions

CS 354 Spring Error Handling What happens when your interpreter encounters a syntax error? –Quitting is easy to implement but not very user friendly. –Ideally, you'd like to skip to the end of the current statement and start again with the next. How do you determine where the end of the statement is? –In this version of the interpreter, read to the next semicolon. –As the language gets more complex, this gets harder.

CS 354 Spring 2005

Evaluation and Control Structures How do we build parse trees for control structures? We need some extra node types (structural tokens) to glue the parts of these statements together –An if statement can have three parts –We need to be able to make lists of statements. The eval method needs new cases for these statements. CS 354 Spring 2005

Boolean expressions The test conditions for control structures need boolean expressions Except for the NOT operator, these are binary so the parse trees look like those for arithmetic expressions The NOT operator only has one child. CS 354 Spring 2005

Lists in Parse Trees To represent a list, you need some kind of back-bone node to separate different elements in the list The token could be LIST CS 354 Spring 2005

if statement An if statement has a test condition, a then part and possibly an else part. The parser needs to build a tree with places for these three parts –use a structural node to separate the then part and the else part To evaluate, evaluate the test condition –if true, evaluate the then part –if false, evaluate the else part or do nothing in there isn’t one –Value of the if statement is the value of the last statement executed CS 354 Spring 2005

Parse tree for if statement CS 354 Spring 2005

while loop A while statement has a test condition a list of one or more statements. The parser needs to build a parse tree for the whole while which will include a parse tree for the test condition and a list of parse trees for the statements in the body of the loop. The evaluator needs a loop that evaluates the test condition and then evaluated the body of the loop if the condition is true Value of the while is the value of the last statement executed CS 354 Spring 2005

Parse Tree for while loop CS 354 Spring 2005

Implementing Functions We need to be able to define functions and call them A function definition is a new kind of statement –Token is FUNCTION A function call can be a statement or it can be an expression –Token is FN_CALL CS 354 Spring 2005

Evaluating a Function Definition Build a parse tree containing the parameter list and the statement list. For static scope, you also need the environment that is active when the function is defined Store this parse tree in the environment with the function name as the key What should the value of a function definition be? CS 354 Spring 2005

Parse Tree for Function Definition CS 354 Spring 2005

Parse Tree for Function Call The parse tree needs the name of the function and a list of the expressions used for arguments CS 354 Spring 2005

Parse Tree for Function Call CS 354 Spring 2005

Evaluating a function call Look up the function in the environment Create a new environment –Extend the one stored with the function for static scope –Extend the current one for dynamic scope For each argument, evaluate it and store it in the new environment as the value of the corresponding parmeter Evaluate the body of the function in this new environment CS 354 Spring 2005