HANDBOOK / GENTLE PRIMER / Describing Syntax /

# Attributes

Nonterm and token predicates may have parameters that are declared and used in the same way as with other predicates, except that for grammar symbols only output parameters are supported.

Given a structuring of the input into phrases (a parse), each nonterm application corresponds to a particular part of the input. The parameter values for that nonterm are often called attributes of the construct that corresponds to the nonterm application. Attributes can be computed from the attributes of constituents.

Here is a classical grammar that computes the value of a binary number:

       'root' bits(-> N) print(N)

       'nonterm' bits(-> INT)
       'rule' bits(-> B): bit(-> B)
       'rule' bits(-> N*2+B): bits(-> N) bit(-> B)

       'nonterm' bit(-> INT)
       'rule' bit(-> 0): "0"
       'rule' bit(-> 1): "1"

Consider the input

       1 0 1

which is decomposed by the second rule into `bits` (`1 0`) with value `N` = 2 and `bit` (`1`) with value `B` = 1. The attribute of the whole sequence is computed as `N*2+B` = 5.