// FILE. . . . . /cygdrive/c/cygwin/home/hak/ilt/src/ilog/rif/HRL.grm
// EDIT BY . . . Hassan Ait-Kaci
// ON MACHINE. . 4j4zn71.Ilog.Biz
// STARTED ON. . Tue Sep 19 10:29:49 2006

// Last modified on Mon Mar 31 10:27:52 2008 by hak

/**
 * This is a Jacc grammar for a <a
 * href="http://lists.w3.org/Archives/Public/public-rif-wg/2006Sep/0035.html">Horn
 * Rule Language (HRL)</a> based on the RIF Condition Language (RCL)
 * defined as a combination of partial grammars.
 * 
 * <p>
 * 
 * It has evolved out of the first-cut grammar for a minimal
 * "human-readable" syntax, which is described in the proposal
 * submitted to the W3C Working Group on the Rule Interchange Format
 * (RIF) by Harold Boley <i>et al.</i>, on April 23, 2006. It is meant
 * to be able to express the abstract syntax form taken by
 * <i>conditions</i> of the rules that appear to be common to a
 * reasonably large variety of families of rule languages. The proposal
 * in question is available from the public RIF WG mail archive: <a
 * href="http://lists.w3.org/Archives/Public/public-rif-wg/2006Apr/0068.html">
 * [RIF] Extensible Design</a>.
 * 
 * <p>
 * 
 * This Jacc grammar specification is a literal transcription of the BNF
 * rules given in the above references.
 *
 * <p>
 *    
 * This version of the HRL (and RCL) grammar(s) is annotated for simple
 * XML serialization using namespace labells identifying the language
 * level at which the constructs belong.
 *
 * <p>
 *
 * We give below the Jacc grammar rules for a "full" HRL using the a
 * version "full" RCL. We use Jacc's <tt>%include</tt> commands to make
 * the grammars modular. Note that the full HRL grammar that is the
 * combination of all the partial included grammars defines a LALR(1)
 * grammar. Thus, so are simpler grammars using a less full version of
 * the RCL (or HRL) - <i>i.e.</i>, by including less rules.
 *
 * <p>
 *
 * This HTML file is the root of a hyperlinked documentation allowing
 * one to explore the grammar via navigation through its elements -
 * rules, terminal and non-terminal symbols. The comments accompanying
 * some rules come from the original documents.  It also contains the
 * pure Yacc rules.  This documentation is generated by Jacc from the
 * Jacc grammar specified in file <a
 * href="HRL.html"><tt>HRL.grm</tt></a> (<i>i.e.</i>, with the command
 * "<tt>jacc -doc HRL</tt>"). Along with this Jacc grammar file, there are
 * other <a href="index.html">supporting source files</a>.
 *
 * <p>
 *
 * Essentially, the rule format 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 <tt>&#36;1</tt>, <tt>&#36;2</tt>,
 * ..., <tt>&#36;n</tt> - the so-called pseudo-variables where the index
 * <tt>n</tt> in <tt>&#36;n</tt> refers to the order of RHS
 * constituents. Such actions appear between curly braces ('<tt>{</tt>' and
 * '<tt>}</tt>') 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
 * ('<tt>[</tt>' and '<tt>]</tt>') and is of the form:
 *
 * <pre>
 * [ XmlTag n_1 ... n_k ]
 * </pre>
 *
 * where <code>XmlTag</code> is an identifier to use as XML tag for this
 * node in the XML serialization of the AST, and the <code>n_k</code>'s
 * are a sequence of numbers denoting positions of symbols in the RHS of
 * the rule (as for pseudo-variables but without <tt>'&#36;'</tt>). The
 * number sequence indicates the order in which subnodes are to be
 * serialized. For example, the annotated rule:
 *
 * <pre>
 * QUANTIF
 *    : 'Exists' Var_plus '(' CONDIT ')'
 *      [ Exists 2 4 ]
 *    ;
 * </pre>
 *
 * means that an AST node for this rule will be serialized thus:
 *
 * <pre>
 *  &lt;Exists&gt;
 *    (Xml serialization of Var_plus)
 *    (Xml serialization of CONDIT)
 *  &lt;/Exists&gt;
 * </pre>
 *
 * 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>i.e.</i>, empty nodes
 * and literal tokens - namely, tokens that do not carry a value).
 *
 * <p>
 *
 * For example, see the two test files <a
 * href="../../examples/Test.hrl"><tt>Test.hrl</tt></a> and <a
 * href="../../examples/Test2.hrl"><tt>Test2.hrl</tt></a>.  Running the
 * command <a href="../../examples/hrl"><tt>hrl</tt></a> on them produces
 * the outputs shown in <a
 * href="../../examples/Test.xml"><tt>Test.xml</tt></a> and  <a
 * href="../../examples/Test2.xml"><tt>Test2.xml</tt></a>.
 */

