307
308 switch (lookahead.ttype) {
309 case OPENPAREN:
310 match(OPENPAREN);
311 e = expression();
312 match(CLOSEPAREN);
313 break;
314 case StreamTokenizer.TT_WORD:
315 String s = lookahead.sval;
316 if (isReservedWord(s)) {
317 throw new SyntaxException(st.lineno(), "IDENTIFIER",
318 "Reserved Word: " + lookahead.sval);
319 }
320 matchID();
321 e = new Identifier(s);
322 log(pdebug, "Parsed: ID -> " + s);
323 break;
324 case StreamTokenizer.TT_NUMBER:
325 double literal = lookahead.nval;
326 matchNumber();
327 e = new Literal(new Double(literal));
328 log(pdebug, "Parsed: number -> " + literal);
329 break;
330 default:
331 throw new SyntaxException(st.lineno(), "IDENTIFIER", lookahead);
332 }
333 log(pdebug, "Parsed: primary -> " + e);
334 return e;
335 }
336
337 /**
338 * Unary -> ('+'|'-') Unary | Primary
339 */
340 private Expression unary() throws ParserException, IOException {
341 Expression e = null;
342 Operator op = null;
343
344 while (true) {
345 switch (lookahead.ttype) {
346 case OPERATOR_PLUS:
347 match(OPERATOR_PLUS);
348 op = Operator.PLUS;
349 break;
350 case OPERATOR_MINUS:
351 match(OPERATOR_MINUS);
352 op = Operator.MINUS;
353 break;
354 default:
355 e = primary();
356 log(pdebug, "Parsed: unary -> " + e);
357 return e;
358 }
359 Expression e1 = new Expression();
360 e1.setOperator(op);
361 e1.setRight(e);
362 log(pdebug, "Parsed: unary -> " + e1);
363 e1.setLeft(new Literal(new Double(0)));
364 e = e1;
365 }
366 }
367
368 /**
369 * MultExpression -> Unary (('*' | '/') Unary)*
370 */
371 private Expression multExpression() throws ParserException, IOException {
372 Expression e = unary();
373 Operator op = null;
374
375 while (true) {
376 switch (lookahead.ttype) {
377 case OPERATOR_MULTIPLY:
378 match(OPERATOR_MULTIPLY);
379 op = Operator.MULTIPLY;
380 break;
381 case OPERATOR_DIVIDE:
382 match(OPERATOR_DIVIDE);
383 op = Operator.DIVIDE;
|
307
308 switch (lookahead.ttype) {
309 case OPENPAREN:
310 match(OPENPAREN);
311 e = expression();
312 match(CLOSEPAREN);
313 break;
314 case StreamTokenizer.TT_WORD:
315 String s = lookahead.sval;
316 if (isReservedWord(s)) {
317 throw new SyntaxException(st.lineno(), "IDENTIFIER",
318 "Reserved Word: " + lookahead.sval);
319 }
320 matchID();
321 e = new Identifier(s);
322 log(pdebug, "Parsed: ID -> " + s);
323 break;
324 case StreamTokenizer.TT_NUMBER:
325 double literal = lookahead.nval;
326 matchNumber();
327 e = new Literal(Double.valueOf(literal));
328 log(pdebug, "Parsed: number -> " + literal);
329 break;
330 default:
331 throw new SyntaxException(st.lineno(), "IDENTIFIER", lookahead);
332 }
333 log(pdebug, "Parsed: primary -> " + e);
334 return e;
335 }
336
337 /**
338 * Unary -> ('+'|'-') Unary | Primary
339 */
340 private Expression unary() throws ParserException, IOException {
341 Expression e = null;
342 Operator op = null;
343
344 while (true) {
345 switch (lookahead.ttype) {
346 case OPERATOR_PLUS:
347 match(OPERATOR_PLUS);
348 op = Operator.PLUS;
349 break;
350 case OPERATOR_MINUS:
351 match(OPERATOR_MINUS);
352 op = Operator.MINUS;
353 break;
354 default:
355 e = primary();
356 log(pdebug, "Parsed: unary -> " + e);
357 return e;
358 }
359 Expression e1 = new Expression();
360 e1.setOperator(op);
361 e1.setRight(e);
362 log(pdebug, "Parsed: unary -> " + e1);
363 e1.setLeft(new Literal(Double.valueOf(0)));
364 e = e1;
365 }
366 }
367
368 /**
369 * MultExpression -> Unary (('*' | '/') Unary)*
370 */
371 private Expression multExpression() throws ParserException, IOException {
372 Expression e = unary();
373 Operator op = null;
374
375 while (true) {
376 switch (lookahead.ttype) {
377 case OPERATOR_MULTIPLY:
378 match(OPERATOR_MULTIPLY);
379 op = Operator.MULTIPLY;
380 break;
381 case OPERATOR_DIVIDE:
382 match(OPERATOR_DIVIDE);
383 op = Operator.DIVIDE;
|