-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.l
156 lines (103 loc) · 2.41 KB
/
scanner.l
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
%option noyywrap
%{
#include <stdlib.h>
#include <stdio.h>
#include "grammar.tab.h"
struct yy_stack_entry {
struct yy_stack_entry *prev;
FILE *fp;
void *cookie;
unsigned line;
YY_BUFFER_STATE yybs; /* Previous; current in YY_CURRENT_BUFFER */
} *yysp;
void *cmalloc(unsigned), _cfree(void *, unsigned);
void
yy_push(void)
{
struct yy_stack_entry *p;
p = cmalloc(sizeof(*p));
p->prev = yysp;
yysp = p;
yysp->yybs = YY_CURRENT_BUFFER;
}
void
yy_pop(void)
{
struct yy_stack_entry *p;
if (yysp->yybs) yy_switch_to_buffer(yysp->yybs);
p = yysp;
yysp = yysp->prev;
_cfree(p, sizeof(*p));
}
void
yy_popall(void)
{
while (yysp) yy_pop();
}
void
yy_push_file(FILE *fp, void *cookie)
{
yy_push();
yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE));
yysp->fp = fp;
yysp->cookie = cookie;
yysp->line = 1;
}
void
yy_push_str(char *s, unsigned n)
{
yy_push();
yy_scan_bytes(s, n);
yysp->fp = 0;
yysp->cookie = 0;
yysp->line = 0;
}
void *
yycookie(void)
{
return (yysp ? yysp->cookie : 0);
}
int
yyline(void)
{
return (yysp ? yysp->line : -1);
}
%}
ALPHA [A-Za-z_\-]
DECDIGIT [0-9]
HEXDIGIT [0-9A-Fa-f]
DQUOTE "\""
EDQUOTE "\\\""
NDQUOTE [^\"]
SYM {ALPHA}({ALPHA}|{DECDIGIT})*
CSYM "#"{SYM}
DSYM ({SYM}|{CSYM})("."({SYM}|{CSYM}))+
%%
"'" { return (TOK_QUOTE); }
"(" { return (TOK_LPAREN); }
"," { return (TOK_COMMA); }
")" { return (TOK_RPAREN); }
"[" { return (TOK_LSQBR); }
"=" { return (TOK_EQUAL); }
":=" { return (TOK_CEQUAL); }
"]" { return (TOK_RSQBR); }
"{" { return (TOK_LBRACE); }
"}" { return (TOK_RBRACE); }
"//" {
char c;
do {
c = input();
} while (!(c == '\n' || c == EOF));
if (yysp) ++yysp->line;
}
"\n" { if (yysp) ++yysp->line; }
"-"?{DECDIGIT}+"."{DECDIGIT}+ { return (TOK_FLOATNUM); }
"-"?{DECDIGIT}+ { return (TOK_DECNUM); }
0[Xx]{HEXDIGIT}+ { return (TOK_HEXNUM); }
{CSYM} { return (TOK_CSYM); }
{SYM} { return (TOK_SYM); }
{DSYM} { return (TOK_DSYM); }
{DQUOTE}({EDQUOTE}|{NDQUOTE})*{DQUOTE} { return (TOK_QSTR); }
{ALPHA}({ALPHA}|{DECDIGIT})*":" { return (TOK_SELN); }
[ \t\r] /* Skip whitespace */
. { fprintf(stderr, "Bad character (0x%02x) in input\n", yytext[0]); }