Documente Academic
Documente Profesional
Documente Cultură
Defining
Rewriting:
Expression Grammars
IF-THEN-ELSE
Declarations
Expression Grammars
precedence
associativity
precedence
Associativity
Ambiguity = program
structure not uniquely defined
E E+E | E*E | (E) | id
String id * id + id has two parse trees:
E
E +
* E
id
id E
+ E
id
id
id
id
Ambiguity
Ambiguity is BAD
Expression Grammars
(precedence)
E E - E | E / E | ( E ) | id
rewrite to
E E-E | T
T T/T | F
F id | ( E )
Example
parse tree for id id / id
E E-E | T
T T/T | F
F id | ( E )
E
-
E
T
T
F
id
/ T
id
id
Example: Preventing
Ambiguity
Associativity
Recursion
X + X
X + X
X + X
The expression
if E1 then if E2 then E3 else E4
has two parse trees
if
E1
if
E4
if
E2
E1
E3
if
E2
E3
E4
if
if
E1
E2
E3
E1
E4
E4
if
E2
E3
Associativity Declarations
E
int
int
int
int
int
E
int
Precedence Declarations
E
int
+ E
int
E
E
int
int E
int
E
*
E
int