|
FOT.grm
|
// FILE. . . . . d:/hak/hlt/src/hlt/fot/syntax/sources/FOT.grm // EDIT BY . . . Hassan Ait-Kaci // ON MACHINE. . Hak-Laptop // STARTED ON. . Sat Jul 14 10:27:06 2018
|
This is a Jacc specification for an interactive parser for Prolog
terms, also known as First-Order Terms. We shall proceed as
for parsing Prolog terms but (at least initially), not bothering with
dynamic operators such as done in this
other term parser. In fact, this one differentiates variables
and structures at the tokenizer level. Although the
notation [] is tokenized as an atomic symbol, Prolog list
notation consisting of bracketed bar-separated pairs (such as
[...|...]) or bracketed comma-separated sequences (such as
[..., ..., ...]) are not tokenized or parsed as in Prolog.
|
/* * Declarations */ %import java.util.*; %import hlt.math.fuzzy.fot.*; %start Session %token PRAGMA FUNCTOR VARIABLE // NUMBER // a number is tokenized as a functor (symbolic constant) %{ 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); } } %} %% /* * Production rules */ Session : Clauses Exit | Exit ; Exit : 'exit' { out.println("*** Bye bye!..."); System.exit(0); } '.' ; Clauses : Clause | Clauses Clause ; Clause : PRAGMA { executePragma($1.svalue()); FOTTokenizer.prompt(); } '.' | { terms = new Stack(); } Term { 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(); } '.' | error { errorManager().reportErrors(true); FOTTokenizer.prompt(); } '.' ; Term : VARIABLE { terms.push(new Term($1.svalue()).markAsVariable()); } // | NUMBER // { // terms.push($1.isInteger() ? new Term((int)$1.nvalue()) // : new Term($1.nvalue())); // } | Structure | '(' Term ')' ; Structure : FUNCTOR { terms.push(new Term($1.svalue())); } | FUNCTOR '(' { terms.push(new Term($1.svalue(),new Vector())); } Body ')' ; Body : Term { term = (Term)terms.pop(); ((Term)terms.peek()).body.add(term); } | Body ',' Term { term = (Term)terms.pop(); ((Term)terms.peek()).body.add(term); } ; %% /* * 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 FOT.grm
by the hlt.language.tools.Hilite Java tool written by Hassan Aït-Kaci