FOTParser.java

// *******************************************************************
// This file has been automatically generated from the grammar in file
// FOT.grm by hlt.language.syntax.ParserGenerator on
// Sun Jul 15 08:21:56 CEST 2018 --- !!! PLEASE DO NO EDIT !!!
// *******************************************************************

import java.io.Reader;
import java.io.StringReader;
import java.io.IOException;
import hlt.language.syntax.*;
import java.util.*;
import hlt.math.fuzzy.fot.*;


/* ************ */
/* PARSER CLASS */
/* ************ */

class FOTParser extends StaticParser
{
  /* ************************ */
  /* PARSER CLASS CONSTRUCTOR */
  /* ************************ */

  public FOTParser (Tokenizer t)
    {
      input = t;
      xmlroot = "Session";
    }

  /* ************************* */
  /* PARSER CLASS DECLARATIONS */
  /* ************************* */

  Stack terms = new Stack();
  Term term, temp;
  Signature signature = new Signature();
  FirstOrderTerm fot;

  public void executePragma (String pragma)
  {
    switch (pragma)
      {
      case "signature":
	out.println(signature);
	break;
      default:
	err.println("??? Unknown pragma: "+pragma);
      }
  }



  /* ********************** */
  /* STATIC INITIALIZATIONS */
  /* ********************** */

  static
    {
      initializeTerminals();
      initializeNonTerminals();
      initializeRules();
      initializeParserActions();
      initializeParserStates();
      initializeActionTables();
      initializeGotoTables();
      initializeStateTables();
    }

  /* ********************* */
  /* PARTIAL PARSE METHODS */
  /* ********************* */

  final static ParseNode $SESSION_SWITCH$ = new ParseNode(terminals[3]);

  public final void parseSession (String s) throws IOException
    {
      parseSession(new StringReader(s));
    }

  public final void parseSession (Reader r) throws IOException
    {
      input.setReader(r);
      errorManager().recoverFromErrors(false);
      setSwitchToken($SESSION_SWITCH$);
      parse();
    }

  /* **************** */
  /* SEMANTIC ACTIONS */
  /* **************** */

  protected ParseNode semanticAction(ParserRule $rule$) throws IOException
    {
      ParseNode $head$ = new ParseNode($rule$.head);

      switch($rule$.index())
        {
          case 2:
            {
            $head$ = $head$.copy(node($rule$,2));
            break;
            }
          case 5:
            {
            out.println("*** Bye bye!...");
  	    System.exit(0);
            break;
            }
          case 9:
            {
            executePragma(node($rule$,0).svalue());
            FOTTokenizer.prompt();
            break;
            }
          case 11:
            {
            terms = new Stack();
            break;
            }
          case 12:
            {
            term = (Term)terms.pop();
  	    // print out the parsed term's lexical form
  	    out.println("lexical >>> "+term);
  	    try // need to report if a functor appears with inconsistent arities
  	      { // converting the parsed Term into its FirstOrderTerm canonical form
  	        fot = term.canonical(signature);
  	        // if all ok, print out the term's canonical form
  	        out.println("canonical >>> "+fot);
  	      }
  	    catch (Exception e)
  	      {
  	        System.err.println(e.getMessage());
  	      }
            FOTTokenizer.prompt();
            break;
            }
          case 14:
            {
            errorManager().reportErrors(true);
            FOTTokenizer.prompt();
            break;
            }
          case 16:
            {
            terms.push(new Term(node($rule$,1).svalue()).markAsVariable());
            break;
            }
          case 19:
            {
            terms.push(new Term(node($rule$,1).svalue()));
            break;
            }
          case 20:
            {
            terms.push(new Term(node($rule$,-1).svalue(),new Vector()));
            break;
            }
          case 22:
            {
            term = (Term)terms.pop();
            ((Term)terms.peek()).body.add(term);
            break;
            }
          case 23:
            {
            term = (Term)terms.pop();
            ((Term)terms.peek()).body.add(term);
            break;
            }
          case 0: case 1: 
            break;
          default:
            $head$ = $head$.copy(node($rule$,1));
            break;
        }
      return $head$;
    }

