An attribute grammar is an SDD without side effects, which are modifications of a non-local environment such as printing a value.The following SDD (attribute grammar) evaluates arithmetic expressions with operators + and * terminated by an endmarker
n
.
For example, the expression “3 * 5 + 4 n
” is evaluated to 19.
In the SDD, each of the nonterminals has a single attribute, called Val .
Explanations of some productions are
|
|
Val(<L>)
to Val(<E>)
, which is the numerical value of the entire expression.
<E> ::= <E1> + <T>
, has one rule, which computes the Val
attribute for the head <E>
as the sum of the values at <E1>
and <T>
.
At any parse-tree node N
labeled <E>
, the value of Val
for <E>
is the sum of the values of Val
at the children of node N
labeled <E>
and <T>
.
digit
has an attribute Lexval
, which is an integer value returned by the lexical analyzer.
The analyzer is software converting a sequence of characters into a sequence of tokens.