/
myparser.y
74 lines (51 loc) · 1.34 KB
/
myparser.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
%{
#include <stdio.h>
#include <stdlib.h>
#define YYDEBUG 1
extern int yylex();
extern int yyparse();
extern size_t lineno;
extern int include_stack_ptr;
extern char filenames[32][256];
extern char *symstack[5];
extern int symstackptr;
void yyerror(const char* s);
%}
%define parse.trace
%verbose
%union {
char *s;
}
%token <s> T_UNKNOWN
%token <s> T_COMMENT
%token <s> T_CONFIG_DIRECTIVE
%token <s> T_CONFIG_DIRECTIVE_ARGUMENT
%token <s> T_INCLUDE
%token <s> T_INCLUDE_SOURCE
%start config
%%
config: /* empty */
| config line
;
line: comment
| config_line
;
comment: T_COMMENT { printf("THIS IS A COMMENT in line %zu: '%s'\n", lineno+1, $1); free($1); symstackptr--; }
;
config_line: config_directive_line
| config_include_line
;
config_directive_line:
T_CONFIG_DIRECTIVE T_CONFIG_DIRECTIVE_ARGUMENT { printf("This is a configuration directive and argument: '%s' '%s'\n", $1, $2); free($1); symstackptr--; free($2); symstackptr--; }
;
config_include_line:
T_INCLUDE T_INCLUDE_SOURCE { printf("%s %s\n", $1, $2); free($1); symstackptr--; free($2); symstackptr--; }
;
%%
void yyerror(const char* s) {
fprintf(stderr, "Parse error: %s in file %s, line %zu\n", s, filenames[include_stack_ptr], lineno+1);
for(int i = symstackptr-1; i >= 0; i--) {
free(symstack[i]);
}
exit(1);
}