-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExprLang.jjt
More file actions
executable file
·166 lines (135 loc) · 3.1 KB
/
ExprLang.jjt
File metadata and controls
executable file
·166 lines (135 loc) · 3.1 KB
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
156
157
158
159
160
161
162
163
164
165
166
// Name:ExprLang.jjt
// Definition of the ExprLang language, including abstract syntax tree generation
//
options
{
IGNORE_CASE = false;
MULTI=true;
VISITOR = true;
}
PARSER_BEGIN(ExprLang)
import java.io.*;
import java.util.*;
public class ExprLang
{
public static Hashtable ST = new Hashtable();
public static void main(String[] args) throws ParseException, FileNotFoundException
{
String temp;
STC temp2;
if (args.length < 1)
{
System.out.println("Please pass in the filename.");
System.exit(1);
}
ExprLang parser = new ExprLang(new FileInputStream(args[0]));
SimpleNode root = parser.program();
System.out.println("Abstract Syntax Tree:");
root.dump(" ");
System.out.println();
System.out.println("Symbol Table:");
Enumeration t = ST.keys();
while (t.hasMoreElements())
{
temp = (String)t.nextElement();
temp2 = (STC)ST.get(temp);
System.out.println(temp);
if (temp2.type != null)
{
System.out.println(" type = " + temp2.type);
}
if (temp2.value != null)
{
System.out.println(" value = " + temp2.value);
}
}
System.out.println();
System.out.println("Program:");
PrintVisitor pv = new PrintVisitor();
root.jjtAccept(pv, null);
System.out.println();
System.out.println("Type Checking:");
TypeCheckVisitor tc = new TypeCheckVisitor();
root.jjtAccept(tc, ST);
}
}
PARSER_END(ExprLang)
TOKEN_MGR_DECLS:
{
static int linenumber = 0;
}
SKIP: /* Whitespace */
{
"\t"
| "\n" {linenumber++;}
| "\r"
| " "
}
TOKEN:
{
<LPAREN: "(">
| <RPAREN: ")">
| <ADD_OP: "+" | "-">
| <MULT_OP: "*" | "/">
| <NOT_OP: "~">
| <BOOL_OP: "&" | "|">
| <INT: "int">
| <BOOL: "bool">
| <NUMBER: (["0"-"9"])+>
| <ID: (["a"-"z","A"-"Z"])+>
| <SEMIC: ";">
}
SimpleNode program() : {}
{
Stms() <EOF> {return jjtThis;}
}
void Stms() #void : {}
{
(SimpleStm() <SEMIC> [Stms() #Stms(2)] )
}
void SimpleStm() #void : {}
{
(expression())
| (declaration())
}
void declaration() #void : {Token t; String name;}
{
t = <INT> name = identifier() {jjtThis.value = t.image;
ST.put(name, new STC("Int", name));} #Decl(1)
| t = <BOOL> name = identifier() {jjtThis.value = t.image;
ST.put(name, new STC("Bool", name));} #Decl(1)
}
void expression() #void : {Token t;}
{
term()
(
(t = <ADD_OP> term() {jjtThis.value = t.image;} #Add_op(2)
)
| (t = <BOOL_OP> term() {jjtThis.value = t.image;} #Bool_op(2)
)
)*
}
void term() #void : {Token t;}
{
<NOT_OP> factor() #Not_op(1)
|
factor()
(t = <MULT_OP> factor() {jjtThis.value = t.image;} #Mult_op(2)
)*
}
void factor() #void : {}
{
(<LPAREN> expression() #Exp(1) <RPAREN>
| number()
| identifier()
)
}
void number() : {Token t;}
{
t = <NUMBER> {jjtThis.value = t.image;
}
}
String identifier() : {Token t;}
{
t = <ID> {jjtThis.value = t.image; return t.image;}
}