Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Compiler Construction Intermediate Code Generation.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Code Generation Mooly Sagiv html:// Chapter 4.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
CS 536 Spring Run-time organization Lecture 19.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Run time vs. Compile time
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Summary Mooly Sagiv html://
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
5.3 Machine-Independent Compiler Features
COP4020 Programming Languages
Chapter 1. Introduction.
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Compiler Construction
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Introduction to Code Generation and Intermediate Representations
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
BY: JAKE TENBERG & CHELSEA SHIPP PROJECT REVIEW: JGIBBERISH.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CSC 8505 Compiler Construction
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Chapter 1 Introduction.
CS 3304 Comparative Languages
Compiler Construction (CS-636)
Overview of Compilation The Compiler BACK End
Chapter 1 Introduction.
Compiler Lecture 1 CS510.
CS 536 / Fall 2017 Introduction to programming languages and compilers
CSE401 Introduction to Compiler Construction
Overview of Compilation The Compiler BACK End
COP4020 Programming Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Structures 1. Overview Objective
Presentation transcript:

Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it

Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis Code Generation Optimization Intermediate Code Generation Semantic Analysis Syntactic Analysis annotated AST abstract syntax tree token stream target language intermediate form intermediate form Synthesis of output program (back-end)

Implementing A Language Given type-checked AST program representation: might want to run it might want to analyze program properties might want to display aspects of program on screen for user... To run program: can interpret AST directly can generate target program that is then run recursively Tradeoffs: time till program can be executed (turnaround time) speed of executing program simplicity of implementation flexibility of implementation

Interpreters Create data structures to represent run-time program state –values manipulated by program –activation record (a\k\a stack frame) for each called method –environment to store local variable bindings –pointer to lexically-enclosing activation record/environment (static link) –pointer to calling activation record (dynamic link) EVAL loop executing AST nodes

Pros and Cons of Interpretation + simple conceptually, easy to implement fast turnaround time good programming environments easy to support fancy language features - slow to execute data structure for value vs. direct value variable lookup vs. registers or direct access EVAL overhead vs. direct machine instructions no optimizations across AST nodes

Compilation Divide interpreter work into two parts: compile-time run-time Compile-time does preprocessing perform some computations at compile-time once produce an equivalent program that gets run many times Only advantage over interpreters: faster running programs

Compile-time Processing Decide representation of run-time data values Decide where data will be stored registers format of stack frames global memory format of in-memory data structures (e.g. records, arrays) Generate machine code to do basic operations just like interpreting expression, except generate code that will evaluate it later Do optimizations across instructions if desired

Compile-time vs Run-time Compile-timeRun-time ProcedureActivation record/stack frame Scope, symbol tableEnvironment (contents of stack frame) VariableMemory location or register Lexically-enclosing scope Static link Calling ProcedureDynamic link

An Interpreter for MiniJava In Environment subdirectory, two data structures: Data structure to represent run-time values: Value hierarchy –analogous to ResolvedType hierarchy Value IntValue BooleanValue ClassValue NullValue

MiniJava Interpreter [continued] Data structure to store Value s for each variable: Environment hierarchy –analogous to Symbol Table hierarchy Environment GlobalEnvironment NestedEnvironment ClassEnvironment CodeEnvironment MethodEnvironment evaluate methods for each kind of AST class

Activation Records Each call of a procedure allocates an activation record (instance of Environment, somewhat poorly named) Activation record stores: mapping from names to Value s, for each formal and local variable in that scope (environment) lexically enclosing activation record (static link) Method activation record: also calling activation record (dynamic link) Class activation record: also methods (to support run-time method lookup) instance variable declarations, not values values stored in class instances, i.e., ClassValues

Activation Records vs Symbol Tables For each method/nested block scope in a program: exactly one symbol table, storing types of names possibly many activation records, one per invocation, each storing values of names For recursive procedures, can have several activation records for same procedure on stack simultaneously All activation records have same “shape,” described by single symbol table

Example... class Fac { public int ComputeFac(int num) { int numAux; if (num < 1) { numAux = 1; } else { numAux = num * this.ComputeFac(num-1); } return numAux; }