XmlGenEx.html

`XmlGenEx.grm`

    //  FILE. . . . . /home/hak/hlt/src/hlt/language/jaccapps/xml/sources/XmlGenEx.grm
    //  EDIT BY . . . Hassan Ait-Kaci
    //  ON MACHINE. . Hp-Dv7
    //  STARTED ON. . Fri Oct 19 23:11:54 2012

This is a small grammar that we use as an example to illustrate JACC's XML serialization annotation mechanism. See also the annotated JACC grammar's hilited source code (i.e., an HTML-beautified rendering of the JACC source files where this comment came from. The file `test.exp` contains a set of expressions that can be parsed and serialized. The resulting XML serialization of the example expression in that file can be seen in the file `test.trace`. The "pure" XML serialization of the expressions in that file can be seen in the file `test.xml`.
Author: |  | Hassan Aït-Kaci
Copyright: |  | © by the author
Version: |  | Last modified on Tue Jan 29 07:24:09 2013 by hak

    /* ************************************************************************ */
    /* ************************ PARAMETER DECLARATIONS ************************ */
    /* ************************************************************************ */

This declares the Java package for the parser generated from this grammar to be `hlt.xml`.

    %package hlt.xml;

Include the ancillary java code contained in file ParserCode.grm for setting the generated parser's parameters and showing XML serialization.

    %include ParserCode.grm

Declare the grammar start symbol to be `$EXP$`.

    %start EXP

This declares this application XML's namespace to be `http://www.hassan-ait-kaci.net/hex`. This is just a fake URI, of course, for the sake of this example ("`hex`" stands for "`hlt example xml`").

    %xmlns "hex" "http://www.hassan-ait-kaci.net/hex"

Define the XML tree root element to be a `hex:expressions`.

    %xmlroot "hex" "expressions"

Declare non-punctuation terminals `$NUMBER$`, `$IDENT$`, and `$VAR$`. NB: `$VAR$` is an identifier starting with a `'?'` (i.e., the `'?'` is part of it, not a separate token).

    %token NUMBER IDENT STRING VAR

Declare operators from lowest to highest precedence.

    %left  OR
    %left  AND
    %token SMALLER EQUAL GREATER
    %left  PLUS
    %left  TIMES

    /* ************************************************************************ */
    /* ***** ANNOTATION SPECIFICATION FOR THE GRAMMAR'S TERMINAL SYMBOLS  ***** */
    /* ************************************************************************ */

Annotate terminal symbol `$VAR$` for XML serialization.

    %xmlinfo VAR [ NS:"hex" LO:"variable" AT:{name = $value} ]

Annotate terminal symbol `$IDENT$` for XML serialization.

    %xmlinfo IDENT [ NS:"hex" LO:"identifier" AT:{name = $value} ]

Annotate terminal symbol `$STRING$` for XML serialization.

    %xmlinfo STRING [ NS:"hex" LO:"string" AT:{value = $value} ]

Annotate terminal symbol `$NUMBER$` for XML serialization.

    %xmlinfo NUMBER [ NS:"hex" LO:"number" AT:{value = $value} ]

Annotate terminal symbol `$PLUS$` for XML serialization.

    %xmlinfo PLUS [ NS:"hex" LO:"ArithmeticOperator" AT:{symbol = "+"} ]

Annotate terminal symbol `$TIMES$` for XML serialization.

    %xmlinfo TIMES [ NS:"hex" LO:"ArithmeticOperator" AT:{symbol = "*"} ]

Annotate terminal symbol `$SMALLER$` for XML serialization.

    %xmlinfo SMALLER [ NS:"hex" LO:"RelationalOperator" AT:{symbol = "<"} ]

Annotate terminal symbol `$GREATER$` for XML serialization.

    %xmlinfo GREATER [ NS:"hex" LO:"RelationalOperator" AT:{symbol = ">"} ]

Annotate terminal symbol `$EQUAL$` for XML serialization.

    %xmlinfo EQUAL [ NS:"hex" LO:"RelationalOperator" AT:{symbol = "="} ]

Annotate terminal symbol `$AND$` for XML serialization.

    %xmlinfo AND [ NS:"hex" LO:"BooleanOperator" AT:{symbol = "&"} ]

Annotate terminal symbol `$OR$` for XML serialization.

    %xmlinfo OR [ NS:"hex" LO:"BooleanOperator" AT:{symbol = "|"} ]

    %%

    /* ************************************************************************ */
    /* ******* ANNOTATED GRAMMAR RULES FOR A SIMPLE EXPRESSION LANGUAGE ******* */
    /* ************************************************************************ */

This grammar's root is the nonterminal symbol `EXP`.

    EXP
      : Exprs
      {
        // show the XML tree
        showXml();
        // show the CST:
    //     System.out.println("Concrete syntax tree:\n");
    //     currentNode().show(2,System.out);
      }
      ;

An `Exprs` is a possibly empty sequence of items, each consisting of an `$Expr$`'s followed by a `'$;$'`.

    Exprs
      : /* empty */
      | Exprs Expr ';'
     ;

An `Expr` is either an `$Atom$` or a `$BinExpr$`.

    Expr
      : Atom
      | BinExpr
      ;

An `Atom` is either a `$NUMBER$`, an `$IDENT$`, a `$STRING$`, or a `$VAR$`.

    Atom
      : NUMBER
      | IDENT
      | STRING
      | VAR
      ;

A `BinExpr` either one of: an `$Expr$` followed by a binary operator (i.e., one of `$PLUS$`, `$TIMES$`, `$AND$`, or `$OR$`), followed by an `$Expr$`; an `$Expr$` followed by a `$PartialRelationalExpr$`.

    BinExpr
      : Expr PLUS Expr

A `BinExpr` is an `$Expr$` followed by the binary operator `$PLUS$` followed by an `$Expr$`.

      [
        NS:"hex"
        LO:"BinaryArithmeticExpression"
        AT:{op = 2/symbol}
        CH:(1 3)
      ]
      |

A `BinExpr` is an `$Expr$` followed by the binary operator `$TIMES$` followed by an `$Expr$`.

        Expr TIMES Expr
      [
        NS:"hex"
        LO:"BinaryArithmeticExpression"
        AT:{op = 2/symbol}
        CH:(1 3)
      ]
      |

A `BinExpr` is an `$Expr$` followed by the binary operator `$AND$` followed by an `$Expr$`.

        Expr AND Expr
      [
        NS:"hex"
        LO:"BinaryBooleanExpression"
        AT:{op = 2/symbol}
        CH:(1 3)
      ]
      |

A `BinExpr` is an `$Expr$` followed by the binary operator `$OR$` followed by an `$Expr$`.

        Expr OR Expr
      [
        NS:"hex"
        LO:"BinaryBooleanExpression"
        AT:{op = 2/symbol}
        CH:(1 3)
      ]
      | Expr PartialRelationalExpr

A `BinExpr` is an `$Expr$` followed by a `$PartialRelationalExpr$`.

      [
        NS:"hex"
        LO:"BinaryRelationalExpression"
        AT:{op = 2/op}
        CH:(1 2[1])
      ]
      // Possible alternative (prefix) - if also modifying the annotation for
      // PartialRelationalExpr accordingly:
      //   [
      //     NS:"hex"
      //     LO:"BinaryExpression"
      //     CH:(2[1] 1 2[2])
      //   ]
      ;

A `PartialRelationalExpr` consists of a relational operator (i.e., one of `$SMALLER$`, `$EQUAL$`, or `$GREATER$`) followed by an `$Expr$`.

    PartialRelationalExpr
    //   : RelOp Expr
      : SMALLER Expr
      [
        NS:"hex"
        LO:"PartialRelationalExpression"
        AT:{op = 1/symbol}
        CH:(2)
      ]
      | EQUAL Expr
      [
        NS:"hex"
        LO:"PartialRelationalExpression"
        AT:{op = 1/symbol}
        CH:(2)
      ]
      | GREATER Expr
      [
        NS:"hex"
        LO:"PartialRelationalExpression"
        AT:{op = 1/symbol}
        CH:(2)
      ]
      // Possible alternative (prefix) - if also modifying the annotation for
      // BinExpr accordingly:
      //   [
      //     NS:"hex"
      //     LO:"PartialRelationalExpression"
      //     CH:(1 2)
      //   ]
      ;

      // /**
      //  * A <tt>BinOp</tt> is one of <tt>$PLUS$</tt>, <tt>$TIMES$</tt>,
      //  * <tt>$AND$</tt>, or <tt>$OR$</tt>.
      //  */
      // BinOp
      //   : PLUS
      //   | TIMES
      //   | AND
      //   | OR
      //   ;

      // /**
      //  * A <tt>RelOp</tt> is one of <tt>$SMALLER$</tt>, <tt>$EQUAL$</tt>,
      //  * or <tt>$GREATER$</tt>.
      //  */
      // RelOp
      //   : SMALLER
      //   | EQUAL
      //   | GREATER
      //   ;

    %%

    /* ************************************************************************ */
    /* ********************* END OF GRAMMAR XmlGenEx.grm ********************** */
    /* ************************************************************************ */

This file was generated on Tue Jan 29 08:35:19 CET 2013 from file `XmlGenEx.grm`
by the `hlt.language.tools.Hilite` Java tool written by Hassan Aït-Kaci