forked from YYZ/javacc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyser.jj
202 lines (177 loc) · 4.11 KB
/
analyser.jj
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************
***** SECTION 1 - OPTIONS *****
*******************************/
options { JAVA_UNICODE_ESCAPE = true;
IGNORE_CASE = true;
}
/*********************************
***** SECTION 2 - USER CODE *****
*********************************/
PARSER_BEGIN(SLPTokeniser)
public class SLPTokeniser{
public static void main(String args[]){
String temp;
STC temp2;
if (args.length < 1)
{
System.out.println("Please pass in the filename.");
System.exit(1);
}
SLPTokeniser parser = new SLPTokeniser(anew FileInputStream(args[0]));
SimpleNode root = parser.program();
System.out.println("Abstract Syntax Tree:");
root.dump(" ");
}
PARSER_END(SLPTokeniser)
/*****************************************
***** SECTION 3 - TOKEN DEFINITIONS *****
*****************************************/
TOKEN_MGR_DECLS :
{
static int commentNesting = 0;
}
SKIP : /*** Ignoring spaces/tabs/newlines ***/
{
" "
| "\t"
| "\n"
| "\r"
| "\f"
}
SKIP : /* COMMENTS */
{
<"/*"> { commentNesting++; } : IN_COMMENT
| < "//" (~["\n"])*"\n">
}
<IN_COMMENT> SKIP :
{
"/*" { commentNesting++; }
| "*/" { commentNesting--;
if (commentNesting == 0)
SwitchTo(DEFAULT);
}
| <~[]>
}
TOKEN : /* Keywords and punctuation */
{
< SEMIC : ";" >
| < COLON : ":" >
| < ASSIGN : ":=" >
| < LBR : "(" >
| < RBR : ")" >
| < COMMA : "," >
| < PLUS_SIGN : "+" >
| < MINUS_SIGN : "-" >
| < MULT_SIGN : "*" >
| < DIV_SIGN : "/" >
| < RCBR : "}" >
| < LCBR : "{" >
| < EXLM : "!" >
| < QUESTION : "?" >
| < PERCENT : "%" >
| < EQUALS : "=" >
| < NOT_EQUAL : "!=" >
| < LEFT_BRACE : "<" >
| < RIGHT_BRACE : ">" >
| < LEFT_BRACE_EQUAL : "<=" >
| < RIGHT_BRACE_EQUAL : ">=" >
| < DOT : "." >
| < AND : "and" >
| < BOOL : "boolean" >
| < CONST : "constant" >
| < DO : "do" >
| < ELSE : "else" >
| < FALSE : "false" >
| < IF : "if" >
| < INT : "integer" >
| < MAIN : "main" >
| < NOT : "not" >
| < OR : "or" >
| < REAL : "real" >
| < RETURN : "return" >
| < STR : "string" >
| < THEN : "then" >
| < TRUE : "true" >
| < VAR : "var" >
| < VOID : "void" >
| < WHILE : "while" >
}
//Starts with a single-double quote, then anything on the keyboard
//Doesn't accept "" or \n, unless it finds a backslash, in which case they're allowed
TOKEN : /* Strings */
{
< STRING : "\"" ((~[ "\"" , "\n", "\\"] )* | (("\\" ("\"" | "\n" | "\\" | "'\n\'"))))* "\"">
}
TOKEN : /* Numbers and identifiers */
{
< NUM : (<DIGIT>)+ | (<DIGIT>) + "." (<DIGIT>) + >
| < #DIGIT : ["0" - "9"] >
| < ID : (<LETTER>)+ (<LETTER> | "_" | <DIGIT>) * >
| < #LETTER : ["a" - "z", "A" - "Z"] >
}
TOKEN : /* Anything not recognised so far */
{
< OTHER : ~[] >
}
//Grammar starts here
void program() : {}{
(decl())*(function())*main_prog()
}
void decl() : {}{
(var_decl() | const_decl())
}
void var_decl () : {}{
<VAR>ident_list()<COLON>type()(<COMMA>ident_list()<COLON>type())*<SEMIC>
}
void const_decl() : {}{
<CONST><ID><COLON>type()<EQUALS>expression()(<COMMA><ID><COLON>type()<EQUALS>expression())*<SEMIC>
}
void function () :{} {
type()<ID><LBR>param_list()<RBR><LCBR>
(decl())*(statement()<SEMIC>)*<RETURN><LBR>expression() | {}<RBR><SEMIC>
<RCBR>
}
void param_list () :{} {
(<ID><COLON>type()(<COMMA><ID><COLON>type())* | {})
}
void type () :{}{
<INT> | <BOOL> | <REAL> | <STR> | <VOID>
}
void main_prog() :{} {
<MAIN>
<LCBR>
(decl())*(statement()<SEMIC>)*
<RCBR>
}
void statement () :{}{
<ID><ASSIGN>expression()
|<ID><ASSIGN><STR>
|<EXLM>expression()
|<QUESTION><ID>
|<ID><LBR>arg_list()<RBR>
|<LCBR>(statement()<SEMIC>)*<RCBR>
|<IF>condition()<THEN>statement()
|<IF>condition()<THEN>statement()<ELSE>statement()
|<WHILE>condition()<DO>statement()
|{}
}
void expression(): {}{
fragment()((<PLUS_SIGN>|<MINUS_SIGN>|<MULT_SIGN>|<DIV_SIGN>)fragment())*
}
void fragment() :{}{
<ID>
|<NUM>
|(<PLUS_SIGN>|<MINUS_SIGN>)fragment()
|expression()
}
void condition() :{}{
<NOT>expression()
|expression()(<EQUALS>|<NOT_EQUAL>|<LEFT_BRACE>|<RIGHT_BRACE>|<LEFT_BRACE_EQUAL>|<RIGHT_BRACE_EQUAL>|<AND>|<OR>)
|<ID>
}
void ident_list () :{}{
<ID>(<COMMA><ID>)*
}
void arg_list():{}{
(<ID>(<COMMA><ID>)* | {})
}