Source code: com/k_int/util/CCL/CCLHelper.java
1 // Title: CCLHelper
2 // @version: $Id: CCLHelper.java,v 1.3 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:
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: CCLHelper.java,v 1.3 2003/05/09 12:54:44 rob_tice Exp $
33 */
34
35
36 // Import auxillary Java classes
37 import java.util.*;
38
39 /**
40 * Simple test of CCL Parser, extends the Base parser and listens for
41 * parse tokens, convert the stream of tokens into prefix notation.
42 */
43
44 public class CCLHelper
45 {
46 public static class ParseNode
47 {
48 public String toString() { return "ParseNode"; }
49 }
50
51 public static class OpenBrace extends ParseNode
52 {
53 public String toString() { return "OpenBrace"; }
54 }
55
56 public static class OpNode extends ParseNode
57 {
58 protected int op;
59
60 public OpNode(int op)
61 {
62 this.op = op;
63 }
64
65 public String toString() { return "OpNode "+op; }
66 }
67
68 public static class TermNode extends ParseNode
69 {
70 protected Object term;
71 protected Vector attrs;
72 public TermNode(Object term, Vector attrs) { this.term = term; this.attrs=attrs; }
73
74 public String toString() { return "TermNode:("+attrs.toString()+")\""+term.toString()+"\""; }
75 }
76
77 public static LinkedList postfixToPrefix(LinkedList postfix_list)
78 {
79 LinkedList result = new LinkedList();
80
81 ParseNode pn = (ParseNode) postfix_list.removeLast();
82 if ( pn instanceof OpNode )
83 {
84 result.addLast(pn);
85 LinkedList rhs = postfixToPrefix(postfix_list);
86 LinkedList lhs = postfixToPrefix(postfix_list);
87 result.addAll(lhs);
88 result.addAll(rhs);
89 }
90 else
91 {
92 result.addLast(pn);
93 }
94
95 return result;
96 }
97 }