HRL.grm

// 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 Thu Nov 02 04:25:24 2006 by hak



This is a Jacc grammar for a Horn Rule Language (HRL) based on the RIF Condition Language (RCL) defined as a combination of partial grammars.

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 et al., on April 23, 2006. It is meant to be able to express the abstract syntax form taken by conditions 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: [RIF] Extensible Design.

This Jacc grammar specification is a literal transcription of the BNF rules given in the above references.

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.

We give below the Jacc grammar rules for a "full" HRL using the a version "full" RCL. We use Jacc's %include 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.e., by including less rules.

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 HRL.grm (i.e., with the command "jacc -doc HRL"). Along with this Jacc grammar file, there are other supporting source files.

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 $1, $2, ..., $n - the so-called pseudo-variables where the index n in $n refers to the order of RHS constituents. 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 ']') and 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).

For example, see the two test files Test.hrl and Test2.hrl. Running the command hrl on them produces the outputs shown in Test.xml and Test2.xml.



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

%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)

%start ROOT

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

%%  



Horn Rule Language ($HRL$) program grammar's root.


ROOT
  : HRL
  { showXml(); }  // show the XML tree
  ;



An $HRL$ program consists of a $RuleSet$.


HRL
  : RuleSet
  [ rif:RuleSet 1 ]  // XML serialization pattern
  ;



A $RuleSet$ is a sequence of one or more $RuleSetItem$s.


RuleSet
  : RuleSetItem
  | RuleSet RuleSetItem
  ;



A $RuleSetItem$ is a $Declaration$ or a $Rule$.


RuleSetItem
  : Declaration ';'
  | Rule
  ;



$Declaration$s 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 $Rule$ is a $HornRule$.


Rule
  : HornRule
  [ rif:HornRule 1 ]  // XML serialization pattern
  ;



A $HornRule$ is an $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

%%

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


This file was generated on Mon Mar 31 17:18:17 PDT 2008 from file HRL.grm
by the ilog.language.tools.Hilite Java tool written by Hassan Aït-Kaci