0% found this document useful (0 votes)
5 views55 pages

PLP LectureSlideChapter 2

Uploaded by

Kalayu Redae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views55 pages

PLP LectureSlideChapter 2

Uploaded by

Kalayu Redae
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Mekelle University

Mekelle Institute of Technology


Department of Information Technology

Programming Languages and Paradigms

2013 E.C, sem: I


Year: II

By: Mezgebe Mehari


mezgebem@gmail.com
0914 21 12 72
Chapter – TWO
Syntax and Semantics

2.1. Formal methods for language processing


2.2. BNF, Values and Types
2.3. Grammars, regular expressions
2.4. Lexical and Syntactic analysis
2.5. Semantics

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

Pragmatics: include the paradigm(s) supported by the PL.


A PL’s pragmatics is concerned with the way in which the PL is
intended to be used in practice.
The syntax of a language can be largely specified using a
formalism called a grammar.
A grammar is written in a language-description-language, or
meta-language, and its purpose is to define all the legal strings
of characters that can form a syntactically valid program.
The language used to define other language is called meta-
language. Eg: BNF(Context free grammar), Regular expressions
Grammars are a meta-language based on a formal theory
developed by the linguist Noam Chomsky[1957], which defined
four levels of grammar, known as regular, context-free, context-
sensitive, and unrestricted.
5
2.1) Formal methods for language processing(Cont'd...)
A context-free grammar has a set of productions P, a set of
terminal symbols T or ∑, and a set of nonterminal symbols N, one
of which, S, is distinguished as the start symbol. Generally,

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

BNF (Backus-Naur Form)


BNF was adapted from Chomsky's theory by John Backus and
Peter Naur to express a formal syntactic definition for
programming languages.
Like many texts, we use the term BNF grammar as a synonym
for contextfree grammar but it’s one form of context-free
grammar that been widely used to define the syntax of
programming languages.

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…)

To illustrate these ideas, here is a pair of productions that defines


the syntax of the grammatical category Binary Digit:

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:

The root of each sub-tree in a parse tree is the node corresponding


to the non-terminal for which the substitution is being made in a
derivation, and the immediate children in left-to-right order
correspond to the right-hand side of the production used in that
substitution.
36
2.2) BNF and Grammar(Cont'd...)
BNF- Parse Trees (Cont’d…)
A complete parse tree for the derivation of the string 352 is shown
in bellow Figure.
Note that once the parse tree is drawn, the order of the derivation
steps is lost; both the leftmost and rightmost derivations result in the
same 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:

Definition: A grammar is ambiguous if its language contains at


least one string with two or more distinct parse trees.
A parse tree for the string 5 - 4 + 3 for grammar Go is given in
Figure bellow:
38
2.2) BNF and Grammar(Cont'd...)
BNF- Parse Trees (Cont’d…)
The structure of this parse
tree is somewhat more
interesting than for
Grammar Ginteger·
A left-to-right interpretation
of this tree leaves suggests
the expression is evaluated as though it had been written (5 - 4) + 3.
This interpretation yields a different numerical result than if we
calculate 5 - (4 + 3). This reflects the fact that the left operand of
the operator + is an expression, which subsequently derives the
string 5 - 4. Similarly, the string 3, which is derived from the Term
to the right of the operator +, is its right operand.
39
2.2) BNF and Grammar(Cont'd...)
SYNTAX OF A SMALL LANGUAGE: CLITE
The ideas discussed in this chapter provide a basis for describing
the syntax of a complete programming language.
However, so as not to be overwhelmed by the details, we have
chosen to define a subset of C, a language that we call Clite. This
language should be familiar to anyone who has written a program
in C, C++ or Java.
A complete grammar for Clite,
Called GClite is shown in the Figure bellow:

continued on the next page…


40
2.2) BNF and Grammar(Cont'd...)
SYNTAX OF A SMALL LANGUAGE: CLITE

continued on the next page…


41
2.2) BNF and Grammar(Cont'd...)
SYNTAX OF A SMALL LANGUAGE: CLITE

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

The last grammar rules starting with the definition of an


Identifier, address lexical issues.
They define the syntax of an Identifier and various forms of
Literal.
In addition, all the keywords and other terminal symbols in
the production rules of Gctite are also part of the lexical
level.

45
2.4) Lexical and Syntactic analysis (Cont'd...)

Lexical analysis (Cont’d…)


The terminal strings that are derivable in the lexical syntax are
called tokens, and they are classified into the following groups:

46
2.4) Lexical and Syntactic analysis (Cont'd...)

Lexical analysis (Cont’d…)


Definition: A token is a logically cohesive sequence of characters
representing a single symbol. E.g: While

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

Semantic Analysis (Cont’d…):


Three different approaches have been used extensively to define
the semantics of a programming language:
1) Operational semantics
2) Axiomatic semantics
3) Denotational semantics
The most straightforward idea of program meaning is dealt with
operational.
The meaning of each statement in the language is dealt with
Axiomatic while denotational o define the meaning of each
type of statement that occurs in the (abstract) syntax as a
state-transforming mathematical function
51
2.5) Semantics(Cont'd...)

52
2.5) Semantics(Cont'd...)

53
2.5) Semantics(Cont'd...)

54
//End Chap-2

Question?
Suggestion!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy