Environments and Evaluation

Slides:



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

CPSC 388 – Compiler Design and Construction
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.
Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
CPSC Compiler Tutorial 9 Review of Compiler.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
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.
Chapter 2 A Simple Compiler
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
ERROR HANDLING Lecture on 27/08/2013 PPT: 11CS10037 SAHIL ARORA.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Language Translators - Lee McCluskey LANGUAGE TRANSLATORS: WEEK 21 LECTURE: Using JavaCup to create simple interpreters
CSC 338: Compiler design and implementation
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
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.
SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 6, 10/02/2003 Prof. Roy Levow.
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
INVISIBLE JACC By Ronald Hathaway. So, What is it? Invisible jacc is a parser generator implemented in java which produces lexical analyzers and parsers.
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.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Compilation With an emphasis on getting the job done quickly Copyright © – Curt Hill.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
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.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
CS 153: Concepts of Compiler Design September 14 Class Meeting
Compiler Design (40-414) Main Text Book:
CS 3304 Comparative Languages
Lexical and Syntax Analysis
A Simple Syntax-Directed Translator
Programming Languages Translator
Parsing and Parser Parsing methods: top-down & bottom-up
8. Symbol Table Chih-Hung Wang
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.
CMPE 152: Compiler Design December 5 Class Meeting
CPSC 388 – Compiler Design and Construction
ADTs, Grammars, Parsing, Tree traversals
CMPE 152: Compiler Design August 21/23 Lab
High-Level Programming Language
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
CMPE 152: Compiler Design September 17 Class Meeting
Presentation transcript:

Environments and Evaluation Interpretation Environments and Evaluation

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

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

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

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

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

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

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

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

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

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

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

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

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

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