blob: cc22b0a3074bbc275d41712f03e85d9afa1cd532 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
options{
//DEBUG_PARSER = true; DEBUG_TOKEN_MANAGER = true;
}
PARSER_BEGIN(Calculator)
import java.util.HashMap;
public class Calculator {
private static HashMap variables = new HashMap();
public static void main(String[] args) {
Calculator parser = new Calculator(System.in);
while (true){
System.out.print("Type your expression: ");
try{
double value = parser.evaluateNextLine();
System.out.println("Returned: " + value);
}catch (ParseException e){
System.out.println(e.getMessage());
try{
parser.passStatement(); // wiping the tokens of the malformed expression
}catch (ParseException ex){
System.out.println(ex.getMessage());
}
}
}
}
}
PARSER_END(Calculator)
SKIP : {
" "|"\t"
}
TOKEN : {
<EOL: "\n"|"\r">|
<PAL: "(">|
<PAR: ")">|
<ADD: "+">|
<SUB: "-">|
<MUL: "*">|
<DIV: "/">|
<EQU: "=">|
<LET: "let">|
<END: "exit">|
<NUM: (["0"-"9"])+("."((["0"-"9"])+))?>|
<ID: (["a"-"z"]|["A"-"Z"])+>
}
double statement() :
{
Token id;
double i = 0.0;
}
{
<END> { System.exit(0); }|
i=expression() { return i; }|
<LET> id=<ID> <EQU> i=expression()
{
variables.put(id.image,i); //Storing the variable
return i;
}
}
double expression() :
{
Token op = null;
double l = 0.0;
double r = 0.0;
}
{
l=term() ((op=<ADD> | op=<SUB>) r=term()
{ // folding everything in the l variable
if (op != null){
if (op.image.equals("+"))
l += r;
else
l -= r;
}
}
)*
{ //returning the parsed value
return l;
}
}
double term() :
{
Token op = null;
double l = 0.0;
double r = 0.0;
}
{
l=factor() ((op=<DIV> | op=<MUL>) r=factor()
{ // folding everything in the l variable
if (op != null){
if (op.image.equals("/"))
l /= r;
else
l *= r;
}
}
)*
{ //returning the parsed value
return l;
}
}
double factor() :
{
Token t = null;
Token id = null;
double i = 0.0;
}
{
(t=<NUM> | id=<ID> | <PAL> (i=expression())? <PAR>)
{
if(id != null){
Object var = variables.get(id.image);
if (var != null)
i = (double) var;
else
throw new ParseException("'" + id.image + "' is not yet defined.");
}else if(t != null)
i = Double.parseDouble(t.image);
return i;
}|
<SUB> (t=<NUM> | id=<ID> | <PAL> (i=expression())? <PAR>)
{
if(id != null){
i = (double)variables.get(id.image);
}else if(t != null)
i = Double.parseDouble(t.image);
return i*(-1);
}
}
double evaluateNextLine() :
{
double result = 0.0;
}
{
(result=statement())? <EOL> {return result;}|
<EOF>{System.out.println("\nBuffer was closed. Exiting."); System.exit(0);}
}
void passStatement() :
{}
{
<EOL>|
<EOF>{System.out.println("\nBuffer was closed. Exiting."); System.exit(0);}
}
|