Pretende-se criar uma gramática atributiva que calcule no símbolo inicial o valor das expressões fornecidas. As expressões são codificadas
O operador @ tem precedência superior aos operadores + (soma) e - (subtracção). Todos os operadores são associativos à esquerda. Podem ser utilizados parêntesis para alteração das precedências.
Exemplo: à sequência ((1@33 + 34)@20 + 70@15 + 2)@10 corresponde o valor 1.9366.
Since + and - have the same precedence, they are defined in the same rule (E), and are defined so that they operate on symbols which may contain the @ operator. Since all operators are left-associative, the corresponding rules in the grammar are left-recursive.
E0 -> E1 + T { E0.val = E1.val + T.val; } E0 -> E1 - T { E0.val = E1.val + T.val; } E -> T { E.val = T.val; } T0 -> T1 @ F { T0.val = T1.val * F.val / 100; } T -> F { T.val = F.val; } F -> ( E ) { F.val = E.val; } F -> N { F.val = N.val; } N0 -> N1 DIG { N0.val = N1.val * 10 + DIG.val; } N -> DIG { N.val = DIG.val; }
The following is the annotated tree.