-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cup
67 lines (56 loc) · 1.73 KB
/
parser.cup
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
package com.pacheco.mee_3;
import java_cup.runtime.*;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.lang.Object;
import java.util.*;
import java_cup.runtime.Symbol;
parser code {:
protected Lexer lexer;
private HashMap<String, Integer> symT;
private String filename;
public Parser(String filename) {
this.filename = filename;
}
:};
init with {:
ComplexSymbolFactory f = new ComplexSymbolFactory();
symbolFactory = f;
File file = new File(filename);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (IOException e) {
e.printStackTrace();
}
lexer = new Lexer(f,fis);
symT = new HashMap<String,Integer>();
:};
scan with {: return lexer.yylex(); :};
terminal Double NUMBER;
terminal ADD;
terminal MINUS;
terminal MULT;
terminal DIV;
terminal POW;
terminal O_BRACKET;
terminal C_BRACKET;
non terminal S;
non terminal EXP;
non terminal NUM;
precedence left ADD, MINUS;
precedence left MULT, DIV;
precedence left POW;
S ::= EXP:e {: System.out.println("result: " + e); :}
;
EXP ::= EXP:e1 ADD EXP:e2 {: RESULT = (Double)e1 + (Double)e2; :}
| EXP:e1 MINUS EXP:e2 {: RESULT = (Double)e1 - (Double)e2; :}
| EXP:e1 MULT EXP:e2 {: RESULT = (Double)e1 * (Double)e2; :}
| EXP:e1 DIV EXP:e2 {: RESULT = (Double)e1 / (Double)e2; :}
| EXP:e1 POW EXP:e2 {: RESULT = Math.pow((Double)e1, (Double)e2); :}
| O_BRACKET EXP:e C_BRACKET {: RESULT = e; :}
| NUM:n {: RESULT = n; :}
;
NUM ::= NUMBER:n {: RESULT = n; System.out.println(n); :}
;