  /* **************** */
  /* TERMINAL SYMBOLS */
  /* **************** */

  static void initializeTerminals ()
    {
      terminals = new ParserTerminal[12];

      newTerminal(0,"$EMPTY$",1,2);
      newTerminal(1,"$E_O_I$",1,2);
      newTerminal(2,"error",1,2);
      newTerminal(3,"$Session_switch$",1,2);
      newTerminal(4,"PRAGMA",1,2);
      newTerminal(5,"FUNCTOR",1,2);
      newTerminal(6,"VARIABLE",1,2);
      newTerminal(7,"exit",1,2);
      newTerminal(8,".",1,2);
      newTerminal(9,"(",1,2);
      newTerminal(10,")",1,2);
      newTerminal(11,",",1,2);
    }

  /* ******************** */
  /* NON-TERMINAL SYMBOLS */
  /* ******************** */

  static void initializeNonTerminals ()
    {
      nonterminals = new ParserNonTerminal[15];

      newNonTerminal(0,"$START$");
      newNonTerminal(1,"$ROOTS$");
      newNonTerminal(2,"Session");
      newNonTerminal(3,"Clauses");
      newNonTerminal(4,"Exit");
      newNonTerminal(5,"$ACTION0$");
      newNonTerminal(6,"Clause");
      newNonTerminal(7,"$ACTION1$");
      newNonTerminal(8,"Term");
      newNonTerminal(9,"$ACTION2$");
      newNonTerminal(10,"$ACTION3$");
      newNonTerminal(11,"$ACTION4$");
      newNonTerminal(12,"Structure");
      newNonTerminal(13,"Body");
      newNonTerminal(14,"$ACTION5$");
    }

  /* **************** */
  /* PRODUCTION RULES */
  /* **************** */

  static void initializeRules ()
    {
      rules = new ParserRule[24];

      rules[0] = new ParserRule(0,1,0,1,2);
      rules[1] = new ParserRule(1,1,1,1,2);
      rules[2] = new ParserRule(1,2,2,1,2);
      rules[3] = new ParserRule(2,2,3,1,2);
      rules[4] = new ParserRule(2,1,4,1,2);
      rules[5] = new ParserRule(5,0,5,1,2);
      rules[6] = new ParserRule(4,3,6,1,2);
      rules[7] = new ParserRule(3,1,7,1,2);
      rules[8] = new ParserRule(3,2,8,1,2);
      rules[9] = new ParserRule(7,0,9,1,2);
      rules[10] = new ParserRule(6,3,10,1,2);
      rules[11] = new ParserRule(9,0,11,1,2);
      rules[12] = new ParserRule(10,0,12,1,2);
      rules[13] = new ParserRule(6,4,13,1,2);
      rules[14] = new ParserRule(11,0,14,1,2);
      rules[15] = new ParserRule(6,3,15,1,2);
      rules[16] = new ParserRule(8,1,16,1,2);
      rules[17] = new ParserRule(8,1,17,1,2);
      rules[18] = new ParserRule(8,3,18,1,2);
      rules[19] = new ParserRule(12,1,19,1,2);
      rules[20] = new ParserRule(14,0,20,1,2);
      rules[21] = new ParserRule(12,5,21,1,2);
      rules[22] = new ParserRule(13,1,22,1,2);
      rules[23] = new ParserRule(13,3,23,1,2);
    }

  /* ************** */
  /* PARSER ACTIONS */
  /* ************** */

