PrLanguage 3
PrLanguage 3
زبانهای برنامهنویسی
دانشگاه بین المللی امام خمینی (ره)
/20
2
/20
3 Discussion of this chapter
/20
4 Description of a programming
language is crutial
A concise yet understandable description of a PL is
difficult but essential to the language’s success: Tradeoff\
Some Chalanges:
Diversity of the people who must understand the
description
Programming language implementors obviously must be
able to determine how the expressions, statements, and
program units of a language are formed, and also their
intended effect when executed. The difficulty of the
implementors’ job is, in part, determined by the
completeness and precision of the language description.
language users must be able to determine how to
encode software solutions by referring to a language
reference manual.
/20
5 Syntax & Semantic
description
/20
6 Syntax & Semantic
description (2)
Although they are often separated for discussion
purposes, syntax and semantics are closely related.
In a well-designed programming language, semantics
should follow directly from syntax; that is, the
appearance of a statement should strongly suggest
what the statement is meant to accomplish.
/20
7 Syntax
/20
8 Syntax; Lexeme
/20
9 Syntax; Token
/20
10 Syntax; Lexeme & Token
Example
index = 2 * count + 17;
/20
11 Two way of Language Definition:
Recognition / Generation
Language Recognizer
For Language: L from alphabet: ∑
We need to construct a mechanism R, called a
recognition device
capable of reading strings of characters from the alphabet Σ
Indicate whether a given input string was or was not in L
R would either accept or reject the given string.
Then: R is a description of L
Because most useful languages are, for all practical
purposes, infinite, this might seem like a lengthy and
ineffective process.
Language Generator
A device that can be used to generate the sentences
of a language.
We can think of the generator as having a button that
produces a sentence of the language every time it is
pushed.
seems to be a device of limited usefulness as a
language descriptor/
/20
13 Formal Methods of Describing
Syntax
Context-Free Grammar
Backus-Naur Form (BNF)
Drivation
Parse Tree
Ambiguity
Operator Precedence
Associativity
An Unambiguous Grammar for if-else (page 227 pdf)
Extended BNF
/20
14 Design / Definition a Language
with BNF
rule, or production
6 Rule
/20
16 Formal Methods of Describing
Syntax
/20
17 BNF: PARSE TREE
/20
18 BNF: Ambiguity
/20
19 BNF: Ambiguity
Left Most Derivation
Solution:
Make a decision:
Right Recursive Rules
Or
Left Recursive Rules
/20
Right Most Derivation
20 BNF: Ambiguity
Solution:
Make a decision:
Right Recursive Rules Using More non-terminals
Or
Left Recursive Rules
/20
21 BNF: NO Ambiguity
/20
22 BNF: Operator Precedence
/20
23
BNF: Associativity of Operators
/20
24
BNF: Associativity of Operators
/20
Left Recursion Left Associativity
25 Design / Definition a Language
with BNF
BNF is a metalanguage for programming languages.
BNF uses abstractions for syntactic structures
How Define??
Un-Ambiguise Rules:
More Non-Terminals / More Rules
Each Rule: Right Recursion or Left Recursion
Assocaitivity
Decision For Each Rule to be: Right Recursiev or Left Recursive
Right Recursive: Right Associativity
Left Recursive: Left Associativity
Operator Precedence: Rules Ordering
More Depth: More Precedence
Other ….Continue
/20
26 BNF: Continue
A Grammar for a Small Language
/20
27 BNF: Continue
A Derivation of Grammar for a Small Language
/20
28 BNF: Continue
A Grammar for Identifier Decration of C
<ident_list> → identifier
| identifier, <ident_list>
<Spaces> Space
| space<spaces>
/20
29 BNF: Continue
if Statement
Ambiguity
/20
30 BNF: Continue
An Unambiguous Grammar for if-else
More Non-Termainals
/20
31 Extended BNF : EBNF
/20
32 Extended BNF : EBNF
/20
33 BNF & EBNF
last slide
/20
34 Attribute Grammars
+ Static Semantic
/20
35 Static Semantics
/20
36 Static Semantics (2)
/20
37 Static Semantic (3)
/20
38 Attribute grammars
/20
39 Attribute Grammars (2)
/20
40 Attribute Grammars (3)
/20
41 Attribute Grammars (4): Ex.
/20
42 Attribute Grammars (5)
Intrinsic Attributes
Intrinsic attributes are synthesized attributes of leaf
nodes whose values are determined outside the parse
tree.
/20
43 Attribute Grammars (6)
Intrinsic Attributes
Given the intrinsic attribute values on a parse tree, the
semantic functions can be used to compute the
remaining attribute values
/20
44 Attribute Grammars (7): Ex.
Predicate functions, which
state the static semantic rules of the language, are associated with
grammar
rules.
/20
45 Dynamic Semantics:
Describing the Meanings of Programs
dynamic semantics
meaning, of the expressions, statements, and program
units of a programming language.
Recall
Because of the power and naturalness of the available
notation, describing syntax is a relatively simple matter.
BUT
No universally accepted notation or approach has been
devised for dynamic semantics.
/20
46 Dynamic Semantics:
Describing the Meanings of Programs
Solution?
There are several of the methods that have been
developed
Operational Semantics
Denotational Semantics
Axiomatic Semantics
Others
PAUSE
/20
We will return to this topic again
47
/20
48 Chaper 5:
Fundamental semantic issues of variables
Names, Bindings, and Scopes
Two primary components of Von Neumann computer
architecture
Memory
stores both instructions and data
Processor
provides operations for modifying the contents of the
memory
The abstractions in a language for the memory cells of
the machine are variables
/20
49 Abstraction for memory cells:
Variables
Name
Address
Type
Value
/20
50 Name
& its Designing issues
/20
51 Name
Special Words
Special Words
To make programs more readable by naming actions to
be performed
To separate the syntactic parts of statements and
programs
Special Words
Reserved word is a special word of a programming
language that cannot be used as a name.
One potential problem with reserved words: If the language
includes a large number of reserved words, the user may
have difficulty making up names that are not reserved. The
best example of this is
COBOL, which has 300 reserved words.
/20
52 Name
Special Words
Special Words
Reserved word
In most languages, names that are defined in other
program units, such as Java packages and C and C++
libraries, can be made visible to a program. These names
are predefined, but visible only if explicitly imported.
Once imported, they cannot be redefined.
Keyword
which means they can be redefined
EX. Fortran
/20
53 Abstraction for memory cells:
Variables
Name
Address
Type
Value
/20
54 Variable
Address
/20
55 Abstraction for memory cells:
Variables
Name
Address
Type
Value
/20
56 Variable
TYPE
/20
57 Abstraction for memory cells:
Variables
Name
Address
Type
Value
/20
58 Variable
Value
/20
59 Abstraction for memory cells:
Variables
Name
Address
Type
Value
Binding
/20
60
Binding
Binding times:
Design ex. Binding * to multiplication
Implementation ex. Binding a Size or range of values to a variable type
Compile ex. Binding a type to a variable in Java
Load ex. Binding a variable to a storage cell
Link ex. Binding a call to library subprogram to subprogram code
Run ex. Some value binding & some storage binding
/20
61 Binding
Example
count = count + 5;
Dynamic Binding
If the binding first occurs during run time
or can change in the course of program execution, it
is called
.
/20
63 Variables
Type Binding
Static type Binding
Explicit Declaration
Implicit Declaration
Implicit variable type binding is done by the language
processor, either a compiler or an interpreter.
Syntactic form of the variable’s name
detrimental to reliability
type inference
For example, in C# a var declaration of a variable must
include an initial value,
/20
64 Variables
Type Binding
Dynamic type Binding
Not Explicit Declaration
Not Implicit Declaration
the variable is bound to a type when it is assigned a
value in an assignment statement
(Such an assignment may also bind the variable to an
address and a memory cell, because different type
values may require different amounts of storage.)
more programming flexibility
Possibility of generic program
dealing with data of any numeric type
/20
65 Variables
Type Binding
Dynamic type Binding
Before the mid-1990s, the most commonly used
programming languages used static type binding, the
primary exceptions being some functional languages
such as Lisp
Since then there has been a significant shift to languages
that use dynamic type binding. In Python, Ruby,
JavaScript, and PHP, type binding is dynamic
Ex. list = [10.2, 3.5]; ………list = 47;……..
/20
66 Variables
Type Binding
Dynamic type Binding disadvantage 1
It causes programs to be less reliable
Less error detection capabitity
For example, suppose that in a particular JavaScript program, i
and x are currently the names of scalar numeric variables and y is
currently the name of an array. Furthermore, suppose that the
program needs the assignment statement
i = x;
but because of a keying error, it has the assignment statement
i = y;
In JavaScript (or any other language that uses dynamic type
binding), no error is detected in this statement by the
interpreter—the type of the variable named i is simply changed
to an array
/20
67 Variables
Type Binding
Dynamic type Binding disadvantage 2
COST
/20
68 Variables
Storage Binding & Lifetime
Allocation
The memory cell to which a variable is bound somehow
must be taken from a pool of available memory
Deallocation
the process of placing a memory cell that has been
unbound from a variable back into the pool of available
memory.
Lifetime
The time during which the variable is bound to a specific
memory location
The lifetime of a variable begins when it is bound to a
specific cell and ends when it is unbound from that cell
/20
69 Variables
Storage Binding & Lifetime
Selon storage binding of a variable and according to the
lifetime:
Scaler Variable
Static
stack-dynamic
explicit heap-dynamic
implicit heap-dynamic
/20
70 Variables
Scope
Visibility of a variable
/20