m |
|||
Line 93: | Line 93: | ||
== Solution == | == Solution == | ||
+ | |||
+ | === Semantic tree and dependency graph === | ||
+ | |||
+ | "x" is an inherited attribute (propagation in blue); "val" is a synthesized attribute (propagation in red). | ||
+ | |||
+ | Temporary handwritten solution. | ||
+ | |||
+ | [[Image:attrgrams-ex02-semantic-tree-1.png]] | ||
+ | |||
+ | === Attribute grammar using only synthesized attributes === | ||
+ | |||
+ | We start by noting that the semantic computed by the previous grammar corresponds to base 5-like numbering (although the digits do not belong to base 5). | ||
+ | |||
+ | The new grammar is trivial to write: | ||
+ | |||
+ | <text> | ||
+ | |||
+ | </text> | ||
+ | |||
+ | "val" is a synthesized attribute (propagation in red). | ||
+ | |||
+ | Temporary handwritten solution. | ||
+ | |||
+ | [[Image:attrgrams-ex02-semantic-tree-2.png]] | ||
[[category:Compilers]] | [[category:Compilers]] | ||
[[category:Teaching]] | [[category:Teaching]] |
Considere a seguinte gramática atributiva:
s | -> | symbol 1 # symbol 2 | { | |
s. val = symbol 1. val + symbol 2. val | ||||
symbol 1. x = 1 | ||||
symbol 2. x = 1 | ||||
} | ||||
symbol 1 | -> | symbol 2 dig | { | |
symbol 2. x = symbol 1. x * 5 | ||||
symbol 1.val = symbol 2.val + dig.val * symbol 1.x | ||||
} | ||||
symbol | -> | dig | { | |
symbol.val = dig.val * symbol.x | ||||
} |
O elemento lexical dig representa um dígito de 0 a 9, e o seu atributo val representa o valor numérico correspondente.
"x" is an inherited attribute (propagation in blue); "val" is a synthesized attribute (propagation in red).
Temporary handwritten solution.
We start by noting that the semantic computed by the previous grammar corresponds to base 5-like numbering (although the digits do not belong to base 5).
The new grammar is trivial to write:
<text>
</text>
"val" is a synthesized attribute (propagation in red).
Temporary handwritten solution.