-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsample.g
More file actions
167 lines (147 loc) · 4.07 KB
/
sample.g
File metadata and controls
167 lines (147 loc) · 4.07 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
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
/*
Sample Grammar
*/
{
#include "d.h"
}
${declare longest_match program}
program : statement*;
statement : external | definition | expression ';' ;
definition : identifier ':' expression ';'
{
D_Sym *s;
char *ts = dup_str($n0.start_loc.s, $n0.end);
${scope} = new_D_Scope(${scope});
s = NEW_D_SYM(${scope}, $n0.start_loc.s, $n0.end);
printf("def Sym '%s' line %d: %X\n", ts, $n0.start_loc.line, (int)(uintptr_t)s);
d_free(ts);
$$ = s;
};
external
: 'extern' external_type identifier ';'
| 'extern' external_type identifier
'(' (external_type (',' external_type)*)? ')' ';'
;
external_type : builtin_types;
expression
: constant
| identifier {
D_Sym *s = find_D_Sym(${scope}, $n0.start_loc.s, $n0.end);
char *ts = dup_str($n0.start_loc.s, $n0.end);
if (s)
printf("ref Sym '%s' line %d val %d: %X of %X\n",
ts, $n0.start_loc.line,
s->user, (int)(uintptr_t)s, (int)(uintptr_t)s->update_of);
else
printf("ref Sym '%s' line %d: not found\n",
ts, $n0.start_loc.line);
d_free(ts);
}
| pre_operator expression
| expression post_operator
| expression binary_operator expression
| '{' expression '}'
| '(' expression ')'
| '[' expression ']'
| expression '?' expression ':' expression $right 8600
| 'if' '(' expression ')' expression $right 6000
| 'if' '(' expression ')' expression 'else' expression $right 6100
| 'while' '(' expression ')' expression $right 6200
| 'do' expression 'while' expression $right 6300
| 'for' '(' expression (';' expression (';' expression)?)? ')'
expression $right 6400
;
constant : integer | float | character | strings;
strings : string+;
binary_operator
: '::' $binary_op_right 10000
| '.' $binary_op_left 9900
| '->' $binary_op_left 9900
| '*' $binary_op_left 9600
| '/' $binary_op_left 9600
| '%' $binary_op_left 9600
| '+' $binary_op_left 9500
| '-' $binary_op_left 9500
| '<<' $binary_op_left 9400
| '>>' $binary_op_left 9400
| '<' $binary_op_left 9300
| '<=' $binary_op_left 9300
| '>' $binary_op_left 9300
| '>=' $binary_op_left 9300
| '==' $binary_op_left 9200
| '!=' $binary_op_left 9200
| '&' $binary_op_left 9100
| '^' $binary_op_left 9000
| '|' $binary_op_left 8900
| '&&' $binary_op_left 8800
| '||' $binary_op_left 8700
| '=' $binary_op_left 8500
| '*=' $binary_op_left 8500
| '/=' $binary_op_left 8500
| '%=' $binary_op_left 8500
| '+=' $binary_op_left 8500
| '-=' $binary_op_left 8500
| '<<=' $binary_op_left 8500
| '>>=' $binary_op_left 8500
| '&=' $binary_op_left 8500
| '|=' $binary_op_left 8500
| '^=' $binary_op_left 8500
| ',' $binary_op_left 8400
| $binary_op_left 7000
;
pre_operator
: '::' $unary_op_right 10000
| '--' $unary_op_right 9800
| '++' $unary_op_right 9800
| '-' $unary_op_right 8600
| '+' $unary_op_right 8600
| '~' $unary_op_right 8600
| '!' $unary_op_right 8600
| '*' $unary_op_right 8600
| '&' $unary_op_right 8600
| '(' external_type ')' $unary_op_right 8600
| 'sizeof' $unary_op_right 8600
;
post_operator
: '--' $unary_op_left 9700
| '++' $unary_op_left 9700
| '{' expression '}' $unary_op_left 9700
| '(' expression ')' $unary_op_left 9700
| '[' expression ']' $unary_op_left 9700
;
builtin_types
: 'int'
| 'uint'
| 'int8'
| 'uint8'
| 'int16'
| 'uint16'
| 'int32'
| 'uint32'
| 'int64'
| 'uint64'
| 'float'
| 'float32'
| 'float64'
;
character: "'[^']*'";
string: "\"[^\"]*\"";
integer: "-?([0-9]|0(x|X))[0-9]*(u|U|b|B|w|W|L|l)*" $term -1;
float: "[\-+]?([0-9]+\.[0-9]*|\.[0-9]+)([eE][\-+]?[0-9]+)?" $term -2;
{
char *reserved_words[] = { "if", "else", "extern", "sizeof", "int", "uint",
"int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64",
"float", "float32", "float64", NULL };
static int is_one_of(char *s, char **list) {
while (*list) { if (!strcmp(s, *list)) return 1; list++; }
return 0;
}
}
identifier: "[a-zA-Z_][a-zA-Z_0-9]*" $term -3 [
char *ts = dup_str($n0.start_loc.s, $n0.end);
if (is_one_of(ts, reserved_words)) {
d_free(ts);
${reject};
}
d_free(ts);
];