csc 8310 programming languages meeting 2 september 2/3, 2014

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

Upload: erick-scott

Post on 24-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

CSC 8310Programming Languages

Meeting 2September 2/3, 2014

Page 2: 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*.

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

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

Page 4: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 5: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 6: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Pam (3)

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

Page 7: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 8: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 9: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 10: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 11: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 12: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 13: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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*

Page 14: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 15: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Abstract Syntax (3)

Assignment = Variable target; Expression sourceExpression = Variable | Value | Binary | UnaryVariable = String idValue = Integer valueBinary = Operator op; Expression term1, term2Unary = Operator op; Expression termOperator = + | - | * | / | !

Page 16: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Abstract Syntax (4)

Here’s PamStseq = Statement *Statement = Read | Write | Assignment | Conditional |

DefLoop | WhileLoopRead = Variable *Write = Variable *Assignment = Variable: target; Expression: sourceConditional = Expression: test; Stseq: thenbranch, elsebranchDefLoop = Expression: numrep; StseqWhileLoop = Expression: test; StseqExpression = Variable | Value | BinaryVariable = String: id

Page 17: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Moving Toward Semantics

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

Page 18: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Name Space

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

Page 19: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 20: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Name Structure (2)

Examples• Label• Constant• Variable • Type

• Procedure/function/method

• Class• Object

Page 21: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Binding

• Association of a program element to a meaning or property

• Example: + associated to– integer addition– set union– string concatenation

Page 22: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Binding (2)

• Example: x may represent– a variable– a type– a method– an object

Page 23: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 24: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 25: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 26: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 27: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 28: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 29: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Binding Time: Example

Consider the assignment statementx := x + 10

Binding times shown in this statement are:• x -> set of possible types @ lang def, translation• x -> type of variable @ translation, execution• x -> set of values @ implementation

Page 30: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Example (2)

• x -> value @ execution• 10 -> representation @ definition, implementation• + -> representation @ definition• := -> representation @definition

Page 31: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Types of Binding

• Syntactic: determined by syntax• Nested: determined by scope• Implicit: determined by import or with

statements• Default: language specific, e.g. Fortran

Page 32: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 33: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 34: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Identifiers (2)

Start with identifiers that name values, usually called variables.

These are bound to locations chosen from the set of locations, denoted Loc

Page 35: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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}]

Page 36: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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:

Page 37: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 38: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 39: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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.

Page 40: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

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

Page 41: CSC 8310 Programming Languages Meeting 2 September 2/3, 2014

Scope (6)

Before next time:

Post the output from Example 2 on piazza.com.