PLP LectureSlideChapter 2
PLP LectureSlideChapter 2
2
2.1) Formal methods for language processing
What is Syntax, Semantics and Pragmatics?
Syntax: The syntax of a program is the form of its declarations,
expressions, statements and program units.
A PL’s syntax is concerned with the form of programs: how expressions,
commands, declarations, and other constructs must be arranged to
make a well-formed program.
A syntax deals solely with the form and structure of symbols in a
language without any consideration of given to their meaning.
Generally,
3
2.1) Formal methods for language processing
What is Syntax, Semantics and Pragmatics?
Semantics: The semantic of a program is concerned with the
meaning of its program.
A PL’s semantics is concerned with the meaning of (well-formed)
programs: how a program may be expected to behave when
executed on a computer.
Generally,
4
2.1) Formal methods for language processing(Cont'd...)
6
2.2) BNF, Values and Types
Values and Types
Before Dealing with BNF: Let’s look at Values and Types.
A value is any entity that can be manipulated by a program:
evaluated, stored, passed as arguments, returned as function results,
and so on.
Values are grouped into types according to the operations that
may be performed on them. Different PLs support different types
of values (according to their intended application areas):
C: enumerands, integers, real numbers, structures, arrays, unions,
pointers to variables, pointers to functions.
Java: booleans, integers, real numbers, arrays, objects.
Haskell: booleans, characters, integers, real numbers, tuples,
disjoint unions, lists, recursive types.
7
2.2) BNF, Values and Types (Cont'd...)
Values and Types (Cont'd...)
8
2.2) BNF, Values and Types (Cont'd...)
Values and Types’ primitives (Cont'd...)
9
2.2) BNF, Values and Types (Cont'd...)
Values and Types’ primitives (Cont'd...)
10
2.2) BNF, Values and Types (Cont'd...)
Values and Types’ primitives (Cont'd...)
11
2.2) BNF, Values and Types (Cont'd...)
Values and Types’ primitives (Cont'd...)
12
2.2) BNF, Values and Types (Cont'd...)
Values and Types’ primitives (Cont'd...)
13
2.2) BNF, Values and Types (Cont'd...)
Composite Values and Types’ (Cont'd...)
14
2.2) BNF, Values and Types (Cont'd...)
Composite Values and Types’ (Cont'd...)
15
2.2) BNF, Values and Types (Cont'd...)
Composite Values and Types’ (Cont'd...)
16
2.2) BNF, Values and Types (Cont'd...)
Composite Values and Types’ (Cont'd...)
17
2.2) BNF, Values and Types (Cont'd...)
Composite Values and Types’ (Cont'd...)
18
2.2) BNF, Values and Types (Cont'd...)
Composite Values and Types’ (Cont'd...)
19
2.2) BNF, Values and Types (Cont'd...)
Composite Values and Types’ (Cont'd...)
20
2.2) BNF, Values and Types (Cont'd...)
Strings and Type systems:
21
2.2) BNF, Values and Types (Cont'd...)
Strings and Type systems (Cont'd...)
22
2.2) BNF, Values and Types (Cont'd...)
Static Vs Dynamic Typing
23
2.2) BNF, Values and Types (Cont'd...)
Static Vs Dynamic Typing(Cont'd...)
24
2.2) BNF, Values and Types (Cont'd...)
Static Vs Dynamic Typing(Cont'd...)
25
2.2) BNF, Values and Types (Cont'd...)
Expressions:
26
2.2) BNF and Grammar
27
2.2) BNF and Grammar(Cont'd...)
BNF (Cont’d…)
Definition: A grammar production has the form A W or A P
where A is a non-terminal symbol and w or p is a string of non-
terminal and terminal symbols.
A production is a rule for rewriting that can be applied to a string of
symbols called a sentential form. A production is interpreted as follows:
The nonterminal A can be replaced by P/W in a sentential form. The
symbol A is often called the left-hand side, while the string P/W is called the
right-hand side of the production. In BNF grammars, the sets of terminal and
non-terminal symbols are disjoint.
When a BNF grammar is used for defining PL syntax, the non-terminals N
identify the language's grammatical categories like Identifier, Integer,
Expression, Statement, and Program. The start symbol S identifies the
principal grammatical category being defined by the grammar (typically
Program), and is usually defined in the first production. The terminal symbols
T or ∑ form the basic alphabet from which programs are constructed.
28
2.2) BNF and Grammar(Cont'd...)
BNF (Cont’d…)
This pair defines a binaryDigit as either 0 or I, but nothing else. The non-terminal
symbols are all the symbols that appear on the left-hand side of at least one
production. For the above grammar, binaryDigit is the only non-terminal.
29
2.2) BNF and Grammar(Cont'd...)
BNF (Cont’d…)
The terminal symbols are all the other symbols that appear in the
productions;
For the above grammar, 0 and 1 are the terminal symbols.
When a series of productions all have the same nonterminal symbol
on their left hand sides, they may be combined into a single
production.
For example, the above two productions can be abbreviated by:
In this case, the alternatives are separated by a vertical bar (|), which
literally means "or," so the interpretation remains the same as the
original pair of productions. In this example, both the right arrow and
vertical bar are meta-symbols, which are symbols that are part of the
meta-language and are not part of the language being defined.
30
2.2) BNF and Grammar(Cont'd...)
BNF (Cont’d…)
All other symbols except the meta-symbols ::= and I were
interpreted as terminal symbols.
Thus, the grammar for an Integer would appear in BNF as.
31
2.2) BNF and Grammar(Cont'd...)
BNF- Derivations(Cont’d…)
To determine whether a particular string of symbols belongs to a
grammatical category, the production rules for that category can
be used to derive the string.
For example, suppose we want to determine if 352 is an Integer. To
do this, we can develop a derivation for this string using the
production rules of the grammar.
32
2.2) BNF and Grammar(Cont'd...)
BNF- Derivations(Cont’d…)
Technically, a derivation is a sequence of strings separated by the
symbol => in which at each step a non-terminal is replaced by the
right-hand side of one of its productions.
The first string in the series is the desired grammatical category and
the last is the string to be derived. The above sequence of steps,
therefore, is properly written as follows:
33
2.2) BNF and Grammar(Cont'd...)
BNF- Derivations(Cont’d…)
Here in the above derivation, each instance of => denotes the
application of a single production rule to transform a string one
step closer to the string to be derived.
Each string in such a derivation is called a sentential form, which can
contain both terminal and non-terminal symbols. For instance, the
sentential form 3 Digit Digit occurs in the fourth step of this derivation.
34
2.2) BNF and Grammar(Cont'd...)
BNF- Derivations(Cont’d…)
We can now define precisely the meaning of the term language
from a purely syntactic point of view:
Definition: The language L defined by a BNF grammar-G is the set of all
terminal strings that can be derived from the start symbol.
35
2.2) BNF and Grammar(Cont'd...)
BNF- Parse Trees (Cont’d…)
Another way to demonstrate that a particular string is a member
of the language defined by a BNF grammar is to describe the
derivation in graphical form. This form is called a parse tree, in
which each derivation step corresponds to a new sub-tree.
For example, the derivation step:
Can be written as the sub-tree:
37
2.2) BNF and Grammar(Cont'd...)
BNF- Parse Trees (Cont’d…)
A parse tree is preferred when the grammatical structure is more
complex. Derivation is a simple linear representation of a parse
tree, and is often more helpful when the string being derived does
not possess an interesting grammatical structure.
Consider the grammar Go, which defines the language of
arithmetic expressions having the operators + and - and one-
digit integer operands:
42
2.2) BNF and Grammar(Cont'd...)
SYNTAX OF A SMALL LANGUAGE: CLITE
43
2.3) Regular expressions and Grammar
Grammar has dealt with BNF but the regular expression is left as
a reading assignment.
Reading
Assignment!
44
2.4) Lexical and Syntactic analysis
Lexical analysis
The grammar GClite has two levels:
1) Lexical level
2) Syntactic level
45
2.4) Lexical and Syntactic analysis (Cont'd...)
46
2.4) Lexical and Syntactic analysis (Cont'd...)
47
2.4) Lexical and Syntactic analysis (Cont'd...)
Syntactic analysis:
Definition: The purpose of the syntactic analyzer, or parser, is to
construct a parse tree using as input the stream of tokens provided
by the lexer.
The output of the parser is usually an abstract syntax tree. The
motivation for using an abstract syntax tree, rather than a parse
tree, as illustrated sample program bellow:
48
2.4) Lexical and Syntactic analysis (Cont'd...)
Syntactic and lexical analysis(Cont’d…)
Generally,
49
2.5) Semantics
Semantics:
Definition: Semantics exactly deals with meaning.
The main motivations for precisely defining the semantics of a
programming language are three fold:
1) To provide programmers with an authoritative definition of the
meaning of all the language constructs.
2) To provide compiler writers with an authoritative definition of
the meaning of all constructs, thus avoiding implementation
differences.
3) To provide a basis for standardizing the language.
NB: A programming language is well-defined only when its
semantics, as well as its syntax and type system.
50
2.5) Semantics(Cont'd...)
52
2.5) Semantics(Cont'd...)
53
2.5) Semantics(Cont'd...)
54
//End Chap-2
Question?
Suggestion!