This explains informally how to annotate a Jacc grammar for XML serialization of the AST.
Essentially, the rule format of Jacc is that of Yacc. As in Yacc, Jacc rules may be annotated with semantic actions, in the form of code involving the rule's RHS constituents (denoted by $1, $2, ..., $n - the so-called pseudo-variables - where the index n in $n refers to the position of a constituent of the rule's RHS). Such actions appear between curly braces ('{' and '}') wherever a symbol may appear in a rule's RHS.
Jacc also allows an additional form of annotation in the RHS of a rule to indicate the XML serialization pattern of the abstract syntactic tree (AST) node corresponding to a derivation with this rule. This XML serialization meta-annotation comes between square brackets ('[' and ']').
The most basic form of XML annotation is of the form:
[ XmlTag n_1 ... n_k ]where
XmlTag is an identifier to use as XML tag for this
node in the XML serialization of the AST, and the n_k's
are a sequence of numbers denoting positions of symbols in the RHS of
the rule (as for pseudo-variables but without '$'). The
number sequence indicates the order in which subnodes are to be
serialized.
For example, the annotated rule:
QUANTIF
: 'Exists' Var_plus '(' CONDIT ')'
[ Exists 2 4 ]
;
means that an AST node for this rule will be serialized thus:
<Exists> (Xml serialization of Var_plus) (Xml serialization of CONDIT) </Exists>Rules without XML serialization annotation follow a default behavior: the serialization is the concatenation of those of its RHS's constituents, eliminating punctuation tokens (i.e., empty nodes and literal tokens - namely, tokens that do not carry a value).
Using the above basic form and trusting the default bevavior for generating well-bracketted tag pairs is sufficient for simple serialization. However, whenever attributes are needed, or when the serialized AST form is not strictly homomorphic to the concrete syntax tree's, one needs more complex forms of annotation.