  static void initializeParserActions ()
    {
      actions = new ParserAction[102];

      newAction(0,5,0);
      newAction(1,2,0);
      newAction(2,0,3);
      newAction(3,0,7);
      newAction(4,0,8);
      newAction(5,0,10);
      newAction(6,1,11);
      newAction(7,1,11);
      newAction(8,1,11);
      newAction(9,1,1);
      newAction(10,0,7);
      newAction(11,0,8);
      newAction(12,0,10);
      newAction(13,1,11);
      newAction(14,1,11);
      newAction(15,1,11);
      newAction(16,0,7);
      newAction(17,0,8);
      newAction(18,0,10);
      newAction(19,1,11);
      newAction(20,1,11);
      newAction(21,1,11);
      newAction(22,1,4);
      newAction(23,1,7);
      newAction(24,1,7);
      newAction(25,1,7);
      newAction(26,1,7);
      newAction(27,1,7);
      newAction(28,1,7);
      newAction(29,1,5);
      newAction(30,1,9);
      newAction(31,0,14);
      newAction(32,0,16);
      newAction(33,0,17);
      newAction(34,1,14);
      newAction(35,0,12);
      newAction(36,1,15);
      newAction(37,1,15);
      newAction(38,1,15);
      newAction(39,1,15);
      newAction(40,1,15);
      newAction(41,1,15);
      newAction(42,1,12);
      newAction(43,1,16);
      newAction(44,1,16);
      newAction(45,1,16);
      newAction(46,1,17);
      newAction(47,1,17);
      newAction(48,1,17);
      newAction(49,0,14);
      newAction(50,0,16);
      newAction(51,0,17);
      newAction(52,1,19);
      newAction(53,1,19);
      newAction(54,1,19);
      newAction(55,0,18);
      newAction(56,1,20);
      newAction(57,1,20);
      newAction(58,1,20);
      newAction(59,0,14);
      newAction(60,0,16);
      newAction(61,0,17);
      newAction(62,0,22);
      newAction(63,0,23);
      newAction(64,1,22);
      newAction(65,1,22);
      newAction(66,1,21);
      newAction(67,1,21);
      newAction(68,1,21);
      newAction(69,0,14);
      newAction(70,0,16);
      newAction(71,0,17);
      newAction(72,1,23);
      newAction(73,1,23);
      newAction(74,0,26);
      newAction(75,1,18);
      newAction(76,1,18);
      newAction(77,1,18);
      newAction(78,0,28);
      newAction(79,1,13);
      newAction(80,1,13);
      newAction(81,1,13);
      newAction(82,1,13);
      newAction(83,1,13);
      newAction(84,1,13);
      newAction(85,0,30);
      newAction(86,1,10);
      newAction(87,1,10);
      newAction(88,1,10);
      newAction(89,1,10);
      newAction(90,1,10);
      newAction(91,1,10);
      newAction(92,0,32);
      newAction(93,1,6);
      newAction(94,1,3);
      newAction(95,1,8);
      newAction(96,1,8);
      newAction(97,1,8);
      newAction(98,1,8);
      newAction(99,1,8);
      newAction(100,1,8);
      newAction(101,1,2);
    }

  /* ************* */
  /* PARSER STATES */
  /* ************* */

  static void initializeParserStates ()
    {
      states = new ParserState[36];

      for (int i=0; i<36; i++) newState(i);
    }

  /* ************* */
  /* ACTION TABLES */
  /* ************* */

