-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathJavaCCGrammar.jj
executable file
·149 lines (128 loc) · 1.88 KB
/
JavaCCGrammar.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
// javacc编译设置
options {
STATIC = false;
IGNORE_CASE = true;
UNICODE_INPUT = true;
}
// 解析类
PARSER_BEGIN(JavaCCGrammar)
package com.github.quxiucheng.parser.javacc.grammar;
public class JavaCCGrammar{
public static void main(String[] args) throws Exception {
JavaCCGrammar grammar = new JavaCCGrammar(System.in);
grammar.function1();
}
}
PARSER_END(JavaCCGrammar)
// 忽律的字符
SKIP:{
" "
| "\t"
| "\n"
| "\r"
| "\r\n"
}
// token关键字
TOKEN:{
<SELECT :"select">
| <UPDATE :"update">
| <DELETE :"delete">
| <COMMA:",">
| <FORM:"form">
| <AS:"as">
| <WHERE:"where">
| <EQUAL:"=">
| <OPEN_PAR: "(">
| <CLOSE_PAR: ")">
}
// 定义java代码块
void javaCodeDemo():
{}
{
{
int i = 0;
System.out.println(i);
}
}
// 函数
void function1():
{
// 定义局部变量
int i=0;
}
{
{return;}
}
// 返回值
double function2():
{
int i=0;
}
{
// 带返回值
{return i;}
}
// 函数调用
double function3():
{
double i;
}
{
{i = function2();}
{return i;}
}
// token复制
void tokenAssignment() :
{
Token st;
Token dt;
}
{
st = <SELECT>
dt = <DELETE>
}
// if语句
void ifExpr():
{}
{
[
<SELECT>
{
System.out.println("if select");
}
]
// 循环,出现一次
(<SELECT>)?
}
// if - else
void ifElseExpr():
{}
{
(
<SELECT> {System.out.println("if else select");}
|
<UPDATE> {System.out.println("if else update");}
|
<DELETE> {System.out.println("if else delete");}
|
{
System.out.println("other");
}
)
}
// while 0~n
void while1Expr():{
}
{
(<SELECT>)*
}
// while 1~n
void while2Expr():{
}
{
(<SELECT>)+
}
// Java 代码
JAVACODE void print(Token t){
System.out.println(t);
}