01: package fri.patterns.interpreter.parsergenerator.examples;
02:
03: import fri.patterns.interpreter.parsergenerator.*;
04: import fri.patterns.interpreter.parsergenerator.semantics.*;
05: import fri.patterns.interpreter.parsergenerator.builder.SerializedParser;
06:
07: /**
08: Calculator for arithmetic expressions, showing the elegance of ReflectSemantic.
09: <p>
10: Syntax: java fri.patterns.interpreter.parsergenerator.examples.Calculator '(4+2.3) *(2 - -6) + 3*2'
11:
12: @author Fritz Ritzberger
13: */
14:
15: public class Calculator extends ReflectSemantic {
16: private static String[][] rules = { // arithmetic sample
17: { "EXPRESSION", "TERM" },
18: { "EXPRESSION", "EXPRESSION", "'+'", "TERM" },
19: { "EXPRESSION", "EXPRESSION", "'-'", "TERM" },
20: { "TERM", "FACTOR", }, { "TERM", "TERM", "'*'", "FACTOR" },
21: { "TERM", "TERM", "'/'", "FACTOR" },
22: { "FACTOR", "`number`", },
23: { "FACTOR", "'-'", "FACTOR" }, // need LALRParserTables instead of SLRParserTables because of this rule
24: { "FACTOR", "'('", "EXPRESSION", "')'" },
25: { Token.IGNORED, "`whitespaces`" }, };
26:
27: public Object EXPRESSION(Object TERM) {
28: return TERM; // do not really need this method as ReflectSemantic.fallback() does this
29: }
30:
31: public Object EXPRESSION(Object EXPRESSION, Object operator,
32: Object TERM) {
33: if (operator.equals("+"))
34: return new Double(((Double) EXPRESSION).doubleValue()
35: + ((Double) TERM).doubleValue());
36: return new Double(((Double) EXPRESSION).doubleValue()
37: - ((Double) TERM).doubleValue());
38: }
39:
40: public Object TERM(Object FACTOR) {
41: return FACTOR; // do not really need this method as ReflectSemantic.fallback() does this
42: }
43:
44: public Object TERM(Object TERM, Object operator, Object FACTOR) {
45: if (operator.equals("*"))
46: return new Double(((Double) TERM).doubleValue()
47: * ((Double) FACTOR).doubleValue());
48: return new Double(((Double) TERM).doubleValue()
49: / ((Double) FACTOR).doubleValue());
50: }
51:
52: public Object FACTOR(Object number) {
53: return Double.valueOf((String) number);
54: }
55:
56: public Object FACTOR(Object minus, Object FACTOR) {
57: return new Double(-((Double) FACTOR).doubleValue());
58: }
59:
60: public Object FACTOR(Object leftParenthesis, Object EXPRESSION,
61: Object rightParenthesis) {
62: return EXPRESSION;
63: }
64:
65: /** SYNTAX: java fri.patterns.interpreter.parsergenerator.examples.Calculator '(4+2.3) *(2 - -6) + 3*2' ... 56.4. */
66: public static void main(String[] args) throws Exception {
67: if (args.length <= 0) {
68: System.err.println("SYNTAX: java "
69: + Calculator.class.getName()
70: + " \"(4+2.3) *(2 - -6) + 3*2\"");
71: System.exit(1);
72: }
73:
74: String input = args[0];
75: for (int i = 1; i < args.length; i++)
76: input = input + " " + args[i];
77:
78: System.err.println("Calculating input >" + input + "<");
79:
80: Parser parser = new SerializedParser().get(rules, "Calculator");
81: boolean ok = parser.parse(input, new Calculator());
82: System.err.println("Parse return " + ok + ", result: "
83: + parser.getResult());
84:
85: /* Variant without SerializedParser:
86: SyntaxSeparation separation = new SyntaxSeparation(new Syntax(rules)); // takes away IGNORED
87: LexerBuilder builder = new LexerBuilder(separation.getLexerSyntax(), separation.getIgnoredSymbols());
88: Lexer lexer = builder.getLexer(input);
89: ParserTables parserTables = new LALRParserTables(separation.getParserSyntax());
90: Parser parser = new Parser(parserTables);
91: boolean ok = parser.parse(lexer, new Calculator());
92: System.err.println("Parse return "+ok+", result: "+parser.getResult());
93: */
94: }
95:
96: }
|