Presentation is loading. Please wait.

Presentation is loading. Please wait.

Environments and Evaluation

Similar presentations


Presentation on theme: "Environments and Evaluation"— Presentation transcript:

1 Environments and Evaluation
Interpretation Environments and Evaluation

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

3 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 2005

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

5 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 2005

6 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 2005

7 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 2005

8 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 2005

9 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 2005

10 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 2005

11 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 2005

12 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 CS 354 Spring 2005

13 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 2005

14 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 2005

15 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


Download ppt "Environments and Evaluation"

Similar presentations


Ads by Google