001: /* Generated By:JavaCC: Do not edit this line. ExpressionParser.java */
002: package org.concern.model.parser;
003:
004: import java.io.*;
005: import org.concern.model.parser.ast.*;
006: import java.util.*;
007:
008: public class ExpressionParser implements ExpressionParserConstants {
009: public static void main(String args[]) throws Exception {
010: try {
011: String expr = "!(!(a-li baba's horse) || !binary code\n) && this computer rulez";
012: System.out.println("Parsed expression: " + expr);
013: ExpressionParser p = new ExpressionParser(new StringReader(
014: expr));
015: AbstractNode n = p.or();
016: System.out.println(n);
017: } catch (Throwable t) {
018: t.printStackTrace();
019: }
020: }
021:
022: final public AbstractNode or() throws ParseException {
023: LinkedList children = new LinkedList();
024: AbstractNode n1;
025: AbstractNode n2;
026: n1 = and();
027: label_1: while (true) {
028: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
029: case OR:
030: ;
031: break;
032: default:
033: jj_la1[0] = jj_gen;
034: break label_1;
035: }
036: jj_consume_token(OR);
037: n2 = and();
038: children.add(n2);
039: }
040: if (children.size() > 0) {
041: children.addFirst(n1);
042: {
043: if (true)
044: return new OrOperator(children);
045: }
046: } else {
047: {
048: if (true)
049: return n1;
050: }
051: }
052: throw new Error("Missing return statement in function");
053: }
054:
055: final public AbstractNode and() throws ParseException {
056: LinkedList children = new LinkedList();
057: AbstractNode n1;
058: AbstractNode n2;
059: n1 = negation();
060: label_2: while (true) {
061: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
062: case AND:
063: ;
064: break;
065: default:
066: jj_la1[1] = jj_gen;
067: break label_2;
068: }
069: jj_consume_token(AND);
070: n2 = negation();
071: children.add(n2);
072: }
073: if (children.size() > 0) {
074: children.addFirst(n1);
075: {
076: if (true)
077: return new AndOperator(children);
078: }
079: } else {
080: {
081: if (true)
082: return n1;
083: }
084: }
085: throw new Error("Missing return statement in function");
086: }
087:
088: final public AbstractNode negation() throws ParseException {
089: AbstractNode n;
090: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
091: case NOT:
092: jj_consume_token(NOT);
093: n = negation();
094: n = new NotOperator(n);
095: break;
096: case IDENTIFIER_CHAR:
097: case LPAREN:
098: n = unaryExpression();
099: break;
100: default:
101: jj_la1[2] = jj_gen;
102: jj_consume_token(-1);
103: throw new ParseException();
104: }
105: {
106: if (true)
107: return n;
108: }
109: throw new Error("Missing return statement in function");
110: }
111:
112: final public AbstractNode unaryExpression() throws ParseException {
113: AbstractNode n;
114: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
115: case IDENTIFIER_CHAR:
116: n = identifier();
117: break;
118: case LPAREN:
119: jj_consume_token(LPAREN);
120: n = or();
121: jj_consume_token(RPAREN);
122: break;
123: default:
124: jj_la1[3] = jj_gen;
125: jj_consume_token(-1);
126: throw new ParseException();
127: }
128: {
129: if (true)
130: return n;
131: }
132: throw new Error("Missing return statement in function");
133: }
134:
135: final public AbstractNode identifier() throws ParseException {
136: AbstractNode n;
137: StringBuffer buff = new StringBuffer();
138: Token t = null;
139: label_3: while (true) {
140: t = jj_consume_token(IDENTIFIER_CHAR);
141: buff.append(t.image);
142: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
143: case IDENTIFIER_CHAR:
144: ;
145: break;
146: default:
147: jj_la1[4] = jj_gen;
148: break label_3;
149: }
150: }
151: {
152: if (true)
153: return new Name(buff.toString().trim());
154: }
155: throw new Error("Missing return statement in function");
156: }
157:
158: public ExpressionParserTokenManager token_source;
159: SimpleCharStream jj_input_stream;
160: public Token token, jj_nt;
161: private int jj_ntk;
162: private int jj_gen;
163: final private int[] jj_la1 = new int[5];
164: static private int[] jj_la1_0;
165: static {
166: jj_la1_0();
167: }
168:
169: private static void jj_la1_0() {
170: jj_la1_0 = new int[] { 0x10, 0x8, 0x62, 0x42, 0x2, };
171: }
172:
173: public ExpressionParser(java.io.InputStream stream) {
174: jj_input_stream = new SimpleCharStream(stream, 1, 1);
175: token_source = new ExpressionParserTokenManager(jj_input_stream);
176: token = new Token();
177: jj_ntk = -1;
178: jj_gen = 0;
179: for (int i = 0; i < 5; i++)
180: jj_la1[i] = -1;
181: }
182:
183: public void ReInit(java.io.InputStream stream) {
184: jj_input_stream.ReInit(stream, 1, 1);
185: token_source.ReInit(jj_input_stream);
186: token = new Token();
187: jj_ntk = -1;
188: jj_gen = 0;
189: for (int i = 0; i < 5; i++)
190: jj_la1[i] = -1;
191: }
192:
193: public ExpressionParser(java.io.Reader stream) {
194: jj_input_stream = new SimpleCharStream(stream, 1, 1);
195: token_source = new ExpressionParserTokenManager(jj_input_stream);
196: token = new Token();
197: jj_ntk = -1;
198: jj_gen = 0;
199: for (int i = 0; i < 5; i++)
200: jj_la1[i] = -1;
201: }
202:
203: public void ReInit(java.io.Reader stream) {
204: jj_input_stream.ReInit(stream, 1, 1);
205: token_source.ReInit(jj_input_stream);
206: token = new Token();
207: jj_ntk = -1;
208: jj_gen = 0;
209: for (int i = 0; i < 5; i++)
210: jj_la1[i] = -1;
211: }
212:
213: public ExpressionParser(ExpressionParserTokenManager tm) {
214: token_source = tm;
215: token = new Token();
216: jj_ntk = -1;
217: jj_gen = 0;
218: for (int i = 0; i < 5; i++)
219: jj_la1[i] = -1;
220: }
221:
222: public void ReInit(ExpressionParserTokenManager tm) {
223: token_source = tm;
224: token = new Token();
225: jj_ntk = -1;
226: jj_gen = 0;
227: for (int i = 0; i < 5; i++)
228: jj_la1[i] = -1;
229: }
230:
231: final private Token jj_consume_token(int kind)
232: throws ParseException {
233: Token oldToken;
234: if ((oldToken = token).next != null)
235: token = token.next;
236: else
237: token = token.next = token_source.getNextToken();
238: jj_ntk = -1;
239: if (token.kind == kind) {
240: jj_gen++;
241: return token;
242: }
243: token = oldToken;
244: jj_kind = kind;
245: throw generateParseException();
246: }
247:
248: final public Token getNextToken() {
249: if (token.next != null)
250: token = token.next;
251: else
252: token = token.next = token_source.getNextToken();
253: jj_ntk = -1;
254: jj_gen++;
255: return token;
256: }
257:
258: final public Token getToken(int index) {
259: Token t = token;
260: for (int i = 0; i < index; i++) {
261: if (t.next != null)
262: t = t.next;
263: else
264: t = t.next = token_source.getNextToken();
265: }
266: return t;
267: }
268:
269: final private int jj_ntk() {
270: if ((jj_nt = token.next) == null)
271: return (jj_ntk = (token.next = token_source.getNextToken()).kind);
272: else
273: return (jj_ntk = jj_nt.kind);
274: }
275:
276: private java.util.Vector jj_expentries = new java.util.Vector();
277: private int[] jj_expentry;
278: private int jj_kind = -1;
279:
280: public ParseException generateParseException() {
281: jj_expentries.removeAllElements();
282: boolean[] la1tokens = new boolean[8];
283: for (int i = 0; i < 8; i++) {
284: la1tokens[i] = false;
285: }
286: if (jj_kind >= 0) {
287: la1tokens[jj_kind] = true;
288: jj_kind = -1;
289: }
290: for (int i = 0; i < 5; i++) {
291: if (jj_la1[i] == jj_gen) {
292: for (int j = 0; j < 32; j++) {
293: if ((jj_la1_0[i] & (1 << j)) != 0) {
294: la1tokens[j] = true;
295: }
296: }
297: }
298: }
299: for (int i = 0; i < 8; i++) {
300: if (la1tokens[i]) {
301: jj_expentry = new int[1];
302: jj_expentry[0] = i;
303: jj_expentries.addElement(jj_expentry);
304: }
305: }
306: int[][] exptokseq = new int[jj_expentries.size()][];
307: for (int i = 0; i < jj_expentries.size(); i++) {
308: exptokseq[i] = (int[]) jj_expentries.elementAt(i);
309: }
310: return new ParseException(token, exptokseq, tokenImage);
311: }
312:
313: final public void enable_tracing() {
314: }
315:
316: final public void disable_tracing() {
317: }
318:
319: }
|