This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
/
demo.y
325 lines (288 loc) · 4.2 KB
/
demo.y
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
%{
//TODO Put your favorite license here
// yacc source generated by ebnf2y[1]
// at 2013-10-04 19:00:20.671014862 +0200 CEST
//
// $ ebnf2y -start Expression -o demo.y demo.ebnf
//
// CAUTION: If this file is a Go source file (*.go), it was generated
// automatically by '$ go tool yacc' from a *.y file - DO NOT EDIT in that case!
//
// [1]: http://github.com/cznic/ebnf2y
package main //TODO real package name
//TODO required only be the demo _dump function
import (
"bytes"
"fmt"
"strings"
"github.com/cznic/strutil"
)
%}
%union {
item interface{} //TODO insert real field(s)
}
%token ANDNOT
%token BOOLEAN
%token FLOAT
%token IDENTIFIER
%token IMAGINARY
%token INTEGER
%token LSH
%token RSH
%token STR
%type <item> /*TODO real type(s), if/where applicable */
ANDNOT
BOOLEAN
FLOAT
IDENTIFIER
IMAGINARY
INTEGER
LSH
RSH
STR
%type <item> /*TODO real type(s), if/where applicable */
Expression
Expression1
Expression11
ExpressionList
ExpressionList1
Factor
Factor1
Factor11
Literal
Operand
Operand1
QualifiedIdent
QualifiedIdent1
Start
Term
Term1
Term11
/*TODO %left, %right, ... declarations */
%start Start
%%
Expression:
Term Expression1
{
$$ = []Expression{$1, $2} //TODO 1
}
Expression1:
/* EMPTY */
{
$$ = []Expression1(nil) //TODO 2
}
| Expression1 Expression11 Term
{
$$ = append($1.([]Expression1), $2, $3) //TODO 3
}
Expression11:
'^'
{
$$ = "^" //TODO 4
}
| '|'
{
$$ = "|" //TODO 5
}
| '-'
{
$$ = "-" //TODO 6
}
| '+'
{
$$ = "+" //TODO 7
}
ExpressionList:
Expression ExpressionList1
{
$$ = []ExpressionList{$1, $2} //TODO 8
}
ExpressionList1:
/* EMPTY */
{
$$ = []ExpressionList1(nil) //TODO 9
}
| ExpressionList1 ',' Expression
{
$$ = append($1.([]ExpressionList1), ",", $3) //TODO 10
}
Factor:
Factor1 Operand
{
$$ = []Factor{$1, $2} //TODO 11
}
Factor1:
/* EMPTY */
{
$$ = nil //TODO 12
}
| Factor11
{
$$ = $1 //TODO 13
}
Factor11:
'^'
{
$$ = "^" //TODO 14
}
| '!'
{
$$ = "!" //TODO 15
}
| '-'
{
$$ = "-" //TODO 16
}
| '+'
{
$$ = "+" //TODO 17
}
Literal:
BOOLEAN
{
$$ = $1 //TODO 18
}
| FLOAT
{
$$ = $1 //TODO 19
}
| QualifiedIdent
{
$$ = $1 //TODO 20
}
| IMAGINARY
{
$$ = $1 //TODO 21
}
| INTEGER
{
$$ = $1 //TODO 22
}
| STR
{
$$ = $1 //TODO 23
}
Operand:
Literal
{
$$ = $1 //TODO 24
}
| QualifiedIdent '(' Operand1 ')'
{
$$ = []Operand{$1, "(", $3, ")"} //TODO 25
}
| '(' Expression ')'
{
$$ = []Operand{"(", $2, ")"} //TODO 26
}
Operand1:
/* EMPTY */
{
$$ = nil //TODO 27
}
| ExpressionList
{
$$ = $1 //TODO 28
}
QualifiedIdent:
IDENTIFIER QualifiedIdent1
{
$$ = []QualifiedIdent{$1, $2} //TODO 29
}
QualifiedIdent1:
/* EMPTY */
{
$$ = nil //TODO 30
}
| '.' IDENTIFIER
{
$$ = []QualifiedIdent1{".", $2} //TODO 31
}
Start:
Expression
{
_parserResult = $1 //TODO 32
}
Term:
Factor Term1
{
$$ = []Term{$1, $2} //TODO 33
}
Term1:
/* EMPTY */
{
$$ = []Term1(nil) //TODO 34
}
| Term1 Term11 Factor
{
$$ = append($1.([]Term1), $2, $3) //TODO 35
}
Term11:
ANDNOT
{
$$ = $1 //TODO 36
}
| '&'
{
$$ = "&" //TODO 37
}
| LSH
{
$$ = $1 //TODO 38
}
| RSH
{
$$ = $1 //TODO 39
}
| '%'
{
$$ = "%" //TODO 40
}
| '/'
{
$$ = "/" //TODO 41
}
| '*'
{
$$ = "*" //TODO 42
}
%%
//TODO remove demo stuff below
var _parserResult interface{}
type (
Expression interface{}
Expression1 interface{}
Expression11 interface{}
ExpressionList interface{}
ExpressionList1 interface{}
Factor interface{}
Factor1 interface{}
Factor11 interface{}
Literal interface{}
Operand interface{}
Operand1 interface{}
QualifiedIdent interface{}
QualifiedIdent1 interface{}
Start interface{}
Term interface{}
Term1 interface{}
Term11 interface{}
)
func _dump() {
s := fmt.Sprintf("%#v", _parserResult)
s = strings.Replace(s, "%", "%%", -1)
s = strings.Replace(s, "{", "{%i\n", -1)
s = strings.Replace(s, "}", "%u\n}", -1)
s = strings.Replace(s, ", ", ",\n", -1)
var buf bytes.Buffer
strutil.IndentFormatter(&buf, ". ").Format(s)
buf.WriteString("\n")
a := strings.Split(buf.String(), "\n")
for _, v := range a {
if strings.HasSuffix(v, "(nil)") || strings.HasSuffix(v, "(nil),") {
continue
}
fmt.Println(v)
}
}
// End of demo stuff