This repository has been archived by the owner on Jul 26, 2018. It is now read-only.
forked from whymirror/greg
-
Notifications
You must be signed in to change notification settings - Fork 30
/
greg.g
342 lines (289 loc) · 7.83 KB
/
greg.g
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# LE Grammar for LE Grammars
#
# Copyright (c) 2007 by Ian Piumarta
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the 'Software'),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, provided that the above copyright notice(s) and this
# permission notice appear in all copies of the Software. Acknowledgement
# of the use of this Software in supporting documentation would be
# appreciated but is not required.
#
# THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK.
#
# Last edited: 2007-09-13 08:12:17 by piumarta on emilia.local
%{
# include "greg.h"
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <assert.h>
# if !_MSC_VER
# include <unistd.h>
# include <libgen.h>
# else
# include <io.h>
# include <fcntl.h>
# include "getopt.h"
# endif
typedef struct Header Header;
struct Header {
char *text;
Header *next;
};
FILE *input= 0;
int verboseFlag= 0;
static int lineNumber= 0;
static const char *fileName= 0;
static char *trailer= 0;
static Header *headers= 0;
void makeHeader(char *text);
void makeTrailer(char *text);
void yyerror(struct _GREG *, const char *message);
# define YY_INPUT(buf, result, max, D) \
{ \
int c= getc(input); \
if ('\n' == c || '\r' == c) ++lineNumber; \
result= (EOF == c) ? 0 : (*(buf)= c, 1); \
}
# define YY_LOCAL(T) static T
# define YY_RULE(T) static T
%}
# Hierarchical syntax
grammar= - ( declaration | definition )+ trailer? end-of-file
declaration= '%{' < ( !'%}' . )* > RPERCENT { makeHeader(yytext); } #{YYACCEPT}
trailer= '%%' < .* > { makeTrailer(yytext); } #{YYACCEPT}
definition= s:identifier { if (push(beginRule(findRule(yytext,1)))->rule.expression)
fprintf(stderr, "rule '%s' redefined\n", yytext); }
EQUAL expression { Node *e= pop(); Rule_setExpression(pop(), e); }
SEMICOLON? #{YYACCEPT}
expression= sequence (BAR sequence { Node *f= pop(); push(Alternate_append(pop(), f)); }
)*
sequence= prefix (prefix { Node *f= pop(); push(Sequence_append(pop(), f)); }
)*
prefix= AND action { push(makePredicate(yytext)); }
| AND suffix { push(makePeekFor(pop())); }
| NOT suffix { push(makePeekNot(pop())); }
| suffix
suffix= primary (QUESTION { push(makeQuery(pop())); }
| STAR { push(makeStar (pop())); }
| PLUS { push(makePlus (pop())); }
)?
primary= (
identifier { push(makeVariable(yytext)); }
COLON identifier !EQUAL { Node *name= makeName(findRule(yytext,0)); name->name.variable= pop(); push(name); }
| identifier !EQUAL { push(makeName(findRule(yytext,0))); }
| OPEN expression CLOSE
| literal { push(makeString(yytext)); }
| class { push(makeClass(yytext)); }
| DOT { push(makeDot()); }
| action { push(makeAction(yytext)); }
| BEGIN { push(makePredicate("YY_BEGIN")); }
| END { push(makePredicate("YY_END")); }
) (errblock { Node *node = pop(); ((struct Any *) node)->errblock = strdup(yytext); push(node); })?
# Lexical syntax
identifier= < [-a-zA-Z_][-a-zA-Z_0-9]* > -
literal= ['] < ( !['] char )* > ['] -
| ["] < ( !["] char )* > ["] -
class= '[' < ( !']' range )* > ']' -
range= char '-' char | char
char= '\\' [abefnrtv'"\[\]\\]
| '\\' [0-3][0-7][0-7]
| '\\' [0-7][0-7]?
| !'\\' .
errblock= '~{' < braces* > '}' -
action= '{' < braces* > '}' -
braces= '{' (!'}' .)* '}'
| !'}' .
EQUAL= '=' -
COLON= ':' -
SEMICOLON= ';' -
BAR= '|' -
AND= '&' -
NOT= '!' -
QUESTION= '?' -
STAR= '*' -
PLUS= '+' -
OPEN= '(' -
CLOSE= ')' -
DOT= '.' -
BEGIN= '<' -
END= '>' -
RPERCENT= '%}' -
-= (space | single-line-comment | quoted-comment)*
space= ' ' | '\t' | end-of-line
single-line-comment= ('#' | '//') till-end-of-line
quoted-comment= "/*" (!"*/" .)* "*/"
till-end-of-line= (!end-of-line .)* end-of-line
end-of-line= '\r\n' | '\n' | '\r'
end-of-file= !.
%%
void yyerror(struct _GREG *G, const char *message)
{
int error_line = 1;
while ( (G->pos < G->limit) && (error_line < lineNumber) )
{
switch(G->buf[G->pos++]) {
case '\n':
if(G->buf[G->pos] == '\r') { ++G->pos;}
++error_line;
break;
case '\r':
if(G->buf[G->pos] == '\n') { ++G->pos;}
++error_line;
break;
}
}
fprintf(stderr, "%s:%d:%d %s", fileName, lineNumber, G->limit - G->pos, message);
if (G->text[0]) fprintf(stderr, " near token '%s'", G->text);
if (G->pos < G->limit || !feof(input))
{
G->buf[G->limit]= '\0';
fprintf(stderr, " before text \"");
while (G->pos < G->limit)
{
if ('\n' == G->buf[G->pos] || '\r' == G->buf[G->pos]) break;
fputc(G->buf[G->pos++], stderr);
}
if (G->pos == G->limit)
{
int c;
while (EOF != (c= fgetc(input)) && '\n' != c && '\r' != c)
fputc(c, stderr);
}
fputc('\"', stderr);
}
fprintf(stderr, "\n");
exit(1);
}
void makeHeader(char *text)
{
Header *header= (Header *)malloc(sizeof(Header));
header->text= strdup(text);
header->next= headers;
headers= header;
}
void makeTrailer(char *text)
{
trailer= strdup(text);
}
static void version(char *name)
{
printf("%s version %d.%d.%d\n", name, GREG_MAJOR, GREG_MINOR, GREG_LEVEL);
}
static void usage(char *name)
{
version(name);
fprintf(stderr, "usage: %s [<option>...] [<file>...]\n", name);
fprintf(stderr, "where <option> can be\n");
fprintf(stderr, " -h print this help information\n");
fprintf(stderr, " -o <ofile> write output to <ofile>\n");
fprintf(stderr, " -v be verbose\n");
fprintf(stderr, " -V print version number and exit\n");
fprintf(stderr, "if no <file> is given, input is read from stdin\n");
fprintf(stderr, "if no <ofile> is given, output is written to stdout\n");
exit(1);
}
#if _MSC_VER
static char* basename(const char *str)
{
static char buf[200];
_splitpath(str, NULL, NULL, buf, NULL);
return buf;
}
#endif
int main(int argc, char **argv)
{
GREG *G;
Node *n;
int c;
output= stdout;
#if _MSC_VER
setmode(fileno(output), O_BINARY);
#endif
input= stdin;
lineNumber= 1;
fileName= "<stdin>";
while (-1 != (c= getopt(argc, argv, "Vho:v")))
{
switch (c)
{
case 'V':
version(basename(argv[0]));
exit(0);
case 'h':
usage(basename(argv[0]));
break;
case 'o':
if (!(output= fopen(optarg, "wb")))
{
perror(optarg);
exit(1);
}
break;
case 'v':
verboseFlag++;
break;
default:
fprintf(stderr, "for usage try: %s -h\n", argv[0]);
exit(1);
}
}
argc -= optind;
argv += optind;
G = yyparse_new(NULL);
#ifdef YY_DEBUG
if (verboseFlag > 0) {
G->debug = DEBUG_PARSE;
if (verboseFlag > 1)
G->debug = DEBUG_PARSE + DEBUG_VERBOSE;
}
#endif
if (argc)
{
for (; argc; --argc, ++argv)
{
if (!strcmp(*argv, "-"))
{
input= stdin;
#if _MSC_VER
setmode(fileno(input), O_BINARY);
#endif
fileName= "<stdin>";
}
else
{
if (!(input= fopen(*argv, "rb")))
{
perror(*argv);
exit(1);
}
fileName= *argv;
}
lineNumber= 1;
if (!yyparse(G))
yyerror(G, "syntax error");
if (input != stdin)
fclose(input);
}
}
else
if (!yyparse(G))
yyerror(G, "syntax error");
yyparse_free(G);
if (verboseFlag)
for (n= rules; n; n= n->any.next)
Rule_print(n);
Rule_compile_c_header();
for (; headers; headers= headers->next)
fprintf(output, "%s\n", headers->text);
if (rules)
Rule_compile_c(rules);
if (trailer)
fprintf(output, "%s\n", trailer);
return 0;
}