-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrammar.rb
188 lines (130 loc) · 5.78 KB
/
grammar.rb
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
case @@type
when :default
@@grammar.add_rule(:main, [:expr ], [:reducible]);
@@grammar.add_rule(:expr, [:expr, :p], [:reducible]);
@@grammar.add_rule(:expr, [:p ], [:reducible]);
t1 = [:expr, TokenTemplate.new(['=']), :expr]
t2 = [:expr, TokenTemplate.new([',']), :expr]
@@grammar.add_rule(:expr, t1)
@@grammar.add_rule(:expr, t2)
@@grammar.add_rule(:p, [:t], [:reducible])
b_open_1 = TokenTemplate.new(['(']);
b_close_1 = TokenTemplate.new([')']);
b_open_2 = TokenTemplate.new(['[']);
b_close_2 = TokenTemplate.new([']']);
b_open_3 = TokenTemplate.new(['{']);
b_close_3 = TokenTemplate.new(['}']);
bracket1 = [b_open_1, :expr, b_close_1]
bracket2 = [b_open_2, :expr, b_close_2]
bracket3 = [b_open_3, :expr, b_close_3]
bracket4 = [b_open_1 , b_close_1]
bracket5 = [b_open_2 , b_close_2]
bracket6 = [b_open_3 , b_close_3]
@@grammar.add_rule(:t, bracket1);
@@grammar.add_rule(:t, bracket2);
@@grammar.add_rule(:t, bracket3);
@@grammar.add_rule(:t, bracket4);
@@grammar.add_rule(:t, bracket5);
@@grammar.add_rule(:t, bracket6);
@@grammar.add_rule(:t, [TokenTemplate.new(:quote)], [:reducible]);
@@grammar.add_rule(:t, [TokenTemplate.new(:id )], [:reducible]);
when :C99
@@grammar.add_rule(:main, [:expr ], [:reducible]);
@@grammar.add_rule(:expr, [:expr, :p], [:reducible]);
@@grammar.add_rule(:expr, [:p ], [:reducible]);
t0 = [:type_def, :expr]
t1 = [:expr, TokenTemplate.new(['=']), :expr]
t2 = [:expr, TokenTemplate.new([',']), :expr]
t3 = [:expr, TokenTemplate.new([';']), :expr]
@@grammar.add_rule(:expr, t0, [:reducible])
@@grammar.add_rule(:expr, t1)
@@grammar.add_rule(:expr, t2)
@@grammar.add_rule(:expr, t3)
@@grammar.add_rule(:p, [:t], [:reducible])
@@grammar.add_rule(:type_def, [:td]);
@@grammar.add_rule(:td, [:type_spec, :td] , [:reducible])
@@grammar.add_rule(:td, [:type_spec, :true_type], [:reducible])
sp_words = ['auto', 'register', 'static', 'long', 'extern', 'typedef', 'const', 'volatile', 'struct'];
spec = [TokenTemplate.new(sp_words)];
@@grammar.add_rule(:type_spec, spec, [:reducible]);
ttype1 = [TokenTemplate.new(["*"])]
ttype2 = [TokenTemplate.new(["&"])]
ttype3 = [TokenTemplate.new(:ptr)]
ttype4 = [:id]
@@grammar.add_rule(:true_type, ttype1, [:reducible]);
@@grammar.add_rule(:true_type, ttype2, [:reducible]);
@@grammar.add_rule(:true_type, ttype3, [:reducible]);
@@grammar.add_rule(:true_type, ttype4, [:reducible]);
idtmp = [TokenTemplate.new(:id, sp_words)]
@@grammar.add_rule(:id, idtmp, [:reducible]);
b_open_1 = TokenTemplate.new(['(']);
b_close_1 = TokenTemplate.new([')']);
b_open_2 = TokenTemplate.new(['[']);
b_close_2 = TokenTemplate.new([']']);
b_open_3 = TokenTemplate.new(['{']);
b_close_3 = TokenTemplate.new(['}']);
bracket1 = [b_open_1, :expr, b_close_1]
bracket2 = [b_open_2, :expr, b_close_2]
bracket3 = [b_open_3, :expr, b_close_3]
bracket4 = [b_open_1 , b_close_1]
bracket5 = [b_open_2 , b_close_2]
bracket6 = [b_open_3 , b_close_3]
@@grammar.add_rule(:t, bracket1);
@@grammar.add_rule(:t, bracket2);
@@grammar.add_rule(:t, bracket3);
@@grammar.add_rule(:t, bracket4);
@@grammar.add_rule(:t, bracket5);
@@grammar.add_rule(:t, bracket6);
@@grammar.add_rule(:t, [:id ] , [:reducible]);
@@grammar.add_rule(:t, [TokenTemplate.new(:quote )] , [:reducible]);
@@grammar.add_rule(:t, [TokenTemplate.new(:comment)] , [:reducible]);
@@grammar.add_rule(:t, [TokenTemplate.new(:float )] , [:reducible]);
@@grammar.add_rule(:t, [TokenTemplate.new(:spchar , ['[', '{', '(', ']', '}', ')', '=', ',', ';'])] , [:reducible]);
when :java
@@grammar.add_rule(:main, [:expr ], [:reducible]);
@@grammar.add_rule(:expr, [:expr, :p], [:reducible]);
@@grammar.add_rule(:expr, [:p ], [:reducible]);
t0 = [:type_def, :expr]
t1 = [:expr, TokenTemplate.new(['=']), :expr]
t2 = [:expr, TokenTemplate.new([',']), :expr]
t3 = [:expr, TokenTemplate.new([';']), :expr]
@@grammar.add_rule(:expr, t0, [:reducible])
@@grammar.add_rule(:expr, t1)
@@grammar.add_rule(:expr, t2)
@@grammar.add_rule(:expr, t3)
@@grammar.add_rule(:p, [:t], [:reducible])
@@grammar.add_rule(:type_def, [:td]);
@@grammar.add_rule(:td, [:type_spec, :td] , [:reducible])
@@grammar.add_rule(:td, [:type_spec, :true_type], [:reducible])
sp_words = ["public", "protected", "private", "static",
"final", "transient", "volatile", "abstract", "synchronized", "native"];
spec = [TokenTemplate.new(sp_words)];
@@grammar.add_rule(:type_spec, spec, [:reducible]);
ttype4 = [:id]
@@grammar.add_rule(:true_type, ttype4, [:reducible]);
idtmp = [TokenTemplate.new(:id, sp_words)]
@@grammar.add_rule(:id, idtmp, [:reducible]);
b_open_1 = TokenTemplate.new(['(']);
b_close_1 = TokenTemplate.new([')']);
b_open_2 = TokenTemplate.new(['[']);
b_close_2 = TokenTemplate.new([']']);
b_open_3 = TokenTemplate.new(['{']);
b_close_3 = TokenTemplate.new(['}']);
bracket1 = [b_open_1, :expr, b_close_1]
bracket2 = [b_open_2, :expr, b_close_2]
bracket3 = [b_open_3, :expr, b_close_3]
bracket4 = [b_open_1 , b_close_1]
bracket5 = [b_open_2 , b_close_2]
bracket6 = [b_open_3 , b_close_3]
@@grammar.add_rule(:t, bracket1);
@@grammar.add_rule(:t, bracket2);
@@grammar.add_rule(:t, bracket3);
@@grammar.add_rule(:t, bracket4);
@@grammar.add_rule(:t, bracket5);
@@grammar.add_rule(:t, bracket6);
@@grammar.add_rule(:t, [:id ] , [:reducible]);
@@grammar.add_rule(:t, [TokenTemplate.new(:quote )] , [:reducible]);
@@grammar.add_rule(:t, [TokenTemplate.new(:comment)] , [:reducible]);
@@grammar.add_rule(:t, [TokenTemplate.new(:float )] , [:reducible]);
@@grammar.add_rule(:t, [TokenTemplate.new(:spchar , ['[', '{', '(', ']', '}', ')', '=', ',', ';'])] , [:reducible]);
end