KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fri > patterns > interpreter > parsergenerator > examples > Calculator


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

85     }
86
87 }
88
Popular Tags