csc 8310 programming languages meeting 2 september 2/3, 2014
TRANSCRIPT
CSC 8310Programming Languages
Meeting 2September 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 sourceExpression = Variable | Value | Binary | UnaryVariable = String idValue = Integer valueBinary = Operator op; Expression term1, term2Unary = Operator op; Expression termOperator = + | - | * | / | !
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
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/method
• 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 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
Example (2)
• x -> value @ execution• 10 -> representation @ definition, implementation• + -> representation @ definition• := -> representation @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.