|
FOTParser.java
|
// ******************************************************************* // This file has been automatically generated from the grammar in file // FOT.grm by hlt.language.syntax.ParserGenerator on // Fri Jun 15 11:01:19 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: out.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 17: { terms.push(new Term(node($rule$,1).svalue())); break; } case 18: { terms.push(new Term(node($rule$,-1).svalue(),new Vector())); break; } case 21: { term = (Term)terms.pop(); ((Term)terms.peek()).body.add(term); break; } case 22: { 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[14]; 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,"Body"); newNonTerminal(13,"$ACTION5$"); } /* **************** */ /* PRODUCTION RULES */ /* **************** */ static void initializeRules () { rules = new ParserRule[23]; 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(13,0,18,1,2); rules[19] = new ParserRule(8,5,19,1,2); rules[20] = new ParserRule(8,3,20,1,2); rules[21] = new ParserRule(12,1,21,1,2); rules[22] = new ParserRule(12,3,22,1,2); } /* ************** */ /* PARSER ACTIONS */ /* ************** */ static void initializeParserActions () { actions = new ParserAction[99]; 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,15); newAction(33,0,16); 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,19); newAction(50,0,14); newAction(51,0,15); newAction(52,0,16); newAction(53,0,18); newAction(54,1,20); newAction(55,1,20); newAction(56,1,20); newAction(57,1,18); newAction(58,1,18); newAction(59,1,18); newAction(60,0,14); newAction(61,0,15); newAction(62,0,16); newAction(63,0,23); newAction(64,0,24); newAction(65,1,21); newAction(66,1,21); newAction(67,1,19); newAction(68,1,19); newAction(69,1,19); newAction(70,0,14); newAction(71,0,15); newAction(72,0,16); newAction(73,1,22); newAction(74,1,22); newAction(75,0,27); newAction(76,1,13); newAction(77,1,13); newAction(78,1,13); newAction(79,1,13); newAction(80,1,13); newAction(81,1,13); newAction(82,0,29); newAction(83,1,10); newAction(84,1,10); newAction(85,1,10); newAction(86,1,10); newAction(87,1,10); newAction(88,1,10); newAction(89,0,31); newAction(90,1,6); newAction(91,1,3); newAction(92,1,8); newAction(93,1,8); newAction(94,1,8); newAction(95,1,8); newAction(96,1,8); newAction(97,1,8); newAction(98,1,2); } /* ************* */ /* PARSER STATES */ /* ************* */ static void initializeParserStates () { states = new ParserState[35]; for (int i=0; i<35; i++) newState(i); } /* ************* */ /* ACTION TABLES */ /* ************* */ static void initializeActionTables () { newActionTables(31); 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,32); setAction(8,6,31); setAction(8,9,33); 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,4); setAction(14,8,46); setAction(14,9,49); setAction(14,10,47); setAction(14,11,48); newActionTable(15,1); setAction(15,10,53); newActionTable(16,3); setAction(16,8,54); setAction(16,10,55); setAction(16,11,56); newActionTable(17,3); setAction(17,5,57); setAction(17,6,58); setAction(17,9,59); newActionTable(18,2); setAction(18,10,63); setAction(18,11,64); newActionTable(19,2); setAction(19,10,65); setAction(19,11,66); newActionTable(20,3); setAction(20,8,67); setAction(20,10,68); setAction(20,11,69); newActionTable(21,2); setAction(21,10,73); setAction(21,11,74); newActionTable(22,1); setAction(22,8,75); newActionTable(23,6); setAction(23,2,76); setAction(23,4,77); setAction(23,5,78); setAction(23,6,79); setAction(23,7,80); setAction(23,9,81); newActionTable(24,1); setAction(24,8,82); newActionTable(25,6); setAction(25,2,83); setAction(25,4,84); setAction(25,5,85); setAction(25,6,86); setAction(25,7,87); setAction(25,9,88); newActionTable(26,1); setAction(26,8,89); newActionTable(27,1); setAction(27,1,90); newActionTable(28,1); setAction(28,1,91); newActionTable(29,6); setAction(29,2,92); setAction(29,4,93); setAction(29,5,94); setAction(29,6,95); setAction(29,7,96); setAction(29,9,97); newActionTable(30,1); setAction(30,1,98); } /* *********** */ /* 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,34); setGoto(2,3,4); setGoto(2,4,5); setGoto(2,6,6); setGoto(2,9,9); newGotoTable(3,3); setGoto(3,4,32); setGoto(3,6,33); setGoto(3,9,9); newGotoTable(4,1); setGoto(4,5,30); newGotoTable(5,1); setGoto(5,7,28); newGotoTable(6,1); setGoto(6,8,13); newGotoTable(7,1); setGoto(7,11,11); newGotoTable(8,1); setGoto(8,10,26); newGotoTable(9,1); setGoto(9,8,17); newGotoTable(10,1); setGoto(10,13,20); newGotoTable(11,2); setGoto(11,8,22); setGoto(11,12,21); newGotoTable(12,1); setGoto(12,8,25); } /* ************ */ /* 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,1); setTables(19,17,10); setTables(20,8,11); setTables(21,18,1); setTables(22,19,1); setTables(23,20,1); setTables(24,8,12); 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); } } /* ***************** */ /* 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 Fri Jun 15 11:10:59 CEST 2018 from file FOTParser.java
by the hlt.language.tools.Hilite Java tool written by Hassan Aït-Kaci