14.1 Building a Logic Language Environment


 
Building Parsers with Java
By Steven John Metsker

Table of Contents
Chapter 14.  Parsing a Logic Language

    Content

A logic language lets a user compose a logic program and pose queries against the program. Figure 14.1 shows the elements of a logic programming environment.

Figure 14.1. The elements of a logic programming environment. In a logic programming environment, the user supplies both the program and a query. The environment parses these and uses a logic engine to prove the query against the program.

graphics/14fig01.gif

Logic languages need an engine to do the work of determining which facts and rules can prove a user's query. The code for the engine used by Logikus lies within the package sjm.engine . You can write Java programs that directly manipulate the engine classes. The package sjm.examples.engine has examples that show how to exercise various features of the engine. The problem with accessing the engine directly from Java is that the code is more voluminous and more complicated than equivalent Logikus code. For example, here is a program that shows how to create the bachelor program of Chapter 13:

 package sjm.examples.engine;  import sjm.engine.*; /**  * Show a <code>Not</code> object in action.  */ public class ShowNot { public static void main(String[] args) {     Program p = new Program();     // bachelor(X) :- male(X), not married(X);     Variable x = new Variable("X");     Structure s0 = new Structure("bachelor", new Term[]{x});     Structure s1 = new Structure("male", new Term[]{x});     Structure s2 = new Not("married", new Term[]{x});     Rule r0 = new Rule(new Structure[]{s0, s1, s2});     p.addAxiom(r0);     // married(jim)     p.addAxiom(new Fact("married", "jim"));     // male(jeremy); male(jim);     p.addAxiom(new Fact("male", "jeremy"));     p.addAxiom(new Fact("male", "jim"));     System.out.println(p);     Variable b = new Variable("B");     Query q = new Query(p,         new Structure("bachelor", new Term[]{b}));     while (q.canFindNextProof()) {         System.out.println();         System.out.println(b + " is a bachelor");     } } } 

Running this program prints

 bachelor(X) :- male(X), not married(X);  married(jim); male(jeremy); male(jim); jeremy is a bachelor 

The Java program constructs and displays a Program object and constructs and issues a Query object against this program. The Program object displays itself as text that a Logikus parser could correctly interpret. Logikus is effectively a facade that makes it easy to use the engine.

Assemblers for a Logikus parser create objects from the classes in sjm.engine . The tasks for building a Logikus parser are the same fundamental tasks as for the other languages in this book:

  • Write a grammar.

  • Write the assemblers that build a Logikus axiom .

  • Generate a parser from the grammar, plugging in the assemblers.


   
Top


Building Parsers with Java
Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net