Package com.ohrasys.cad.bnf

Provides a general facility to verify a stream of tokens using commonly used Backus Naur tests.

See:
          Description

Interface Summary
BNFTestImplementor An interface that all BNF test implementations must implement.
 

Class Summary
BNFAbstractTest Abstract Backus-Naur test.
BNFNoFallthruTest Represents a terminal test
BNFOneOfManyRequiredTest A test in which one of the sub-tests must pass.
BNFOneOrMoreOptionalTest A test in which all of the sub-tests must occur zero or more times
BNFOneOrMoreRequiredTest A test in which all of the sub-tests must occur one or more times.
BNFOptionalTest A test in which all sub-tests must occur zero or one time
BNFRequiredTest A test in which all sub-tests must complete once
BNFTestableObject An abstract wrapper class used to wrap objects such that a token may be easily retrieved
BNFTestResult A class used to represent the completion status of a test.
 

Exception Summary
BNFTestException An exception class for the BNF test package
 

Package com.ohrasys.cad.bnf Description

Provides a general facility to verify a stream of tokens using commonly used Backus Naur tests.

The table below is a summary of Backus Naur formalism syntax.

      Symbol       Meaning
      <element> :: "Element is composed of"
       ELEMENT     Required element
      [ELEMENT]    The element may be absent or occur once.
      {ELEMENT}    One of the elements in the braces must occur.
      {ELEMENT}*   All elements in the braces may be absent or occur one or more
                   times.
      {ELEMENT}+   All elements in the braces must occur one or more
                   times.
      <element>    A composite element
      |            "Or"
    

The following table illustrates how specific classes map to Backus Naur symbols.

      Symbol       Maps To
      <element> :: BNFRequiredTest
       ELEMENT     BNFNoFallthruTest
      [ELEMENT]    BNFOptionalTest
      {ELEMENT}    BNFOneOfManyRequiredTest
      {ELEMENT}*   BNFOneOrMoreOptionalTest
      {ELEMENT}+   BNFOneOrMoreRequiredTest
      |            No mapping required
    

For example the following Backus Naur statements:

      <db>       :: HEADER [UNITS] {<gds>|<cif>} {<property>}+ {<record>}* ENDDB
      <gds>      :: GDSTYPE [VERSION]
      <cif>      :: CIFTYPE [VERSION]
      <property> :: PROPTYPE PROPVALUE
      <record>   :: RECTYPE [<body>] ENDREC
      <body>     :: VALUE [TRANSFORM]
    

Would map to the following java code. The for statement at the end uses the dbT test instance to test if a stream of tokens meets the syntax requirements defined by the test. The construction and initialization of the token array occurs outside the scope of this snippet.

      //...
      int TRANSFORM = 0x00;
      int VALUE     = 0x01;
      int ENDREC    = 0x02;
      int RECTYPE   = 0x03;
      int RECVALUE  = 0x04;
      int PROPTYPE  = 0x05;
      int PROPVALUE = 0x06;
      int CIFTYPE   = 0x07;
      int GDSTYPE   = 0x08;
      int VERSION   = 0x09;
      int HEADER    = 0x0A;
      int UNITS     = 0x0B;
      int ENDDB     = 0x0C;

      BNFTestImplementor   xfrmT         = new BNFOptionalTest(new BNFTestImplementor[]{BNFNoFallthruTest(TRANSFORM)});
      BNFTestImplementor   valueT        = new BNFNoFallthruTest(VALUE);
      BNFTestImplementor[] bodyTests     = {valueT, xfrmT};

      BNFTestImplementor   bodyT         = new BNFOptionalTest(bodyTests);
      BNFTestImplementor   rectypeT      = new BNFNoFallthruTest(RECTYPE);
      BNFTestImplementor   endrecT       = new BNFNoFallthruTest(ENDREC);
      BNFTestImplementor[] recordTests   = {rectypeT, bodyT, endrecT};

      BNFTestImplementor   recordT       = new BNFRequiredTest(recordTests);
      BNFTestImplementor   proptypeT     = new BNFNoFallthruTest(PROPTYPE);
      BNFTestImplementor   propvalueT    = new BNFNoFallthruTest(PROPVALUE);
      BNFTestImplementor[] propertyTests = {proptypeT propvalueT};

      BNFTestImplementor   propertyT     = new BNFRequiredTest(propertyTests);
      BNFTestImplementor   gdstypeT      = new BNFNoFallthruTest(GDSTYPE);
      BNFTestImplementor   ciftypeT      = new BNFNoFallthruTest(CIFTYPE);
      BNFTestImplementor   versionT      = new BNFOptionalTest(new BNFTestImplementor[]{BNFNoFallthruTest(VERSION)});
      BNFTestImplementor[] gdsTests      = {gdstypeT versionT};
      BNFTestImplementor[] cifTests      = {ciftypeT versionT};

      BNFTestImplementor   gdsT          = new BNFRequiredTest(gdsTests);
      BNFTestImplementor   cifT          = new BNFRequiredTest(cifTests);
      BNFTestImplementor   recordG       = new BNFOneOrMoreOptionalTest(
        new BNFTestImplementor[] { recordT });

      BNFTestImplementor   propertyG     = new BNFOneOrMoreRequiredTest(
        new BNFTestImplementor[] { propertyT });

      BNFTestImplementor   typeG         = new BNFOneOfManyRequiredTest(
        new BNFTestImplementor[] { gdsT, cifT });

      BNFTestImplementor   enddbT        = new BNFNoFallthruTest(ENDDB);
      BNFTestImplementor   unitsT        = new BNFOptionalTest(new BNFTestImplementor[]{BNFNoFallthruTest(UNITS)});
      BNFTestImplementor   headerT       = new BNFNoFallthruTest(HEADER);
      BNFTestImplementor[] dbTests       = {
        headerT, unitsT, typeG, propertyG, recordG, enddbT };

      BNFTestImplementor   dbT = new BNFRequiredTest(dbTests);

      for(int i = 0; i < tokens.length; i++){
        if(dbT.test(tokens[i]).isFailed())
          return false
      }
      return true;
      //...