Building Parsers with Java

sjm.examples.logic
Class LogikusParser

java.lang.Object
  |
  +--sjm.examples.logic.LogikusParser

public class LogikusParser
extends java.lang.Object

This class provides a parser for Logikus, a logic language similar to Prolog.

The grammar this class supports is:

 
 
     axiom        = structure (ruleDef | Empty);
     structure    = functor ('(' commaList(term) ')' | Empty);
     functor      = '.' | LowercaseWord | QuotedString;
     term         = structure | Num | list | variable;
     variable     = UppercaseWord | '_';
 
ruleDef = ":-" commaList(condition); condition = structure | not | evaluation | comparison | list;
not = "not" structure ;
evaluation = '#' '(' arg ',' arg ')'; comparison = operator '(' arg ',' arg ')'; arg = expression | functor; operator = '<' | '>' | '=' | "<=" | ">=" | "!=" ; expression = phrase ('+' phrase | '-' phrase)*; phrase = factor ('*' factor | '/' factor)*; factor = '(' expression ')' | Num | variable;
list = '[' (listContents | Empty) ']'; listContents = commaList(term) listTail; listTail = ('|' (variable | list)) | Empty;
commaList(p) = p (',' p)*;
The following program and query use most of the features of the Logikus grammar:
     // program
     member(X, [X | Rest]);
     member(X, [Y | Rest]) :- member(X, Rest);
     primes([2, 3, 5, 7, 11, 13]);
     factor(X, P, Q) :- 
         primes(Primes), 
         member(P, Primes), member(Q, Primes), =(P*Q, X); 
 
// query factor(91, A, B)
// results A = 7.0, B = 13.0 A = 13.0, B = 7.0 no
The class LogikusFacade simplifies the construction of Program and Query objects from the text given above. A Java program can prove the query to generate the results.

The class LogikusIde is an example of using the Logikus parser in practice. It uses LogikusFacade to create a Query, proves the query, and displays the query's variables for each proof. As in Prolog, the Logikus development environment prints "no" when no further proofs remain.


Field Summary
protected  Sequence expression
           
protected  Sequence list
           
protected  Sequence structure
           
 
Constructor Summary
LogikusParser()
           
 
Method Summary
protected  Parser arg()
           
 Parser axiom()
          Return a parser that recognizes the grammar:
protected static Sequence commaList(Parser p)
           
 Sequence comparison()
          Return a parser that recognizes the grammar:
 Alternation condition()
          Return a parser that recognizes the grammar:
protected  Parser divideFactor()
           
protected  Parser evaluation()
           
protected  Parser expression()
           
protected  Parser factor()
           
protected  Parser functor()
           
 Sequence list()
          Return a parser that recognizes the grammar:
protected  Parser listContents()
           
protected  Parser listTail()
           
protected  Parser minusPhrase()
           
protected  Parser not()
           
 Parser num()
           
protected  Parser operator()
           
protected  Parser phrase()
           
protected  Parser plusPhrase()
           
static Parser query()
          Return a parser that recognizes the grammar:
protected  Parser ruleDef()
           
static Parser start()
          Return a parser that recognizes the grammar:
protected  Parser structure()
           
protected  Parser term()
           
protected  Parser timesFactor()
           
protected  Parser variable()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

structure

protected Sequence structure

expression

protected Sequence expression

list

protected Sequence list
Constructor Detail

LogikusParser

public LogikusParser()
Method Detail

arg

protected Parser arg()

axiom

public Parser axiom()
Return a parser that recognizes the grammar:
    axiom = structure (ruleDef | Empty);
 
Returns:
a parser that recognizes an axiom

commaList

protected static Sequence commaList(Parser p)

comparison

public Sequence comparison()
Return a parser that recognizes the grammar:
    comparison = operator '(' arg ',' arg ')';
 
Returns:
a parser that recognizes a comparison

condition

public Alternation condition()
Return a parser that recognizes the grammar:
    condition = structure | not | evaluation | comparison | 
                list;
 
Returns:
a parser that recognizes a condition

divideFactor

protected Parser divideFactor()

evaluation

protected Parser evaluation()

expression

protected Parser expression()

factor

protected Parser factor()

functor

protected Parser functor()

list

public Sequence list()
Return a parser that recognizes the grammar:
    list = '[' (listContents | Empty) ']';
 
The class comment gives the complete grammar for lists, as part of the Logikus grammar.
Returns:
a parser that recognizes a list

listContents

protected Parser listContents()

listTail

protected Parser listTail()

minusPhrase

protected Parser minusPhrase()

not

protected Parser not()

num

public Parser num()

operator

protected Parser operator()

phrase

protected Parser phrase()

plusPhrase

protected Parser plusPhrase()

query

public static Parser query()
Return a parser that recognizes the grammar:
    query = commaList(condition);
 
Returns:
a parser that recognizes a query

ruleDef

protected Parser ruleDef()

start

public static Parser start()
Return a parser that recognizes the grammar:
    axiom = condition (ruleDefinition | empty);
 
Returns:
a parser that recognizes an axiom

structure

protected Parser structure()

term

protected Parser term()

timesFactor

protected Parser timesFactor()

variable

protected Parser variable()

by Steve Metsker