Programming Languages Meeting 3 September 9/10, 2014.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Intermediate Code Generation
Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Names and Bindings.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
Chapter 2 A Simple Compiler
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
CSI 3120, Grammars, page 1 Language description methods Major topics in this part of the course: –Syntax and semantics –Grammars –Axiomatic semantics (next.
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Compiler Construction
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Programming Languages Meeting 6 September 30, 2014 October 1, 2014.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Computer Engineering Rabie A. Ramadan Lecture 5.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
SE424 Languages with Context A Block Structured Language.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Programming Language Descriptions. What drives PL Development? Computers are “in charge” of extremely important issues Execute a program literally. Exercise.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Programming Languages Meeting 4 September 16/17, 2014.
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.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
EGR 2261 Unit 11 Pointers and Dynamic Variables
CS 326 Programming Languages, Concepts and Implementation
Type Checking, and Scopes
A Simple Syntax-Directed Translator
Information Science and Engineering
Organization of Programming Languages
Mini Language Interpreter Programming Languages (CS 550)
Programming Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Faculty of Computer Science and Information System
Presentation transcript:

Programming Languages Meeting 3 September 9/10, 2014

Homework Results Look for sequences. Every RHS is one. ::= 0|1|2|3|4|5|6|7|8|9 ::= | ::= ::= ( ) | λ ::= ( ) | | ::= |,

Homework (2) Spelling: it is grammar There are four different grammars cited in the slides from the first lecture. I expected four parse trees. You cannot include ( ) in the parse tree if they don’t appear in the input string.

From Last Time Example 2 (Scope): How many identifiers used? How many defining points? What is the program output?

Plan for Tonight A survey and acting on the results Assignment statements – Simple semantics – Making them more complex and testing the limits of our definitions – Reference values – Multiple assignment statements – Syntax and semantics of complex lhs expressions

Survey On the small piece of paper, write down all the programming languages you have installed on your computer because you have: – An IDE for the language – An interpreter for the language – A compiler for the language Hint: Check the Program Files folder or the Applications folder

On to Assignment Statements In addition to Env and Loc, which have been defined before, define Value, the set of storable values for a particular type V* = Value + {undefined} Store = Loc  V*

Assignment (2) Investigating the semantics of an assignment statement, which stores a value in a named location: Abstract syntax shows we need – An l-expression: the target of the assignment – A value: to store in the target

Assignment (3) Possible l-expressions are – a simple identifier – an array component – a field in a record – a pointer image – and others

Assignment (4) At declaration time each identifier is associated with an attribute record, which contains – A type descriptor – An addressing function (the identity function for nonstructured types) – Storage requirements (# of words and layout)

Assignment (5) At instantiation, which occurs at procedure invocation, each identifier is mapped to a base location, which becomes an argument to the addressing function. Let M = Ide  Loc + {unbound}  V* with the understanding that {unbound}  {undefined}

Assignment (6) In the simplest cases we can work with M = Ide  V* bypassing consideration of locations. Ready for the formal semantics of an assignment statement:

Assignment (7) The meaning of the assignment statement A := E is a function that maps M to M, sometimes denoted by [A:=E] defined so that M goes to M’ where M is the original memory state: a function assigning identifiers to values M’ is the new memory state, defined by

Assignment (8) M(x) if x ≠ A M’(x) = v if x = A Note that this says: The previous value of A is irrelevant No other values are affected

Your Turn Exercise 1: Assume Ide = { a,b,c }, Value = { 0,1,2,3 } Find [a := 2] Caution: There are several sets of functions involved. Keep them straight.

Your Turn (2) Exercise 2: (Refer to the handout: Syntax Definition for REF) 1.Determine the four components of the context- free grammar specification for REF – start symbol, nonterminals, etc. 2.Write a small (no more than 5 lines) syntactically correct program in REF. Put your final version on the special sheet of paper.

Exercise 2 (2) 3.What is the output of your program? 4.How many different types does the language support? 5.Create the parse tree for the program you have been given.

Exercise 3 For the program fragment on the following slide, give the elements of Env, Loc, Store, and M that represent the program state after the fragment loads. If you need memory addresses, make them up. Hint: Figure out what the elements of each of the semantic domains look like.

Exercise 3 (2) program declare ONE : integer constant = 1; declare TWO : integer constant = 2; declare A,B : integer; declare RC,RD : ref integer; declare RRE, RRF : ref ref integer;

Semantics of REF Semantics of a REF assignment statement: First the LHS: It is an identifier with a type. – Type is integer constant: invalid assignment—type mismatch – Type is [ref…] integer: Assume j occurrences of ref, j = 0,1,2,…

Semantics (2) Now the RHS: three kinds of expressions a)+ operation included b)literal integer c)single identifier

Semantics (3) Generating a RHS value: Case a) RHS involves + – Find location bound to variable, perform operation as indicated in RHS dereferencing identifiers as necessary, obtaining value v Case b) RHS is an integer – The value v is the value of the integer

Semantics (4) Case c) RHS is a single identifier, with type – Integer constant: Value v is the value of the constant – [ref]… integer: There are 3 subcases, depending on the reference level k of the identifier Case 1: j < k+1 Case 2: j = k+1 Case 3: J > k+1

Semantics (5) Taking each of these subcases in turn j < k+1 : Dereference RHS identifier to level j. Set v to the value of the dereferenced identifier. j = k+1 : Set v to the location of the RHS identifier j > k+1 : Invalid statement—type mismatch

Semantics (6) Store v in location determined by LHS. – If v is an integer, the reference level of the LHS, j, must be 0. Otherwise, there is a type mismatch. – If v is a reference value, it is of the same level as the LHS, so is storable.

Exercise 4 Using the declaration fragment from before, execute the following statements and determine the memory function M : Ide -> Loc -> V* when the fragment completes.

Exercise 4 (2) A := ONE; B := A + ONE; RC := A; RD := B; RRE := RC; RRF := RD;

Exercise 5 Finally, assume that you have executed the 6 lines of code in Exercise 4. Make each of the statements below the 7 th line of code and determine how M changes. 1. A := RRF + B; 2. B := RRE; 3. RC := RRF + RD; 4. RRF : = RRE; 5. RRE : = RD;

Installing Systems Make sure that you have installed on your computer: ChucK ( ) -- chuck.cs.princeton.edu Python (3.4.1) -- python.org Perl (5.20.0) – perl.org

For Next Time Install the systems noted on the previous slide. Execute the five statements in REF in Exercise 5. Post the results on piazza.com