CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Programming Languages and Paradigms
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.
Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 2 A Simple Compiler
Names and Bindings Introduction Names Variables The concept of binding Chapter 5-a.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
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.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Imperative Programming
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
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.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
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.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
ISBN Chapter 3 Describing Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Syntax (2).
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
 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.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Programming Languages Meeting 3 September 9/10, 2014.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Organization of Programming Languages Meeting 3 January 15, 2016.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Lecture 3 Translation.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
System Software Unit-1 (Language Processors) A TOY Compiler
A Simple Syntax-Directed Translator
Constructing Precedence Table
CSC 8310 Programming Languages
Organization of Programming Languages
Mini Language Interpreter Programming Languages (CS 550)
Representation, Syntax, Paradigms, Types
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

From Last Time Let A be the keyboard producible alphabet – 26 lower case letters – 26 upper case letters – 10 digits – 32 printing symbols – Tab, Space, Newline The set of programs L defined by a programming language is a subset of A*.

Context-Free Languages Defined by a context-free grammar Grammar expressed in BNF or some extension – Start symbol – Non-terminal symbols – Terminal symbols – Productions Grammar used – to check a string for inclusion in language L – to generate a string in the language L

Pam Referring to the syntax of the programming language Pam, let’s determine: Its alphabet Details of its grammar – Terminal symbols – Non-terminal symbols – Start symbol – Productions

Pam (2) Exercises with Pam: 1.Write the shortest possible syntactically correct program in Pam. 2.Write a program in Pam that reads a positive integer n and writes the value of n! 3.Modify your program to allow the input to include 0.

Pam (3) 4.Modify your program to produce a table of the first p factorials. Read p as the input.

Abstracting the Syntax EBNF does a good job defining the concrete syntax of a language. Working across languages and looking at fundamental principles of languages, we need less concrete information.

Abstracting (2) For example, if a language allows a list of identifiers in various statements, we don’t care what symbol is used to separate the identifiers. Concrete syntax shows form. Abstract syntax shows relationships between essential elements.

Abstracting (3) More examples: An assignment statement needs a value and a place to store it. The symbol for the action is irrelevant. Various languages have used = := => <− A conditional statement needs a Boolean valued expression and one or two statements.

Discovering the Abstract Syntax General idea: remove from the parse tree of a phrase in a programming language nodes that convey no meaning. Specifically: Remove all terminal symbols that are separators, terminators or other punctuation and the paths leading to them.

Discovering (2) Condense paths containing no choices to initial branch, terminal symbol, and parent of terminal symbol. Condense subtrees involving an operator by renaming the root with the operator and maintaining the branches with the operator arguments.

Discovering (3) Your turn: – Use the standard grammar for expressions – Construct the parse tree for 3*x-4*x*y – Condense the parse tree to show the abstract syntax

Abstract Syntax, formally Category name = list of alternatives or list of essential components of the category A sequence of alternatives is denoted by * Statements = Statement*

Abstract Syntax (2) Essential components are described by a sequence of Categories separated by ; Each Category is associated with the field or fields necessary to specify the category. Some examples

Abstract Syntax (3) Assignment = Variable target; Expression source Expression = Variable | Value | Binary | Unary Variable = String id Value = Integer value Binary = Operator op; Expression term1, term2 Unary = Operator op; Expression term Operator = + | - | * | / | !

Abstract Syntax (4) Here’s Pam Stseq = Statement * Statement = Read | Write | Assignment | Conditional | DefLoop | WhileLoop Read = Variable * Write = Variable * Assignment = Variable: target; Expression: source Conditional = Expression: test; Stseq: thenbranch, elsebranch DefLoop = Expression: numrep; Stseq WhileLoop = Expression: test; Stseq Expression = Variable | Value | Binary Variable = String: id

Moving Toward Semantics Concrete syntax Abstract syntax Semantics: described in various ways – Program function – Denotational – Operational – Axiomatic

Name Space Questions: Within a program, what is named? What is the (concrete) syntax of a name?

Name Structure Any structure of a programming language (PL) represented by an identifier, sometimes called a denotable value – Note that denotable value can include code for functions, methods, etc. Implemented by a declaration or binding of identifier to structure Done explicitly or implicitly

Name Structure (2) Examples Label Constant Variable Type Procedure/function/me thod Class Object

Binding Association of a program element to a meaning or property Example: + associated to – integer addition – set union – string concatenation

Binding (2) Example: x may represent – a variable – a type – a method – an object

Binding (3) More definitions: The association of a program element to a meaning is accomplished by a semantic function If the association has more than one value (straining the definition of function), the element is overloaded

Binding Time The time from language definition through program execution at which a program element is mapped by a semantic function to its meaning Informally, who decides what x means and when

Binding Time (2) The 6 typical binding times, from latest to earliest are: Execution time Load time Link time Compile time Language implementation time Language definition time

Binding Time (3) Execution time – Executing a statement binds an identifier to a value by binding a location to the value – Implicitly declaring a variable binds an identifier to a location – Entering a subprogram or some code segments binds a formal parameter to an actual parameter

Binding Time (4) Load time – Global identifiers bind to their locations Link (build) time – Library function names bind to the actual code to be executed

Binding Time (5) Compile time – Variable declaration binds a variable to a set of values Language implementation time – Binds values to their representations Language definition time – Binds symbols to their meanings

Binding Time: Example Consider the assignment statement x := x + 10 Binding times shown in this statement are: x -> set of possible lang def, translation x -> type of translation, execution x -> set of implementation

Example (2) x -> execution 10 -> definition, implementation + -> definition := ->

Types of Binding Syntactic: determined by syntax Nested: determined by scope Implicit: determined by import or with statements Default: language specific, e.g. Fortran

Binding of Identifiers The effect of the environment map associating identifiers to locations. Let Loc denote the set of available locations, including many words of physical memory if necessary. Formally Env = Ide −> [ Loc + {unbound}] Gives meaning but not values to identifiers Env is a set of functions. Each function describes a particular binding.

Identifiers Let Ide be the set of identifiers for a programming language. Partition this set into keywords: special meanings, e.g. int reserved words: keywords that cannot be redefined user defined names: your choice

Identifiers (2) Start with identifiers that name values, usually called variables. These are bound to locations chosen from the set of locations, denoted Loc

Identifiers (3) Your turn: Do a quick computation. If we have 3 possible identifiers and 4 locations available to store values, how many elements are there in semantic category Env, which was defined as Ide −> [ Loc + {unbound}]

Scope Each identifier used in a program has meaning in part or all of the program. Its static scope is the region associated with this meaning. The formal definition is:

Scope (2) Given a text file with a syntactically correct program, each identifier used in the program, has one or more defining points that provide binding occurrences of the identifier.

Scope (3) Associated with each defining point is a region, which is the part of the text file in which the identifier has meaning. The static scope of the identifier is the largest subset of its region, which contains no other defining point of an identifier with the same spelling.

Scope (4) Notes: The static scope may be a disconnected sequence of program elements. The definition tries to capture what happens with identifiers that are used as index variables of loops.

Scope (5) Your turn: For Examples 1 and 2, find – The set of identifiers used – The defining point(s) of each identifier – The region associated with each defining point – The scope of each identifier – The output of the program

Scope (6) Before next time: Post the output from Example 2 on piazza.com.