Professional Documents
Culture Documents
LANGUAGES
ECEg - 4182
by
Dr. T.R.SRINIVASAN, B.E., M.E., Ph.D.,
Assistant Professor,
Faculty of Electrical and Computer Engineering,
Jimma Institute of Technology,
Jimma University,
Jimma, Ethiopia
1
Defining Program Syntax and
semantics
Chapter 2
Defining a Programming Language
• Defining a programming language requires
specifying its syntax and its semantics.
• Syntax:
• The form or structure of the expressions, statements,
and program units.
• Example: if (<exp>) then <statement>
• Semantics:
• The meaning of the expressions, statements, and
program units.
• Example: if the value of <exp> is non-zero, then
<statement> is executed otherwise omitted.
3
Syntax and Semantics
• There is universal agreement on how to
express syntax.
4
Who needs language definitions?
• Other language designers
• To evaluate whether or not the language
requires changes before its initial
implementation and use.
• Programmers
• To understand how to use the language to solve
problems.
• Implementers
• To understand how to write a translator for the
language into machine code (compiler)
5
Language Sentences
• A sentence is a string of characters over some alphabet.
6
Recognizers vs. Generators
• Recognizers:
• Accept a sentence, and return true if the sentence is in the
language.
• Similar to syntactic analysis phase of compilers.
• Generators
• Push a button, and out pops a legal sentence in the language.
7
Definition of a BNF Grammar
8
Definition of a BNF Grammar
• A set of terminal symbols
• Example: { "a" "b" "c" "(" ")" "," }
• A set of non-terminal symbols
• Example: { <prog> <stmt> }
• A set of productions
• Syntax: A single non-terminal, followed by a
"->", followed by a sequence of terminals and non-
terminals.
• Example: <prog> -> "begin" <stmt_list> "end"
• A distinguished non-terminal, the Start Symbol
• Example: <prog>
9
Example BNF Grammar
• Productions
• <prog> -> "begin" <stmt_list> "end"
• <stmt_list> -> <stmt>
• <stmt_list> -> <stmt> ";" <stmt_list>
• <stmt> -> <var> ":=" <exp>
• <var> -> "a"
• <var> -> "b"
• <var> -> "c"
• <exp> -> <var> "+" <var>
• <exp> -> <var> "-" <var>
• <exp> -> <var>
10
Extended BNF
• EBNF extends BNF syntax to make grammars more
readable.
• EBNF does not make BNF more expressive, it’s a
short-hand.
• Sequence :
• <if> -> "if "<test> "then" <stmt>
• Optional – [ ] :
• <if> -> "if "<test> "then" <stmt> ["else" <stmt>]
• Alternative – | :
• <number> -> <integer> | <real>
• Group – ( ) :
• <exp> -> <var> | ( <var> "+" <var> )
• Repetition – { }:
• <ident_list> -> <ident> { "," <ident>}
11
Generation
12
Example sentence generation
• begin <stmt_list> end
• begin <stmt> end
• begin <var> := <exp> end
• begin b := <exp> end
• begin b := <var> end
• begin b := c end
<stmt>
<var> := <exp>
<var>
b
c
14
Recognition
• Grammar can also be used to test if a sentence is in
the language. This is “recognition”.
16
Attribute Grammars: Definition
20
Annotated Parse Tree
21
Basic Idea of Attribute Grammars
• Take a BNF parse tree and add values to
nodes.
22
Attribute Grammar Example
• This is not a “real” example.
• BNF:
• <proc> -> procedure <proc_name>
<proc_body>
end <end_name>;
• Semantic rule:
• <proc_name>.string = <end_name>. string
• Attributes:
• A “string” attribute value is computed and attached to
<proc_name> and <end_name> during parsing.
23
Syntax And Semantics
24
An English Grammar
A sentence is a noun <S> ::= <NP> <V> <NP>
phrase, a verb, and a
noun phrase.
25
How The Grammar Works
• The grammar is a set of rules that say how
to build a tree—a parse tree
• You put <S> at the root of the tree
• The grammar’s rules say how children can
be added at any point in the tree
• For instance, the rule
<S> ::= <NP> <V> <NP>
says you can add nodes <NP>, <V>, and
<NP>, in that order, as children of <S>
26
A Parse Tree
<S>
27
A Programming Language Grammar
28
A Parse Tree
<exp>
( <exp> )
((a+b)*c)
<exp> * <exp>
( <exp> ) c
<exp> + <exp>
a b
29
Sample Grammar
• <Sum> ::= 0
• <Sum >::= 1
• <Sum> ::= <Sum> + <Sum>
• <Sum> ::= (<Sum>)
30
Sample Grammar
• Terminals: 0 1 + ( )
• Non-terminals: <Sum>
• Start symbol = <Sum>
<Sum> ::= 0
<Sum >::= 1
<Sum> ::= <Sum> + <Sum>
<Sum> ::= (<Sum>)
Can be abbreviated as (using EBNF)
<Sum> ::= 0 | 1 | <Sum> + <Sum> | (<Sum>)
31
BNF Derivations
• Generate ( 0 + 1 ) + 0
<Sum> =>
32
BNF Derivations
• Pick a non-terminal
<Sum> =>
33
BNF Derivations
34
BNF Derivations
• Pick a non-terminal:
35
BNF Derivations
•Pick a rule and substitute:
• <Sum> ::= ( <Sum> )
36
BNF Derivations
• Pick a non-terminal:
37
BNF Derivations
•Pick a rule and substitute:
• <Sum> ::= <Sum> + <Sum>
38
BNF Derivations
• Pick a non-terminal:
39
BNF Derivations
•Pick a rule and substitute:
• <Sum >::= 1
40
BNF Derivations
• Pick a non-terminal:
41
BNF Derivations
•Pick a rule and substitute:
• <Sum >::= 0
42
BNF Derivations
• Pick a non-terminal:
43
BNF Derivations
•Pick a rule and substitute
• <Sum> ::= 0
46
start symbol
<S> ::= <NP> <V> <NP>
a production
<NP> ::= <A> <N>
tokens
47
Example
<exp> ::= <exp> + <exp> | <exp> * <exp> | ( <exp> )
| a | b | c
49
Parse Trees
51
Language Definition
• We use grammars to define the syntax of
programming languages
• The language defined by a grammar is the
set of all strings that can be derived by
some parse tree for the grammar
• As in the previous example, that set is often
infinite
• Constructing grammars is a little like
programming...
52
Constructing Grammars
• Most important trick: divide and conquer
• Example: the language of Java declarations:
a type name, a list of variables separated by
commas, and a semicolon
• Each variable can be followed by an
initializer:
float a;
boolean a,b,c;
int a=1, b, c=1+2;
53
Example, Continued
54
Example, Continued
55
Example, Continued
56
BNF Semantics
57
Remember Parse Trees
58
Example
• Consider grammar:
<exp> ::= <factor>
| <factor> + <factor>
<factor> ::= <bin>
| <bin> * <exp>
<bin> ::= 0 | 1
59
Example cont.
• 1 * 1 + 0: <exp>
60
Example cont.
• 1 * 1 + 0: <exp>
<factor>
61
Example cont.
• 1 * 1 + 0: <exp>
<factor>
<bin> * <exp>
62
Example cont.
• 1 * 1 + 0: <exp>
<factor>
<bin> * <exp>
1 <factor> + <factor>
67
Three “Equivalent” Grammars
G1: <subexp> ::= a | b | c | <subexp> - <subexp>
68
<subexp>
<var> - <subexp>
b <var>
<subexp>
<subexp> - <var>
G3 parse tree: c
<subexp> - <var>
<var> b
69
Why Parse Trees Matter
• We want the structure of the parse tree to
correspond to the semantics of the string it
generates
• This makes grammar design much harder: we’re
interested in the structure of each parse tree, not
just in the generated string
• Parse trees are where syntax meets semantics
70
Outline
• Operators
• Precedence
• Associativity
• Ambiguities
71
Operators
72
Operator Terminology
73
More Operator Terminology
74
Working Grammar
75
Issue #1: Precedence
<exp>
<exp> * <exp>
<exp> + <exp> c
a b
76
Operator Precedence
77
Precedence In The Grammar
G4: <exp> ::= <exp> + <exp>
| <exp> * <exp>
| (<exp>)
| a | b | c
78
Correct Precedence
<exp>
<exp> + <exp>
a <mulexp> * <mulexp>
b c
79
Issue #2: Associativity
<exp> <exp>
b c a b
80
Operator Associativity
81
Associativity Examples
• C
• ML
• Fortran
83
Correct Associativity
<exp>
<exp> + <mulexp>
<mulexp> <rootexp> c
<rootexp> b
85
Example: Ambiguous Grammar
• 0+1+0
<Sum> <Sum>
<Sum> + <Sum> <Sum> + <Sum>
<Sum> + <Sum> 0 0 <Sum> + <Sum>
0 1 1 0
86
Two Major Sources of
Ambiguity
• Lack of determination of operator
precedence
• Lack of determination of operator
associatively
87
Issue #3: Ambiguity
• G4 was ambiguous: it generated more than one
parse tree for the same string
• Fixing the associativity and precedence problems
eliminated all the ambiguity
• This is usually a good thing: the parse tree
corresponds to the meaning of the program, and
we don’t want ambiguity about that
• Not all ambiguity stems from confusion about
precedence and associativity...
88
Dangling Else In Grammars
<stmt> ::= <if-stmt> | s1 | s2
<if-stmt> ::= if <expr> then <stmt> else <stmt>
| if <expr> then <stmt>
<expr> ::= e1 | e2
and the next slide shows two different parse trees for it...
89
<if-stmt>
e1 <if-stmt> s2
e2 s1
<if-stmt>
e2 s1 s2
Eliminating The Ambiguity
<stmt> ::= <if-stmt> | s1 | s2
<if-stmt> ::= if <expr> then <stmt> else <stmt>
| if <expr> then <stmt>
<expr> ::= e1 | e2
91
Eliminating The Ambiguity
<stmt> ::= <if-stmt> | s1 | s2
<if-stmt> ::= if <expr> then <full-stmt> else <stmt>
| if <expr> then <stmt>
<expr> ::= e1 | e2
The effect is that the new grammar can match an else part
with an if part only if all the nearer if parts are already
matched.
92
Correct Parse Tree
<if-stmt>
e1 <if-stmt>
e2 s1 s2
93
Conclusion
• Grammars define syntax, and more
• They define not just a set of legal programs, but a
parse tree for each program
• The structure of a parse tree corresponds to the
order in which different parts of the program are
to be executed
• Therefore, grammars contribute to the definition
of semantics
94
End of Syntax and Semantics
95
Quiz
• Try to figure out what kind of problem is found in
this grammar and try to fix it
<Sum> ::= 0
<Sum >::= 1
<Sum> ::= <Sum> + <Sum>
<Sum> ::= (<Sum>)
Can be abbreviated as (using EBNF)
<Sum> ::= 0 | 1 | <Sum> + <Sum> | (<Sum>)
96
Solution
97