  static void initializeActionTables ()
    {
      newActionTables(32);

      newActionTable(0,7);
	setAction(0,2,5);
	setAction(0,3,2);
	setAction(0,4,4);
	setAction(0,5,6);
	setAction(0,6,7);
	setAction(0,7,3);
	setAction(0,9,8);

      newActionTable(1,1);
	setAction(1,1,1);

      newActionTable(2,1);
	setAction(2,1,9);

      newActionTable(3,6);
	setAction(3,2,12);
	setAction(3,4,11);
	setAction(3,5,13);
	setAction(3,6,14);
	setAction(3,7,10);
	setAction(3,9,15);

      newActionTable(4,1);
	setAction(4,1,22);

      newActionTable(5,6);
	setAction(5,2,23);
	setAction(5,4,24);
	setAction(5,5,25);
	setAction(5,6,26);
	setAction(5,7,27);
	setAction(5,9,28);

      newActionTable(6,1);
	setAction(6,8,29);

      newActionTable(7,1);
	setAction(7,8,30);

      newActionTable(8,3);
	setAction(8,5,33);
	setAction(8,6,31);
	setAction(8,9,32);

      newActionTable(9,1);
	setAction(9,8,34);

      newActionTable(10,1);
	setAction(10,8,35);

      newActionTable(11,6);
	setAction(11,2,36);
	setAction(11,4,37);
	setAction(11,5,38);
	setAction(11,6,39);
	setAction(11,7,40);
	setAction(11,9,41);

      newActionTable(12,1);
	setAction(12,8,42);

      newActionTable(13,3);
	setAction(13,8,43);
	setAction(13,10,44);
	setAction(13,11,45);

      newActionTable(14,3);
	setAction(14,8,46);
	setAction(14,10,47);
	setAction(14,11,48);

      newActionTable(15,4);
	setAction(15,8,52);
	setAction(15,9,55);
	setAction(15,10,53);
	setAction(15,11,54);

      newActionTable(16,3);
	setAction(16,5,56);
	setAction(16,6,57);
	setAction(16,9,58);

      newActionTable(17,2);
	setAction(17,10,62);
	setAction(17,11,63);

      newActionTable(18,2);
	setAction(18,10,64);
	setAction(18,11,65);

      newActionTable(19,3);
	setAction(19,8,66);
	setAction(19,10,67);
	setAction(19,11,68);

      newActionTable(20,2);
	setAction(20,10,72);
	setAction(20,11,73);

      newActionTable(21,1);
	setAction(21,10,74);

      newActionTable(22,3);
	setAction(22,8,75);
	setAction(22,10,76);
	setAction(22,11,77);

      newActionTable(23,1);
	setAction(23,8,78);

      newActionTable(24,6);
	setAction(24,2,79);
	setAction(24,4,80);
	setAction(24,5,81);
	setAction(24,6,82);
	setAction(24,7,83);
	setAction(24,9,84);

      newActionTable(25,1);
	setAction(25,8,85);

      newActionTable(26,6);
	setAction(26,2,86);
	setAction(26,4,87);
	setAction(26,5,88);
	setAction(26,6,89);
	setAction(26,7,90);
	setAction(26,9,91);

      newActionTable(27,1);
	setAction(27,8,92);

      newActionTable(28,1);
	setAction(28,1,93);

      newActionTable(29,1);
	setAction(29,1,94);

      newActionTable(30,6);
	setAction(30,2,95);
	setAction(30,4,96);
	setAction(30,5,97);
	setAction(30,6,98);
	setAction(30,7,99);
	setAction(30,9,100);

      newActionTable(31,1);
	setAction(31,1,101);

    }

  /* *********** */
  /* GOTO TABLES */
  /* *********** */

  static void initializeGotoTables ()
    {
      newGotoTables(13);

      newGotoTable(0,6);
	setGoto(0,1,1);
	setGoto(0,2,2);
	setGoto(0,3,4);
	setGoto(0,4,5);
	setGoto(0,6,6);
	setGoto(0,9,9);

      newGotoTable(1,0);

      newGotoTable(2,5);
	setGoto(2,2,35);
	setGoto(2,3,4);
	setGoto(2,4,5);
	setGoto(2,6,6);
	setGoto(2,9,9);

      newGotoTable(3,3);
	setGoto(3,4,33);
	setGoto(3,6,34);
	setGoto(3,9,9);

      newGotoTable(4,1);
	setGoto(4,5,31);

      newGotoTable(5,1);
	setGoto(5,7,29);

      newGotoTable(6,2);
	setGoto(6,8,13);
	setGoto(6,12,15);

      newGotoTable(7,1);
	setGoto(7,11,11);

      newGotoTable(8,1);
	setGoto(8,10,27);

      newGotoTable(9,2);
	setGoto(9,8,25);
	setGoto(9,12,15);

      newGotoTable(10,1);
	setGoto(10,14,19);

      newGotoTable(11,3);
	setGoto(11,8,21);
	setGoto(11,12,15);
	setGoto(11,13,20);

      newGotoTable(12,2);
	setGoto(12,8,24);
	setGoto(12,12,15);

    }

