Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Interpretation Environments and Evaluation. CS 354 Spring 20052 Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree."— Presentation transcript:

1 Interpretation Environments and Evaluation

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

3 CS 354 Spring 20053 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.

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

5 CS 354 Spring 20055 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.

6 CS 354 Spring 20056 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.

7 CS 354 Spring 20057 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.

8 CS 354 Spring 20058 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.

9 CS 354 Spring 20059 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

10 CS 354 Spring 200510 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

11 CS 354 Spring 200511 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.

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

13 CS 354 Spring 200513 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

14 CS 354 Spring 200514 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

15 CS 354 Spring 200515 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.

16 CS 354 Spring 2005

17 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

18 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

19 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

20 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

21 Parse tree for if statement CS 354 Spring 2005

22 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

23 Parse Tree for while loop CS 354 Spring 2005

24 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

25 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

26 Parse Tree for Function Definition CS 354 Spring 2005

27 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

28 Parse Tree for Function Call CS 354 Spring 2005

29 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


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

Similar presentations


Ads by Google