-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_fstring.l
43 lines (34 loc) · 971 Bytes
/
04_fstring.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
%top{
/* --------------------------------
* define python f-string syntax
*
* demostrates 'start condition' in flex
* --------------------------------
*/
}
%{
#include <string.h>
%}
%option outfile="04_fstring.c"
/* define state identifiers */
%x fstr
%x inpo
/* define start and stop conditions of the state */
fstr_start f\"
fstr_stop \"
inpo_start "{"
inpo_stop "}"
/* use human readable name instead of regex */
identifier [a-zA-Z_][a-zA-Z0-9_][^}]*
%%
{fstr_start} { printf("fstr started\n"); BEGIN(fstr); }
<fstr>{inpo_start} { printf("interpolation will start\n"); BEGIN(inpo); }
<fstr>[^}{"]?* { printf("whatever, length: %ld\n", strlen(yytext)); }
<inpo>{identifier} { printf("variable name: |%s|\n", yytext); }
<inpo>{inpo_stop} { printf("interpolation will stop\n"); BEGIN(fstr); }
<fstr>{fstr_stop} { printf("fstr stop\n"); BEGIN(INITIAL); }
. { /* ignore non f-strings */ }
%%
int main(int argc, char* argv[]) {
yylex();
}