aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-01-09 08:35:38 +1000
committerDavid Walter Seikel2012-01-09 08:35:38 +1000
commita94fa72317334d05501923cfae51d555fef43583 (patch)
tree6409bb0902eb63c39483dd95940a737484733d65 /LuaSL
parentOne of these days I'll get spaces to work. Another hack at it. (diff)
downloadSledjHamr-a94fa72317334d05501923cfae51d555fef43583.zip
SledjHamr-a94fa72317334d05501923cfae51d555fef43583.tar.gz
SledjHamr-a94fa72317334d05501923cfae51d555fef43583.tar.bz2
SledjHamr-a94fa72317334d05501923cfae51d555fef43583.tar.xz
Added ANSI C flex and yacc files, just for reference.
Diffstat (limited to 'LuaSL')
-rw-r--r--LuaSL/src/ANSI_C.l182
-rw-r--r--LuaSL/src/ANSI_C.y471
2 files changed, 653 insertions, 0 deletions
diff --git a/LuaSL/src/ANSI_C.l b/LuaSL/src/ANSI_C.l
new file mode 100644
index 0000000..8a6b435
--- /dev/null
+++ b/LuaSL/src/ANSI_C.l
@@ -0,0 +1,182 @@
1D [0-9]
2L [a-zA-Z_]
3H [a-fA-F0-9]
4E [Ee][+-]?{D}+
5P [Pp][+-]?{D}+
6FS (f|F|l|L)
7IS ((u|U)|(u|U)?(l|L|ll|LL)|(l|L|ll|LL)(u|U))
8
9%{
10#include <stdio.h>
11#include "y.tab.h"
12
13void count(void);
14%}
15
16%%
17"/*" { comment(); }
18"//"[^\n]* { /* consume //-comment */ }
19
20
21"auto" { count(); return(AUTO); }
22"_Bool" { count(); return(BOOL); }
23"break" { count(); return(BREAK); }
24"case" { count(); return(CASE); }
25"char" { count(); return(CHAR); }
26"_Complex" { count(); return(COMPLEX); }
27"const" { count(); return(CONST); }
28"continue" { count(); return(CONTINUE); }
29"default" { count(); return(DEFAULT); }
30"do" { count(); return(DO); }
31"double" { count(); return(DOUBLE); }
32"else" { count(); return(ELSE); }
33"enum" { count(); return(ENUM); }
34"extern" { count(); return(EXTERN); }
35"float" { count(); return(FLOAT); }
36"for" { count(); return(FOR); }
37"goto" { count(); return(GOTO); }
38"if" { count(); return(IF); }
39"_Imaginary" { count(); return(IMAGINARY); }
40"inline" { count(); return(INLINE); }
41"int" { count(); return(INT); }
42"long" { count(); return(LONG); }
43"register" { count(); return(REGISTER); }
44"restrict" { count(); return(RESTRICT); }
45"return" { count(); return(RETURN); }
46"short" { count(); return(SHORT); }
47"signed" { count(); return(SIGNED); }
48"sizeof" { count(); return(SIZEOF); }
49"static" { count(); return(STATIC); }
50"struct" { count(); return(STRUCT); }
51"switch" { count(); return(SWITCH); }
52"typedef" { count(); return(TYPEDEF); }
53"union" { count(); return(UNION); }
54"unsigned" { count(); return(UNSIGNED); }
55"void" { count(); return(VOID); }
56"volatile" { count(); return(VOLATILE); }
57"while" { count(); return(WHILE); }
58
59{L}({L}|{D})* { count(); return(check_type()); }
60
610[xX]{H}+{IS}? { count(); return(CONSTANT); }
620{D}+{IS}? { count(); return(CONSTANT); }
63{D}+{IS}? { count(); return(CONSTANT); }
64L?'(\\.|[^\\'\n])+' { count(); return(CONSTANT); }
65
66{D}+{E}{FS}? { count(); return(CONSTANT); }
67{D}*"."{D}+({E})?{FS}? { count(); return(CONSTANT); }
68{D}+"."{D}*({E})?{FS}? { count(); return(CONSTANT); }
690[xX]{H}+{P}{FS}? { count(); return(CONSTANT); }
700[xX]{H}*"."{H}+({P})?{FS}? { count(); return(CONSTANT); }
710[xX]{H}+"."{H}*({P})?{FS}? { count(); return(CONSTANT); }
72
73
74L?\"(\\.|[^\\"\n])*\" { count(); return(STRING_LITERAL); }
75
76"..." { count(); return(ELLIPSIS); }
77">>=" { count(); return(RIGHT_ASSIGN); }
78"<<=" { count(); return(LEFT_ASSIGN); }
79"+=" { count(); return(ADD_ASSIGN); }
80"-=" { count(); return(SUB_ASSIGN); }
81"*=" { count(); return(MUL_ASSIGN); }
82"/=" { count(); return(DIV_ASSIGN); }
83"%=" { count(); return(MOD_ASSIGN); }
84"&=" { count(); return(AND_ASSIGN); }
85"^=" { count(); return(XOR_ASSIGN); }
86"|=" { count(); return(OR_ASSIGN); }
87">>" { count(); return(RIGHT_OP); }
88"<<" { count(); return(LEFT_OP); }
89"++" { count(); return(INC_OP); }
90"--" { count(); return(DEC_OP); }
91"->" { count(); return(PTR_OP); }
92"&&" { count(); return(AND_OP); }
93"||" { count(); return(OR_OP); }
94"<=" { count(); return(LE_OP); }
95">=" { count(); return(GE_OP); }
96"==" { count(); return(EQ_OP); }
97"!=" { count(); return(NE_OP); }
98";" { count(); return(';'); }
99("{"|"<%") { count(); return('{'); }
100("}"|"%>") { count(); return('}'); }
101"," { count(); return(','); }
102":" { count(); return(':'); }
103"=" { count(); return('='); }
104"(" { count(); return('('); }
105")" { count(); return(')'); }
106("["|"<:") { count(); return('['); }
107("]"|":>") { count(); return(']'); }
108"." { count(); return('.'); }
109"&" { count(); return('&'); }
110"!" { count(); return('!'); }
111"~" { count(); return('~'); }
112"-" { count(); return('-'); }
113"+" { count(); return('+'); }
114"*" { count(); return('*'); }
115"/" { count(); return('/'); }
116"%" { count(); return('%'); }
117"<" { count(); return('<'); }
118">" { count(); return('>'); }
119"^" { count(); return('^'); }
120"|" { count(); return('|'); }
121"?" { count(); return('?'); }
122
123[ \t\v\n\f] { count(); }
124. { /* Add code to complain about unmatched characters */ }
125
126%%
127
128int yywrap(void)
129{
130 return 1;
131}
132
133
134void comment(void)
135{
136 char c, prev = 0;
137
138 while ((c = input()) != 0) /* (EOF maps to 0) */
139 {
140 if (c == '/' && prev == '*')
141 return;
142 prev = c;
143 }
144 error("unterminated comment");
145}
146
147
148int column = 0;
149
150void count(void)
151{
152 int i;
153
154 for (i = 0; yytext[i] != '\0'; i++)
155 if (yytext[i] == '\n')
156 column = 0;
157 else if (yytext[i] == '\t')
158 column += 8 - (column % 8);
159 else
160 column++;
161
162 ECHO;
163}
164
165
166int check_type(void)
167{
168/*
169* pseudo code --- this is what it should check
170*
171* if (yytext == type_name)
172* return TYPE_NAME;
173*
174* return IDENTIFIER;
175*/
176
177/*
178* it actually will only return IDENTIFIER
179*/
180
181 return IDENTIFIER;
182}
diff --git a/LuaSL/src/ANSI_C.y b/LuaSL/src/ANSI_C.y
new file mode 100644
index 0000000..28e2ca2
--- /dev/null
+++ b/LuaSL/src/ANSI_C.y
@@ -0,0 +1,471 @@
1%token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF
2%token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
3%token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
4%token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
5%token XOR_ASSIGN OR_ASSIGN TYPE_NAME
6
7%token TYPEDEF EXTERN STATIC AUTO REGISTER INLINE RESTRICT
8%token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
9%token BOOL COMPLEX IMAGINARY
10%token STRUCT UNION ENUM ELLIPSIS
11
12%token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
13
14%start translation_unit
15%%
16
17primary_expression
18 : IDENTIFIER
19 | CONSTANT
20 | STRING_LITERAL
21 | '(' expression ')'
22 ;
23
24postfix_expression
25 : primary_expression
26 | postfix_expression '[' expression ']'
27 | postfix_expression '(' ')'
28 | postfix_expression '(' argument_expression_list ')'
29 | postfix_expression '.' IDENTIFIER
30 | postfix_expression PTR_OP IDENTIFIER
31 | postfix_expression INC_OP
32 | postfix_expression DEC_OP
33 | '(' type_name ')' '{' initializer_list '}'
34 | '(' type_name ')' '{' initializer_list ',' '}'
35 ;
36
37argument_expression_list
38 : assignment_expression
39 | argument_expression_list ',' assignment_expression
40 ;
41
42unary_expression
43 : postfix_expression
44 | INC_OP unary_expression
45 | DEC_OP unary_expression
46 | unary_operator cast_expression
47 | SIZEOF unary_expression
48 | SIZEOF '(' type_name ')'
49 ;
50
51unary_operator
52 : '&'
53 | '*'
54 | '+'
55 | '-'
56 | '~'
57 | '!'
58 ;
59
60cast_expression
61 : unary_expression
62 | '(' type_name ')' cast_expression
63 ;
64
65multiplicative_expression
66 : cast_expression
67 | multiplicative_expression '*' cast_expression
68 | multiplicative_expression '/' cast_expression
69 | multiplicative_expression '%' cast_expression
70 ;
71
72additive_expression
73 : multiplicative_expression
74 | additive_expression '+' multiplicative_expression
75 | additive_expression '-' multiplicative_expression
76 ;
77
78shift_expression
79 : additive_expression
80 | shift_expression LEFT_OP additive_expression
81 | shift_expression RIGHT_OP additive_expression
82 ;
83
84relational_expression
85 : shift_expression
86 | relational_expression '<' shift_expression
87 | relational_expression '>' shift_expression
88 | relational_expression LE_OP shift_expression
89 | relational_expression GE_OP shift_expression
90 ;
91
92equality_expression
93 : relational_expression
94 | equality_expression EQ_OP relational_expression
95 | equality_expression NE_OP relational_expression
96 ;
97
98and_expression
99 : equality_expression
100 | and_expression '&' equality_expression
101 ;
102
103exclusive_or_expression
104 : and_expression
105 | exclusive_or_expression '^' and_expression
106 ;
107
108inclusive_or_expression
109 : exclusive_or_expression
110 | inclusive_or_expression '|' exclusive_or_expression
111 ;
112
113logical_and_expression
114 : inclusive_or_expression
115 | logical_and_expression AND_OP inclusive_or_expression
116 ;
117
118logical_or_expression
119 : logical_and_expression
120 | logical_or_expression OR_OP logical_and_expression
121 ;
122
123conditional_expression
124 : logical_or_expression
125 | logical_or_expression '?' expression ':' conditional_expression
126 ;
127
128assignment_expression
129 : conditional_expression
130 | unary_expression assignment_operator assignment_expression
131 ;
132
133assignment_operator
134 : '='
135 | MUL_ASSIGN
136 | DIV_ASSIGN
137 | MOD_ASSIGN
138 | ADD_ASSIGN
139 | SUB_ASSIGN
140 | LEFT_ASSIGN
141 | RIGHT_ASSIGN
142 | AND_ASSIGN
143 | XOR_ASSIGN
144 | OR_ASSIGN
145 ;
146
147expression
148 : assignment_expression
149 | expression ',' assignment_expression
150 ;
151
152constant_expression
153 : conditional_expression
154 ;
155
156declaration
157 : declaration_specifiers ';'
158 | declaration_specifiers init_declarator_list ';'
159 ;
160
161declaration_specifiers
162 : storage_class_specifier
163 | storage_class_specifier declaration_specifiers
164 | type_specifier
165 | type_specifier declaration_specifiers
166 | type_qualifier
167 | type_qualifier declaration_specifiers
168 | function_specifier
169 | function_specifier declaration_specifiers
170 ;
171
172init_declarator_list
173 : init_declarator
174 | init_declarator_list ',' init_declarator
175 ;
176
177init_declarator
178 : declarator
179 | declarator '=' initializer
180 ;
181
182storage_class_specifier
183 : TYPEDEF
184 | EXTERN
185 | STATIC
186 | AUTO
187 | REGISTER
188 ;
189
190type_specifier
191 : VOID
192 | CHAR
193 | SHORT
194 | INT
195 | LONG
196 | FLOAT
197 | DOUBLE
198 | SIGNED
199 | UNSIGNED
200 | BOOL
201 | COMPLEX
202 | IMAGINARY
203 | struct_or_union_specifier
204 | enum_specifier
205 | TYPE_NAME
206 ;
207
208struct_or_union_specifier
209 : struct_or_union IDENTIFIER '{' struct_declaration_list '}'
210 | struct_or_union '{' struct_declaration_list '}'
211 | struct_or_union IDENTIFIER
212 ;
213
214struct_or_union
215 : STRUCT
216 | UNION
217 ;
218
219struct_declaration_list
220 : struct_declaration
221 | struct_declaration_list struct_declaration
222 ;
223
224struct_declaration
225 : specifier_qualifier_list struct_declarator_list ';'
226 ;
227
228specifier_qualifier_list
229 : type_specifier specifier_qualifier_list
230 | type_specifier
231 | type_qualifier specifier_qualifier_list
232 | type_qualifier
233 ;
234
235struct_declarator_list
236 : struct_declarator
237 | struct_declarator_list ',' struct_declarator
238 ;
239
240struct_declarator
241 : declarator
242 | ':' constant_expression
243 | declarator ':' constant_expression
244 ;
245
246enum_specifier
247 : ENUM '{' enumerator_list '}'
248 | ENUM IDENTIFIER '{' enumerator_list '}'
249 | ENUM '{' enumerator_list ',' '}'
250 | ENUM IDENTIFIER '{' enumerator_list ',' '}'
251 | ENUM IDENTIFIER
252 ;
253
254enumerator_list
255 : enumerator
256 | enumerator_list ',' enumerator
257 ;
258
259enumerator
260 : IDENTIFIER
261 | IDENTIFIER '=' constant_expression
262 ;
263
264type_qualifier
265 : CONST
266 | RESTRICT
267 | VOLATILE
268 ;
269
270function_specifier
271 : INLINE
272 ;
273
274declarator
275 : pointer direct_declarator
276 | direct_declarator
277 ;
278
279
280direct_declarator
281 : IDENTIFIER
282 | '(' declarator ')'
283 | direct_declarator '[' type_qualifier_list assignment_expression ']'
284 | direct_declarator '[' type_qualifier_list ']'
285 | direct_declarator '[' assignment_expression ']'
286 | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']'
287 | direct_declarator '[' type_qualifier_list STATIC assignment_expression ']'
288 | direct_declarator '[' type_qualifier_list '*' ']'
289 | direct_declarator '[' '*' ']'
290 | direct_declarator '[' ']'
291 | direct_declarator '(' parameter_type_list ')'
292 | direct_declarator '(' identifier_list ')'
293 | direct_declarator '(' ')'
294 ;
295
296pointer
297 : '*'
298 | '*' type_qualifier_list
299 | '*' pointer
300 | '*' type_qualifier_list pointer
301 ;
302
303type_qualifier_list
304 : type_qualifier
305 | type_qualifier_list type_qualifier
306 ;
307
308
309parameter_type_list
310 : parameter_list
311 | parameter_list ',' ELLIPSIS
312 ;
313
314parameter_list
315 : parameter_declaration
316 | parameter_list ',' parameter_declaration
317 ;
318
319parameter_declaration
320 : declaration_specifiers declarator
321 | declaration_specifiers abstract_declarator
322 | declaration_specifiers
323 ;
324
325identifier_list
326 : IDENTIFIER
327 | identifier_list ',' IDENTIFIER
328 ;
329
330type_name
331 : specifier_qualifier_list
332 | specifier_qualifier_list abstract_declarator
333 ;
334
335abstract_declarator
336 : pointer
337 | direct_abstract_declarator
338 | pointer direct_abstract_declarator
339 ;
340
341direct_abstract_declarator
342 : '(' abstract_declarator ')'
343 | '[' ']'
344 | '[' assignment_expression ']'
345 | direct_abstract_declarator '[' ']'
346 | direct_abstract_declarator '[' assignment_expression ']'
347 | '[' '*' ']'
348 | direct_abstract_declarator '[' '*' ']'
349 | '(' ')'
350 | '(' parameter_type_list ')'
351 | direct_abstract_declarator '(' ')'
352 | direct_abstract_declarator '(' parameter_type_list ')'
353 ;
354
355initializer
356 : assignment_expression
357 | '{' initializer_list '}'
358 | '{' initializer_list ',' '}'
359 ;
360
361initializer_list
362 : initializer
363 | designation initializer
364 | initializer_list ',' initializer
365 | initializer_list ',' designation initializer
366 ;
367
368designation
369 : designator_list '='
370 ;
371
372designator_list
373 : designator
374 | designator_list designator
375 ;
376
377designator
378 : '[' constant_expression ']'
379 | '.' IDENTIFIER
380 ;
381
382statement
383 : labeled_statement
384 | compound_statement
385 | expression_statement
386 | selection_statement
387 | iteration_statement
388 | jump_statement
389 ;
390
391labeled_statement
392 : IDENTIFIER ':' statement
393 | CASE constant_expression ':' statement
394 | DEFAULT ':' statement
395 ;
396
397compound_statement
398 : '{' '}'
399 | '{' block_item_list '}'
400 ;
401
402block_item_list
403 : block_item
404 | block_item_list block_item
405 ;
406
407block_item
408 : declaration
409 | statement
410 ;
411
412expression_statement
413 : ';'
414 | expression ';'
415 ;
416
417selection_statement
418 : IF '(' expression ')' statement
419 | IF '(' expression ')' statement ELSE statement
420 | SWITCH '(' expression ')' statement
421 ;
422
423iteration_statement
424 : WHILE '(' expression ')' statement
425 | DO statement WHILE '(' expression ')' ';'
426 | FOR '(' expression_statement expression_statement ')' statement
427 | FOR '(' expression_statement expression_statement expression ')' statement
428 | FOR '(' declaration expression_statement ')' statement
429 | FOR '(' declaration expression_statement expression ')' statement
430 ;
431
432jump_statement
433 : GOTO IDENTIFIER ';'
434 | CONTINUE ';'
435 | BREAK ';'
436 | RETURN ';'
437 | RETURN expression ';'
438 ;
439
440translation_unit
441 : external_declaration
442 | translation_unit external_declaration
443 ;
444
445external_declaration
446 : function_definition
447 | declaration
448 ;
449
450function_definition
451 : declaration_specifiers declarator declaration_list compound_statement
452 | declaration_specifiers declarator compound_statement
453 ;
454
455declaration_list
456 : declaration
457 | declaration_list declaration
458 ;
459
460
461%%
462#include <stdio.h>
463
464extern char yytext[];
465extern int column;
466
467void yyerror(char const *s)
468{
469 fflush(stdout);
470 printf("\n%*s\n%*s\n", column, "^", column, s);
471}