// FILE. . . . . /home/hak/ilt/src/ilog/language/syntax/xml/Comment // EDIT BY . . . Hassan Ait-Kaci // ON MACHINE. . 4j4zn71 // STARTED ON. . Sat Apr 07 15:31:10 2007 // Last modified on Sat Apr 07 15:31:22 2007 by hak /** * This grammar specifies the form of XML annotations one can use in a * Jacc grammar. These annotations are used for generating an XML * serialization of the AST. The grammar of XmlAnnotations is herein * expressed as a Jacc grammar itself. Hence, this Jacc grammar * generates the parser used by Jacc itself (!) to enable itself to * (meta-)parse complicated XML annotations that necessitate parsing * power beyond simple Regular Expression matching. * *
* * This comment 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. * *
* * In Jacc, XML annotation patterns of more elaborate form than the * basic form are possible and given by the following grammar: * *
* XmlAnnotation
* : Head ChildrenPositions_opt
* ;
* Head
* : URI Attributes_opt
* ;
* ChildPosition
* : Value
* | XmlAnnotation
* ;
* Attributes
* : '(' Attribute_plus ')'
* ;
* Attribute
* : URI '=' Value
* ;
* Value
* : Path
* | Path '.' IDE
* ;
* Path
* : INT
* | Path '.' INT
* ;
*
*
* Here is an example of use to illustrate:
*
* * * Given the annotated grammar: *
* Exp * : INT [ Int(value = 1."nvalue()") ] * | NAME [ Var(name = 1."svalue()") ] * | BinExp * ; * BinExp * : Exp BinOp Exp [ BinExp(operation = 2."function") 1 3 ] * | Exp PartialExp [ BinExp(operation = 2."relation") 1 2.2 ] * // or [ BinExp 2.1 1 2.2 ] * ; * PartialExp * : RelOp Exp [ PartialExp(op = 1."relation") 2 ] * ; * BinOp * : '+' [ BinOp(function = "Plus") ] * | '\*' [ BinOp(function = "Multiply") ] * ; * RelOp * : '<' [ RelOp(relation = "LessThan") ] * | '>' [ RelOp(relation = "GreaterThan") ] * ; ** * The serialization of the expression "x+1 < y\*3" is: * *
* <BinExp operation="LessThan"> * <BinExp operation="Plus"> * <Var name="x"/> * <Int value="1"/> * </BinExp> * <BinExp operation="Multiply"> * <Var name="y"/> * <Int value="3"/> * </BinExp> * </BinExp> ** * The semantics of an annotation is as follows. * *
Annotation of the form: *
* XmlAnnotation * : '[' Head ChildrenPositions_opt ']' * ; ** means that Head specifies the XML tag and its * attributes, and that its elements will be specified by * ChildrenPositions_opt. * *
Annotation of the form: *
* Head * : URI Attributes_opt * ; ** means that URI is the XML tag, possibly with * attributes, specified by Attributes_opt. * *
Annotation of the form: *
* ChildPosition * : Value * | XmlAnnotation * ; ** means that a ChildPosition is specified either as a * Value denoting the position in the annotated rule's RHS * of whose XML element is used in the specified XML pattern; or a * XmlAnnotation specifying an XML serialization to splice in * lieu of this ChildPosition. * *
Annotation of the form: *
* Attributes
* : '(' Attribute_plus ')'
* ;
*
* specifies a sequence of attributes of an XML tag.
*
* Annotation of the form: *
* Attribute * : URI '=' Value * ; ** specifies an attribute/value pair. * *
Annotation of the form: *
* Value * : Path * | Path '.' IDE * ; * Path * : INT * | Path '.' INT * ; ** means that a Value is either a path of period separated * natural numbers, possibly terminated by a string. Each natural * number (i.e., an INT in a Path, stands for * the index of a constituent in the RHS. *----------------- FINISH LATER ... * n denotes the serialization of the * n-th RHS element when in context of a rule, and that of the * n-th element in the XML serialization, when applied to one. *
* $XmlAnnotation$ * : $Head$ $ChildrenPositions_opt$ * ; ** means that $Head$ specifies the XML tag and its * attributes, and that its elements will be specified by * $ChildrenPositions_opt$. */ /** * Annotation of the form: *
* $Head$ * : $URI$ $Attributes_opt$ * ; ** means that $URI$ is the XML tag, possibly with * attributes, specified by $Attributes_opt$. * */ /** * Annotation of the form: *
* $ChildPosition$ * : $Value$ * | $XmlAnnotation$ * ; ** means that a $ChildPosition$ is specified either as a * $Value$ denoting the position in the annotated rule's RHS * of whose XML element is used in the specified XML pattern; or a * $XmlAnnotation$ specifying an XML serialization to splice in * lieu of this $ChildPosition$. * */ /** * Annotation of the form: *
* $Attributes$
* : '(' $Attribute_plus$ ')'
* ;
*
* specifies a sequence of attributes of an XML tag.
*
*/
/**
* Annotation of the form:
* * $Attribute$ * : $URI$ '=' $Value$ * ; ** specifies an attribute/value pair. * */ /** * Annotation of the form: *
* $Value$ * : $BasicValue$ * | $Value$ '.' $BasicValue$ * ; ** means that a $Value$ is either a basic value such as a natural * number, a string, or an identifier with, or without, arguments; or, the * projection of a value on an attribute denoted by a basic value. * */ /** * Annotation of the form: *
* $BasicValue$ * : $INT$ * | $IDE$ * | $IDE$ $Arguments_opt$ * ; ** is interpreted such that: *