  /* ************ */
  /* STATE TABLES */
  /* ************ */

  static void initializeStateTables ()
    {
      setTables(0,0,0);
      setTables(1,1,1);
      setTables(2,2,1);
      setTables(3,3,2);
      setTables(4,3,3);
      setTables(5,4,1);
      setTables(6,5,1);
      setTables(7,6,4);
      setTables(8,7,5);
      setTables(9,8,6);
      setTables(10,9,7);
      setTables(11,10,1);
      setTables(12,11,1);
      setTables(13,12,8);
      setTables(14,13,1);
      setTables(15,14,1);
      setTables(16,8,9);
      setTables(17,15,1);
      setTables(18,16,10);
      setTables(19,8,11);
      setTables(20,17,1);
      setTables(21,18,1);
      setTables(22,19,1);
      setTables(23,8,12);
      setTables(24,20,1);
      setTables(25,21,1);
      setTables(26,22,1);
      setTables(27,23,1);
      setTables(28,24,1);
      setTables(29,25,1);
      setTables(30,26,1);
      setTables(31,27,1);
      setTables(32,28,1);
      setTables(33,29,1);
      setTables(34,30,1);
      setTables(35,31,1);
    }
}

/* ***************** */
/* ANCILLARY CLASSES */
/* ***************** */



/*
 * Ancillary classes
 */

/* ************************************************************************ */



This is the class for terms being partially parsed as syntactic objects and eventually translated into a FirstOrderTerm. N.B.: numbers are not parsed as we consider only symbolic functors.


class Term
{
  String symbol = null;
  Vector body = null;

  boolean isVariable = false;

  Term markAsVariable ()
  {
    isVariable = true;
    return this;
  }
  
  Term (String symbol)
    {
      this.symbol = symbol;
    }

  Term (String symbol, Vector body)
    {
      this.symbol = symbol;
      this.body = body;
    }

  public final boolean hasBody ()
    {
      return (body != null && !body.isEmpty());
    }

  public String toString ()
    {
      if (!hasBody())
        return symbol;
      
      StringBuffer s = new StringBuffer(symbol);

      s.append("(");
      for (int i=0; i<body.size(); i++)
        {
          s.append(body.get(i));
          if (i<body.size()-1) s.append(",");
        }
      s.append(")");

      return s.toString();
    }

  /* ************************************************************************ */

  

Converts this parsed lexical Term into an actual FirstOrderTerm defining functors as needed in the provided Signature.


  FirstOrderTerm canonical (Signature signature)
  {
    if (isVariable)
      return new Variable(symbol);

    if (!hasBody())
      return new FirstOrderTermStructure(signature.defineFunctor(symbol,0));

    FirstOrderTerm[] arguments = new FirstOrderTerm[body.size()];

    FirstOrderTerm term
      = new FirstOrderTermStructure(signature.defineFunctor(symbol,arguments.length),arguments);

    for (int i=0; i<arguments.length; i++)
      arguments[i] = ((Term)body.get(i)).canonical(signature);
    
    return term;
  }
}

/* ************************************************************************ */




This file was generated on Sun Jul 15 08:21:59 CEST 2018 from file FOTParser.java
by the hlt.language.tools.Hilite Java tool written by Hassan Aït-Kaci