Title: Syntax Semantics
1Syntax -- Semantics
Grammar
Determines ??
Help??
Determines
Ambiguities
2Ambiguity in English
I saw an airplane with a telescope.
I saw a girl with her boyfriend.
I saw a scout with a telescope on the mountain
top.
I saw a pretty girl with a telescope on the
mountain top.
3Ambiguity in Programming Languages
if (a 2) if (b 1) b else
a
Dangling else
if (a 2) if (b 1) b else a
4Three Equivalent Grammars
G1 - a b c
G2 -
a b c
G3 -
a b c
They are equivalent! (what does that mean?)
5Parse a-b-c using G1
G1 - a b c
-
-
a
-
-
c
b
c
a
b
G1 is an ambiguous grammar
6Left and Right Association rules
Right associative rule a-b-c-d a (b-(c-d))
Left associative rule a-b-c-d ((ab)-c)-d
7G2 and G3 Grammars
G2 -
a b c G3
- a b c
Right associative rule
Left associative rule a-b-c-d ((ab)-c)-d
8A grammar for Arithmetic Expressions
( ) a
b c
Example ((ab)c)
9What is the meaning of abc
c
a
b
c
a
b
10An unambiguous grammar for Arithmetic Expressions
( )
a b c
a
Example abc
b
c
11An unambiguous grammar for Arithmetic Expressions
( )
a b c
a
Example abc
Right association
b
abc a(bc)
c
12Alter the order
( )
a b c
( )
Example (ab)c
c
a
b
13An unambiguous left associative grammar for
Arithmetic Exp.
No such thing called left associative grammar
Better way to say this A grammar for left
associative arithmetic exp.
( )
a b c
c
Example abc
b
a
14Dangling else
s1 s2 if
then else if
then e1 e2
if e1 then if e2 then s1 else s2
if (a 2) if (b 1) b else
a
if (a 2) if (b 1) b else a
15if (a 2) if (b 1) b else a
if (a 2) if (b 1) b else
a
Most languages else goes withnearest then
16Eliminating The Ambiguity
s1 s2 if
then else
if then e1 e2
then-else are all matched
s1 s2
if then else
17A Parse Tree for if-stmt
18Languages That Dont Dangle
- Algol then part cant be another if (a block is
allowed) - Ada if statement must be terminated with an end
if
19Options
- Rewrite grammar to eliminate ambiguity
- Leave ambiguity but explain in accompanying text
how things like associativity, precedence, and
the dangling else should be parsed - Do both in separate grammars
20Abstract Syntax Tree
- Abbreviated version of the parse tree
- Details are implementation-dependent
- Usually, there is a node for every operation,
with a subtree for every operand
Most systems construct an AST instead
21parse tree
abstract syntax tree
22Operators
- Special symbols for frequently-used simple
operations like addition, subtraction,
multiplication and division - Usually predefined, but not always
- Usually a single token, but not always
23Operands
- Operands are the inputs to an operator, like 1
and 2 in the expression 12 - Unary operators take one operand -1
- Binary operators take two 12
- Ternary operators take three a?bc
24Operator Position
- infix a b
- prefix a b
- postfix a b
25Operator Precedence
- Most languages put at a higher precedence level
than . - abc a(bc)
(sign, as unary operators)
-
/
-
26Precedence Examples in C
a b c
a b a
27Some conclusions from the textbook
with (my view)
- Grammars define syntax, and more (more?)
- They define not just a set of legal programs, but
the (a) parse tree for each program - The structure of a parse tree corresponds to the
order (entity) in which different parts of the
program are to be executed (as an entity) - Thus, grammars contribute (a little) to the
definition of semantics