tlvc.y

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 %locations
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
2 %define parse.error verbose
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
3
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
4 %{
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
5 #include "tlvast.h"
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
6 #include "tlvc.yy.h"
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
7 extern void yyerror(const char *s);
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
8 int field_index = 0;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
9 %}
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 %union {
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
12 int num;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
13 char *str;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
14 struct tlv_field *field;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
15 struct tlv_field_list_head *field_list;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
16 struct tlv *msg;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
17 }
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 %token <str> IDENT
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
20 %token <num> TYPE
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
21 %token <num> NUMBER
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
22 %token MESSAGE
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
23
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
24 %type <msg> message
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
25 %type <field> field
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
26 %type <field_list> field_list
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
27
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
28 %%
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
29
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
30 input:
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
31 /* empty */
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
32 | input message {
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
33 TAILQ_INSERT_TAIL(&g_tlvs, $2, entries);
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
34 }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
35 ;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
36
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
37 message:
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
38 MESSAGE IDENT NUMBER '{' field_list '}' {
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
39 struct tlv *m = malloc(sizeof(*m));
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
40 m->name = strdup($2);
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
41 m->tag = $3;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
42 TAILQ_INIT(&m->fields);
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
43
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
44 struct tlv_field *f;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
45 TAILQ_FOREACH(f, $5, entries) {
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
46 TAILQ_INSERT_TAIL(&m->fields, f, entries);
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
47 }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
48
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
49 $$ = m;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
50 }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
51 ;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
52
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
53 field_list:
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
54 /* empty */ {
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
55 $$ = NULL;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
56 }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
57 | field_list field {
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
58 if ($1 == NULL) {
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
59 struct tlv_field_list_head *list = malloc(sizeof(*list));
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
60 TAILQ_INIT(list);
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
61 TAILQ_INSERT_TAIL(list, $2, entries);
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
62 $$ = list;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
63 } else {
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
64 TAILQ_INSERT_TAIL($1, $2, entries);
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
65 $$ = $1;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
66 }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
67 field_index = 0;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
68 }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
69 ;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
70
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
71 field:
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
72 TYPE IDENT ';' {
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
73 struct tlv_field *f = malloc(sizeof(*f));
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
74 f->type = $1;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
75 f->name = strdup($2);
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
76 f->tag = field_index++;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
77 $$ = f;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
78 }
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
79 ;
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
80
59c92fa19678 Initial Lexer/Parser
Gong Zhile <gongzl@stu.hebust.edu.cn>
parents:
diff changeset
81 %%

mercurial