Algorithm

advertisement
Grammars
Help to
determine ??
Determine
Syntax
4/13/2015
Ambiguities
!=
Semantics
IT 327
1
Ambiguity in English
I saw an airplane with a telescope.
I saw a girl with her boyfriend.
I saw a boy scout with a telescope
on the mountain top.
I saw a pretty girl with a telescope
on the mountain top.
4/13/2015
IT 327
2
Ambiguity in Programming
Languages
if (a > 2)
if (b > 1)
b++;
else
a++;
Dangling else
if (a > 2)
if (b > 1)
b++;
else
a++;
4/13/2015
IT 327
3
Three “Equivalent” Grammars
G1:
<subexp> ::= <subexp> - <subexp> | a | b | c
G2:
<subexp>
<var>
::= <var> - <subexp> | <var>
::= a | b | c
G3:
<subexp>
<var>
::= <subexp> - <var> | <var>
::= a | b | c
They are equivalent! (what does that mean?)
4/13/2015
IT 327
4
Parse
a – b - c
using G1
<subexp> ::= <subexp> - <subexp> | a | b | c
G1:
<subexp>
<subexp>
a
-
<subexp>
<subexp>
-
<subexp>
<subexp> - <subexp> <subexp> - <subexp>
b
c
a
<subexp>
c
b
G1 is an ambiguous grammar
4/13/2015
IT 327
5
Left and Right Association rules
Right associative rule:
a-b-c-d = a–(b-(c-d))
Left associative rule:
a-b-c-d = ((a–b)-c)-d
4/13/2015
IT 327
6
G2: <subexp> ::= <var> - <subexp> | <var>
<var>
::= a | b | c
G3: <subexp> ::= <subexp> - <var> | <var>
<var> ::= a | b | c
<subexp>
<subexp>
<var>
a
-
<subexp>
<subexp>
<var>
b
<subexp> -
- <subexp>
<var>
<var>
c
<var>
<var>
c
b
a
Right associative rule:
a-b-c = a-(b-c)
4/13/2015
-
Left associative rule:
a-b-c = (a-b)-c
IT 327
7
A grammar for Arithmetic Expressions
<exp> ::= <exp> + <exp> |
<exp> * <exp> |
( <exp> ) |
a | b | c
Example: ((a+b)*c)
4/13/2015
IT 327
8
What is the meaning of a+b*c
We prefer this
Using left--most
derivation
<exp>
<exp>
<exp> * <exp>
<exp> + <exp>
<exp> + <exp>
a
c
a
<exp> * <exp>
b
b
c
Using right-most derivation doesn’t help
4/13/2015
IT 327
9
The problem is too much freedom
<exp>
<exp>
<exp> * <exp>
<exp> + <exp>
<exp> + <exp>
a
b
c
a
Call this a term
<exp> * <exp>
b
And term does not
generate exp directly
c
Solution: restricting <exp> from generating * directly.
A term is an exp: a*b
An exp may not be a term: a+b
4/13/2015
IT 327
<exp> ::= <term>
<term> ::= <exp>
<term> ::= (<exp> )
10
An unambiguous grammar for Arithmetic Expressions
<exp>
<exp> ::= <term> + <exp> | <term>
<term> ::= <fact> * <term> | <fact>
<fact> ::= ( <exp> ) |
a | b | c
<term> + <exp>
<fact>
Example: a+b*c
a
<term>
<fact> * <term>
b
<fact>
c
4/13/2015
IT 327
11
An unambiguous grammar for Arithmetic Expressions
<exp> ::= <term> + <exp> | <term>
<exp>
<term> ::= <fact> * <term> | <fact>
<term> + <exp>
<fact> ::= ( <exp> ) |
a | b | c
<fact>
a
Example: a+b+c
Right association
a+b+c = a+(b+c)
4/13/2015
<term> + <exp>
<fact>
<term>
b
<fact>
c
IT 327
12
Using ( and ) to alter the order
<exp>
<exp> ::= <term> + <exp> | <term>
<term> ::= <fact> * <term> | <fact>
<term> + <exp>
<fact> ::= ( <exp> ) |
a | b | c
(
Example: (a+b)+c
<exp>
)
<term> + <exp>
a
4/13/2015
<term>
<fact>
IT 327
<fact>
c
b
13
An unambiguous left associative grammar (?) for Arithmetic Exp.
No such thing called left associative grammar
<exp>
A better way to say this:
A grammar for left associative arithmetic exp. <exp> +
<term>
<exp> ::= <exp> + <term> | <term>
<term> ::= <term> * <fact> | <fact>
<exp> +
<term>
<fact>
<term>
<fact>
c
<fact>
b
<fact> ::= ( <exp> ) |
a | b | c
Example: a+b+c
a
4/13/2015
IT 327
14
Too much freedom to generate
Dangling else
if statements from stmt
<stmt> ::= <if-stmt> | s1 | s2
<if-stmt> ::= if <expr> then <stmt> else <stmt>
| if <expr> then <stmt>
<expr> ::= e1 | e2
if e1 then if e2 then s1 else s2
if (a > 2)
if (b > 1)
b++;
else
a++;
4/13/2015
if (a > 2)
if (b > 1)
b++;
else
a++;
IT 327
15
<if-stmt>
if
<exp>
then
e1
if
<exp>
<stmt>
else
s2
<if-stmt>
then
<stmt>
e2
s1
<if-stmt>
if
<exp>
then
e1
if
<exp>
e2
4/13/2015
<stmt>
if (a > 2)
if (b > 1)
b++;
else
a++;
then
<stmt>
<if-stmt>
<stmt>
s1
else
if (a > 2)
if (b > 1)
b++;
else
a++;
<stmt>
s2
IT 327
Most languages: else goes with
nearest then
16
Eliminating The Ambiguity
<stmt> ::= <if-stmt> | s1 | s2
Numbers of then and
else are the same
<if-stmt> ::= if <expr> then <full-stmt> else <stmt>
| if <expr> then <stmt>
<expr> ::= e1 | e2
<full-stmt> ::= <full-if> | s1 | s2
<full-if> ::= if <expr> then <full-stmt> else <full-stmt>
4/13/2015
IT 327
17
A Parse Tree for if-stmt
<if-stmt>
if
<exp>
then
e1
if
<exp>
e2
4/13/2015
then
<stmt>
<if-stmt>
<full-stmt>
s1
IT 327
else
<stmt>
s2
18
Languages That Don’t Dangle
Algol: then part can’t be another if (a block
is allowed)
Ada: if statement must be terminated with an
end if
4/13/2015
IT 327
19
Options to deal with ambiguity
• 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
4/13/2015
IT 327
20
Abstract 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
4/13/2015
IT 327
21
parse tree
<exp>
<exp>
<mulexp>
<rootexp>
+
<exp>
+
<mulexp>
<mulexp>
<rootexp>
c
<rootexp>
b
+
a
+
c
abstract syntax tree
a
4/13/2015
IT 327
b
22
Operators
• 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
4/13/2015
IT 327
23
Operands
• Operands are the inputs to an operator, like
1 and 2 in the expression 1+2
• Unary operators take one operand: -1
• Binary operators take two: 1+2
• Ternary operators take three: a?b:c
4/13/2015
IT 327
24
Operator Position
• infix: a + b
• prefix: + a b
• postfix: a b +
4/13/2015
IT 327
25
Operator Precedence
The priority in In the competition of getting operands.
Most languages put * at a higher precedence level than +.
a+b*c = a+(b*c)
+
(sign, as unary operators)
-
^
*
/
+
-
%
:=
4/13/2015
IT 327
26
Precedence Examples in C
a + b * c
a = b < c ? * p + b * c : 1 << d ()
a <= 0 || 100 <= a
4/13/2015
IT 327
27
Some 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
4/13/2015
IT 327
28
Download