tlvc.l

Mon, 01 Sep 2025 00:14:59 +0800

author
Gong Zhile <gongzl@stu.hebust.edu.cn>
date
Mon, 01 Sep 2025 00:14:59 +0800
changeset 0
59c92fa19678
permissions
-rw-r--r--

Initial Lexer/Parser

0
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
1 %{
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
2 #include <string.h>
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
3 #include <stdlib.h>
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
4 #include "tlvast.h"
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
5 #include "tlvc.tab.h"
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
6 %}
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
7
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
8 %option noyywrap
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
9 %option yylineno
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
10
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
11 %%
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
12 "message" { return MESSAGE; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
13
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
14 "u8" { yylval.num = TLV_TYPE_U8; return TYPE; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
15 "u16" { yylval.num = TLV_TYPE_U16; return TYPE; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
16 "u32" { yylval.num = TLV_TYPE_U32; return TYPE; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
17 "u64" { yylval.num = TLV_TYPE_U64; return TYPE; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
18
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
19 "STRING" { yylval.num = TLV_TYPE_STRING; return TYPE; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
20
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
21 0x[0-9a-fA-F]+ { yylval.num = (int)strtol(yytext,NULL,16); return NUMBER; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
22
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
23 [a-zA-Z_][a-zA-Z0-9_]* { yylval.str = strdup(yytext); return IDENT; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
24
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
25 [ \t\r\n]+ ; // skip whitespace
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
26 "{" { return '{'; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
27 "}" { return '}'; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
28 ";" { return ';'; }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
29 . { fprintf(stderr,"Unexpected char: %c\n", yytext[0]); }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
30 %%

mercurial