Chapter 2, Part 1: An interpreter architecture for C-like languages Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall 2010.

Slides:



Advertisements
Similar presentations
Chapter 2, Part 2: An interpreter for an object-oriented language Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Advertisements

Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Chapter 6: From an assignment core to Java Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
1 C++ Syntax and Semantics The Development Process.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Names and Bindings.
Types and Arithmetic Operators
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
JavaScript, Fourth Edition
JavaScript, Third Edition
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Variables, Assignment & Math Storing and naming data.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Copyright Curt Hill Variables What are they? Why do we need them?
Pointers *, &, array similarities, functions, sizeof.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Data And Variables Chapter Names vs. Values Michael Jordan name (the letter sequence used to refer to something) value (the thing itself)
Programming with Microsoft Visual Basic th Edition
Characters and Strings
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lecture.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Learning Javascript From Mr Saem
Windows Programming Lecture 03. Pointers and Arrays.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Variables in Java A variable holds either
Topics Designing a Program Input, Processing, and Output
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Revision Lecture
Variables, Expressions, and IO
Semantic Analysis Chapter 6.
14th September IIT Kanpur
OPERATORS (1) CSC 111.
IDENTIFIERS CSC 111.
Building Java Programs Chapter 2
Chapter 3: Input/Output
Semantic Analysis Chapter 6.
CHAPTER FOUR VARIABLES AND CONSTANTS
Topics Designing a Program Input, Processing, and Output
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
C Programming Pointers
Lexical Elements & Operators
CIS 136 Building Mobile Apps
Variables and Constants
Python fundamental.
Introduction to Pointers
Presentation transcript:

Chapter 2, Part 1: An interpreter architecture for C-like languages Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall

Pointers and Left Value The meaning of a variable is slightly different when it is at the left-hand side of an assignment and when it is at the right-hand side. – e.g. y = x + 3 A pointer variable explicitly represents a location in memory. – e.g. z = &y #called “referencing” *z = x + 3 #called “de-referencing” The left-hand side of an assignment could be either a variable, or the (possibly multiple) de- referencing of a pointer variable 2

Grammar of a C-like language P : Program L : LefthandSide CL : CommandList C : Command E : Expression I : Variable N : Numeral P ::= CL CL ::= C | C ; CL C ::= L = E | while E : C end | print L | E ::= N | ( E1 + E2 ) | L | & L L ::= I | * L N ::= string of digits I ::= strings of letters, not including the keywords 3

How to interpret this language? 4 The environment maps variable names to memory locations Memory maps locations to values stored at the location y = 5 ; z = 0 ; x = (6 + y) y = 5 ; z = &y ; x = (6 + *z)

Operator Tree Syntax 5 PTREE ::= [ CTREE+ ] where CTREE+ means one or more CTREEs CTREE ::= ["=", LTREE, ETREE] | ["while", ETREE, CLIST] | ["print", VAR] ETREE ::= NUM | ["+", ETREE, ETREE] | ["&", LTREE] | LTREE LTREE ::= VAR | ["*", LTREE] NUM ::= string of digits VAR ::= string of letters but not "while" or "print" or "end"

Exercises 6 x = 3 x = 2; p = &x; y = *p x = 1; p = &x; *p = 2 x = 0; y = (6 + &x); *x = 999; print y x = 0; y = (6 + &x); *y = 999; print y Please show the result of interpreting the following programs:

Adding Declarations Why declaration is needed – Make sure any variable used is “allocated” – Mark the “type” of a variable so that programming mistakes can be caught earlier Type information – Explains the nature of the data manipulated by the program – Associated with declarations – “Type check” can be performed during translation/interpretation 7

The C-like language with declaration P : Program D : Declaration CL : CommandList L : LefthandSide C : Command E : Expression I : Variable N : Numeral P ::= CL CL ::= C | C ; CL C ::= L = E | while E : C end | print L | declare I : T T ::= int | * T E ::= N | ( E1 + E2 ) | L | & L L ::= I | * L N ::= string of digits I ::= strings of letters, not including the keywords, while, print 8

Exercise: extend the abstract syntax with variable declarations 9 PTREE ::= [ CTREE+ ] where CTREE+ means one or more CTREEs CTREE ::= ["=", LTREE, ETREE] | ["while", ETREE, CLIST] | ["print", VAR] ETREE ::= NUM | ["+", ETREE, ETREE] | ["&", LTREE] | LTREE LTREE ::= VAR | ["*", LTREE] NUM ::= string of digits VAR ::= string of letters but not "while" or "print" or "end"

Interpreter architecture with declarations 10 The environment maps variable names to type and memory locations *int = [“ptr, “int”] **int = [“ptr”, “ptr”, “int”]

When and how to use the declarations Declaring a variable requires allocating space for it in memory – Using an un-declared variable will be prohibited – Double declaration is not allowed Using a variable will need to check its type – “+” is only allowed on ints – “*” is only allowed on pointers – Types on both sides of assignment must match 11

Exercise: show results of interpreting the following programs 12 declare x: int; x = 3; declare y: int; y = (x + 1) declare x: int; declare p: *int; x = 2; p = &x; declare y: int; y = *p declare x: int; declare p: *int; x = 1; p = &x; x = *x declare x : int ; declare y : * int ; declare z : ** int ; z = &y ; y = &x ; **z = 99

Questions Without type checking, when will some of the programming errors be caught? Does type-checking need to be done every time a program is run? 13