Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC 8310 Programming Languages Meeting 2 September 2/3, 2014."— Presentation transcript:

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

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

3 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

4 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

5 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.

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

7 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.

8 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.

9 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.

10 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.

11 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.

12 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

13 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*

14 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

15 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 = + | - | * | / | !

16 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

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

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

19 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

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

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

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

23 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

24 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

25 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

26 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

27 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

28 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

29 Binding Time: Example Consider the assignment statement x := 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

30 Example (2) x -> value @ execution 10 -> representation @ definition, implementation + -> representation @ definition := -> representation @definition

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

32 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.

33 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

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

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

36 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:

37 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.

38 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.

39 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.

40 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

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


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

Similar presentations


Ads by Google