////////////////////////////////////////////////////////////////////////

%package ilog.rif;

%include Keywords.grm	// RCL language reserved keywords
%include ParserCode.grm	// Code for setting params and showing xml serialization

%token Name Fun Rel Value Var // NB: Var is a token (i.e., the '?' is part of it)

%xmlns "rif" "http://www.w3.org/2007/rif#"

%start ROOT

////////////////////////////////////////////////////////////////////////

%%  

/**
 * Horn Rule Language (HRL) program grammar's root.
 */
ROOT
  : HRL
  { showXml(); }  // show the XML tree
  ;

/**
 * HRL program units are mae of declarations and rulesets.
 */
HRL
  : RuleSet
  [ rif:RuleSet 1 ]  // XML serialization pattern
  ;

/**
 * Rulesets are sequences of ruleset items.
 */
RuleSet
  : RuleSetItem
  | RuleSet RuleSetItem
  ;

/**
 * A RuleSetItem is a declaration or a rule.
 */
RuleSetItem
  : Declaration ';'
  | Rule
  ;

/**
 * Declarations are needed to discriminate relation and function symbols.
 * NB: an identifier can be declared explicitly as either a relation symbol
 * or a function symbol. An undeclared identifier will cause an error.
 */
Declaration
  : 'Relation' Name
    { Lexicon.relation($2.svalue()); } // make this a relation's name
    [ rif:Relation 2 ]  // XML serialization pattern
  | 'Function' Name
    { Lexicon.function($2.svalue()); } // make this a function's name
    [ rif:Function 2 ]  // XML serialization pattern
  ;

/**
 * A Horn rule is a kind of rule. 
 */
Rule
  : HornRule
  [ rif:HornRule 1 ]  // XML serialization pattern
  ;

/**
 * A Horn rule is a logical implication.
 */
HornRule
  : Implication
  ;

/**
 * An implication consists of a $HEAD$ (the LHS - what's on the left of
 * $:-$) and a $BODY$ (the RHS - what's on the right of $:-$).
 */
Implication
  : HEAD ':-' BODY
  [ hrl:Implies 3 1 ]  // XML serialization pattern
  ;

/**
 * A Horn rule's head is a literal formula ($LITFORM$).
 */
HEAD
  : LITFORM
  [ hrl:Consequent 1 ]  // XML serialization pattern
  ;

/**
 * A Horn rule's body is a condition ($CONDIT$).
 */
BODY
  : CONDIT
  [ hrl:Antecedent 1 ]  // XML serialization pattern
  ;

////////////////////////////////////////////////////////////////////////
// Includes:
////////////////////////////////////////////////////////////////////////

// RIF Condition Language basis:
%include RCL_basis.grm

// RCL's dual (value,object) constants:
%include RCL_valobj.grm

// RCL's universal quantification:
%include RCL_forall.grm

// RCL's constructive negation:
%include RCL_neg.grm

// RCL's negation-as-failure:
%include RCL_naf.grm

// RCL's both negation-as-failure and constructive negation:
%include RCL_nafneg.grm

%%

////////////////////////////////////////////////////////////////////////
