Source code: com/k_int/util/CCL/CCLTest.java
1 // Title: CCL Test
2 // @version: $Id: CCLTest.java,v 1.7 2003/05/09 12:54:44 rob_tice Exp $
3 // Copyright: Copyright (C) 2001, Knowledge Integration Ltd.
4 // @author: Ian Ibbotson ( ian.ibbotson@k-int.com )
5 // Company: KI
6 // Description: Handle a CCL string
7 //
8
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version 2.1 of
13 // the license, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place - Suite
23 // 330, Boston, MA 02111-1307, USA.
24 //
25
26 package com.k_int.util.CCL;
27
28 /**
29 CCLLexer : Parse a CCL string
30 based on "Little Language pattern (Patterns in Java, Mark Grand, 1998)
31 * @author Ian Ibbotson
32 * @version $Id: CCLTest.java,v 1.7 2003/05/09 12:54:44 rob_tice Exp $
33 */
34
35
36 // Import auxillary Java classes
37 import java.io.*;
38 import java.util.*;
39
40 /**
41 * Simple test of CCL Parser, extends the Base parser and listens for
42 * parse tokens, convert the stream of tokens into prefix notation.
43 */
44
45 public class CCLTest extends BaseCCLParser
46 {
47 Stack op_stack = new Stack();
48 LinkedList postfix_list = new LinkedList();
49
50 public static void main(String args[])
51 {
52 CCLConfig config = new CCLConfig();
53 CCLTest test = new CCLTest(System.in, config);
54 }
55
56 public CCLTest(InputStream s, CCLConfig config)
57 {
58 super ( new InputStreamReader(s), config );
59
60 System.out.println("CCLTest: Enter any number of CCL strings, enter ctrl-d to quit");
61
62 while ( token != CCLLexer.EOF )
63 {
64 op_stack.clear();
65 postfix_list.clear();
66
67 // Special sentinel so we know when parsing is complete.
68 visitOpenBrace();
69 this.parse();
70 // Special sentinel so we know when parsing is complete.
71 visitCloseBrace();
72
73 System.out.println("\nBefore processing postfix: "+postfix_list.toString()+"\n\n");
74 LinkedList prefix_list = CCLHelper.postfixToPrefix(postfix_list);
75
76 System.err.println("Converting postfix into prefix list");
77 System.out.println("\nAt end of parse prefix: "+prefix_list.toString()+"\n\n");
78 }
79 }
80
81 public void visitOp(int op)
82 {
83 System.err.println("visitOp("+op+")");
84 // Pop any operators of Equal or Higher presidence off the stack
85 while ( ( op_stack.size() > 0 ) &&
86 ( op_stack.peek() instanceof CCLHelper.OpNode ) &&
87 ( ((CCLHelper.OpNode)op_stack.peek()).op >= op ) )
88 postfix_list.addLast(op_stack.pop());
89
90 op_stack.push(new CCLHelper.OpNode(op));
91 dump();
92 }
93
94 public void visitCompleteRestriction(Vector attrs, Object value)
95 {
96 System.err.println("visitCompleteRestriction()");
97 postfix_list.addLast(new CCLHelper.TermNode(value, attrs));
98 }
99
100 public void visitOpenBrace()
101 {
102 System.err.println("visitOpenBrace()");
103 op_stack.push(new CCLHelper.OpenBrace());
104 }
105
106 public void visitCloseBrace()
107 {
108 System.err.println("visitCloseBrace()");
109
110 // Pop everything off the operator stack until we reach a (
111 Object o = op_stack.pop();
112 while ( ( ! ( o instanceof CCLHelper.OpenBrace ) ) && ( op_stack.size() > 0 ) )
113 {
114 postfix_list.addLast(o);
115 o = op_stack.pop();
116 }
117 }
118
119 public void dump()
120 {
121 System.out.println("Op Stack: "+op_stack.toString());
122 System.out.println("Arg List : "+postfix_list.toString());
123 }
124
125 public boolean isCCLQualifier(String s)
126 {
127 if ( config.lookup(s) != null )
128 return true;
129
130 return false;
131 }
132 }