-
Notifications
You must be signed in to change notification settings - Fork 0
/
pasclang.l
252 lines (208 loc) · 4.65 KB
/
pasclang.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
%{
/* This file is part of the software similarity tester SIM.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: pasclang.l,v 2.2 2017-12-11 14:12:36 dick Exp $
*/
/*
PASCAL language front end for the similarity tester.
Author: Maarten van der Meulen <[email protected]>
Date: May 1986
*/
#include "options.h"
#include "token.h"
#include "properties.h"
#include "idf.h"
#include "lex.h"
#include "lang.h"
/* General language front end data */
Token lex_token;
size_t lex_nl_cnt;
size_t lex_tk_cnt;
size_t lex_non_ASCII_cnt;
/* Language-dependent data */
/* Data for module idf */
static const struct idf ppcmd[] = {
{"define", META('d')},
{"else", META('e')},
{"endif", META('E')},
{"if", META('i')},
{"ifdef", META('I')},
{"ifndef", META('x')},
{"include", MTCT('I')},
{"line", META('l')},
{"undef", META('u')}
};
static const struct idf reserved[] = {
{"and", NORM('&')},
{"array", NORM('A')},
{"begin", NORM('{')},
{"case", NORM('c')},
{"const", NORM('C')},
{"div", NORM('/')},
{"do", NORM('D')},
{"downto", NORM('d')},
{"else", NORM('e')},
{"end", NORM('}')},
{"extern", CTRL('E')},
{"file", NORM('F')},
{"for", NORM('f')},
{"function", NORM('p')}, /* Equal to procedure */
{"goto", NORM('g')},
{"if", NORM('i')},
{"in", NORM('I')},
{"label", NORM('l')},
{"mod", NORM('%')},
{"nil", NORM('n')},
{"not", NORM('!')},
{"of", No_Token},
{"or", NORM('|')},
{"packed", NORM('P')},
{"procedure", NORM('p')},
{"program", No_Token},
{"record", NORM('r')},
{"repeat", NORM('R')},
{"set", NORM('s')},
{"then", No_Token},
{"to", NORM('t')},
{"type", NORM('T')},
{"until", NORM('u')},
{"var", NORM('v')},
{"while", NORM('w')},
{"with", NORM('W')}
};
/* Special treatment of identifiers */
static Token
idf2token(int hashing) {
Token tk;
lower_case(yytext); /* Pascal is case-insensitive */
tk = idf_in_list(yytext, reserved, sizeof reserved, IDF);
if (Token_EQ(tk, IDF) && hashing) {
/* return a one-Token hash code */
tk = idf_hashed(yytext);
}
return tk;
}
/* Token sets for properties module */
const Token Non_Finals[] = {
IDF, /* identifier */
NORM('{'), /* also begin */
NORM('('),
NORM('['),
NORM('A'), /* array */
NORM('c'), /* case */
NORM('C'), /* const */
NORM('/'), /* div */
CTRL('E'), /* extern */
NORM('F'), /* file */
NORM('f'), /* for */
NORM('g'), /* goto */
NORM('i'), /* if */
NORM('l'), /* label */
NORM('P'), /* packed */
NORM('p'), /* procedure/function */
NORM('r'), /* record */
NORM('R'), /* repeat */
NORM('s'), /* set */
NORM('T'), /* type */
NORM('v'), /* var */
NORM('w'), /* while */
NORM('W'), /* with */
No_Token
};
const Token Non_Initials[] = {
NORM(')'),
NORM('}'),
NORM(';'),
No_Token
};
const Token Openers[] = {
NORM('{'),
NORM('('),
NORM('['),
No_Token
};
const Token Closers[] = {
NORM('}'),
NORM(')'),
NORM(']'),
No_Token
};
/* Language-dependent code */
const char *Subject = "Pascal programs";
void
Init_Language(void) {
Init_Language_Properties(Non_Finals, Non_Initials, Openers, Closers);
}
%}
%option noyywrap
%Start Comment
Layout ([ \t\r\f])
ASCII95 ([\040-\176])
AnyQuoted (\\.)
StrChar ([^\'\n\\]|{AnyQuoted})
StartComment ("{"|"(*")
EndComment ("}"|"*)")
SafeComChar ([^*}\n])
UnsafeComChar ("*")
Digit ([0-9])
Idf ([A-Za-z][A-Za-z0-9_]*)
%%
{StartComment} { /* See clang.l */
BEGIN Comment;
}
<Comment>{SafeComChar}+ { /* safe comment chunk */
}
<Comment>{UnsafeComChar} { /* unsafe char, read one by one */
}
<Comment>"\n" { /* to break up long comments */
return_eol();
}
<Comment>{EndComment} { /* end-of-comment */
BEGIN INITIAL;
}
\'{StrChar}*\' { /* character strings */
return_ch(STR);
}
^#{Layout}*include.* { /* ignore #include lines */
}
^#{Layout}*{Idf} { /* a preprocessor line */
char *idf = yytext+1;
/* skip layout in front of preprocessor identifier */
while (*idf == ' ' || *idf == '\t') {
idf++;
}
return_tk(idf_in_list(idf, ppcmd, sizeof ppcmd, NORM('#')));
}
{Digit}+ { /* numeral, passed as an identifier */
return_tk(IDF);
}
{Idf}/"(" { /* identifier in front of ( */
Token tk;
tk = idf2token(is_set_option('F'));
if (!Token_EQ(tk, No_Token)) return_tk(tk);
}
{Idf} { /* identifier */
Token tk;
tk = idf2token(0 /* no hashing */);
if (!Token_EQ(tk, No_Token)) return_tk(tk);
}
\; { /* semicolon, conditionally ignored */
if (is_set_option('f')) return_ch(yytext[0]);
}
\n { /* count newlines */
return_eol();
}
{Layout} { /* ignore layout */
}
{ASCII95} { /* copy other text */
return_ch(yytext[0]);
}
. { /* count non-ASCII chars */
lex_non_ASCII_cnt++;
}
%%
/* More language-dependent code */
void
yystart(void) {
BEGIN INITIAL;
}