-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
132 lines (116 loc) · 2.18 KB
/
parser.y
File metadata and controls
132 lines (116 loc) · 2.18 KB
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
%{
#include <stdio.h>
#include <string.h>
#include <command.h>
#include <Argument.h>
int yylex(void);
void yyerror(char *s);
extern int yylineno;
%}
%union {
const char* symbol;
int numberValue;
char charValue;
struct RawArgument argumentValue;
};
%token <numberValue> DECNUMBER
%token <numberValue> HEXNUMBER
%token <numberValue> CHARNUMBER
%token <charValue> PLUS_OR_MINUS
%token <symbol> IDENTIFIER
%token <symbol> NEWLINE
%token <symbol> STRING
%type <symbol> program
%type <argumentValue> argument
%type <numberValue> number
%%
program:
program line
| { /* empty rules have no default action, so this avoids a warning*/ }
;
line:
NEWLINE
| statement NEWLINE;
statement:
label genericCommand
| label
| genericCommand
;
genericCommand:
command
| metaCommand
;
command:
IDENTIFIER {
(void)command0($1);
}
| IDENTIFIER argument ',' argument {
command2($1, &$2, &$4);
}
| IDENTIFIER argument {
command1($1, &$2);
}
;
label:
IDENTIFIER ':' {
label($1);
}
;
metaCommand:
'.' IDENTIFIER argument {
metaCommand1($2, &$3);
}
|
'.' IDENTIFIER IDENTIFIER argument {
metaCommand2($2, $3, &$4);
}
;
argument:
IDENTIFIER {
$$.type = IDENTIFIER_ARGUMENT;
$$.identifier = $1;
}
| number {
$$.type = VALUE_ARGUMENT;
$$.value = $1;
}
| '(' number ')' {
$$.type = DEREFERENCED_VALUE_ARGUMENT;
$$.value = $2;
}
| '(' IDENTIFIER ')' {
$$.type = DEREFERENCED_IDENTIFIER_ARGUMENT;
$$.identifier = $2;
}
| '(' IDENTIFIER PLUS_OR_MINUS number ')' {
$$.type = DEREFERENCED_INDEXED_IDENTIFIER_ARGUMENT;
$$.identifier = $2;
$$.indexOperation = $3;
$$.value = 0;
$$.indexValue = $4;
$$.indexIdentifier = 0;
}
| '(' IDENTIFIER PLUS_OR_MINUS IDENTIFIER ')' {
$$.type = DEREFERENCED_INDEXED_IDENTIFIER_ARGUMENT;
$$.identifier = $2;
$$.indexOperation = $3;
$$.indexValue = 0;
$$.indexIdentifier = $4;
}
| STRING {
$$.type = STRING_ARGUMENT;
$$.identifier = $1;
}
;
number:
DECNUMBER
| HEXNUMBER
| CHARNUMBER
;
%%
void yyerror(char *s) {
extern const char* filename;
extern int NUMBER_OF_ERRORS;
fprintf(stderr, "%s:%d: error: %s\n", filename, yylineno, s);
++NUMBER_OF_ERRORS;
}