annotate lwcc/parse_c.y @ 551:4ec5c94ade71

Added tag lwtools-4.21 for changeset c71c80a3b280
author William Astle <lost@l-w.ca>
date Sun, 23 Apr 2023 10:23:43 -0600
parents a3e277c58df9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
1 %include {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
2 #include <assert.h> // only needed due to a bug in lemon
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
3 #include <stdio.h>
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
4 #include "parse.h"
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
5 #include "tree.h"
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
6 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
7
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
8 %token_type { struct tokendata * }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
9 %token_prefix PTOK_
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
10 %token_destructor { tokendata_free($$); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
11 %default_type { node_t * }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
12 %extra_argument { struct parserinfo *pinfo }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
13
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
14 program(A) ::= rprogram(B). { A = B; pinfo -> parsetree = A; }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
15
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
16 rprogram(A) ::= rprogram(C) globaldecl(B). {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
17 A = C;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
18 node_addchild(A, B);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
19 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
20 rprogram(A) ::= . { A = node_create(NODE_PROGRAM); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
21
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
22 globaldecl(A) ::= vardecl(B). { A = B; }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
23 globaldecl(A) ::= fundecl(B). { A = B; }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
24
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
25 vardecl(A) ::= datatype(B) ident(C) ENDS. {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
26 A = node_create(NODE_DECL, B, C);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
27 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
28
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
29 ident(A) ::= IDENTIFIER(B). { A = node_create(NODE_IDENT, B -> strval); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
30
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
31 datatype(A) ::= typename(B). { A = B; }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
32 datatype(A) ::= datatype(B) STAR. { A = node_create(NODE_TYPE_PTR, B); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
33
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
34 typename(A) ::= KW_VOID. { A = node_create(NODE_TYPE_VOID); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
35 typename(A) ::= KW_FLOAT. { A = node_create(NODE_TYPE_FLOAT); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
36 typename(A) ::= KW_DOUBLE. { A = node_create(NODE_TYPE_DOUBLE); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
37 typename(A) ::= KW_LONG KW_DOUBLE. { A = node_create(NODE_TYPE_LDOUBLE); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
38 typename(A) ::= baseint(B). { A = B; }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
39 typename(A) ::= KW_UNSIGNED. { A = node_create(NODE_TYPE_UINT); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
40 typename(A) ::= KW_SIGNED. { A = node_create(NODE_TYPE_INT); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
41 typename(A) ::= KW_SIGNED baseint(B). { A = B; if (A -> type == NODE_TYPE_CHAR) A -> type = NODE_TYPE_SCHAR; }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
42 typename(A) ::= KW_UNSIGNED baseint(B). {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
43 A = B;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
44 switch (A -> type)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
45 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
46 case NODE_TYPE_CHAR:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
47 A -> type = NODE_TYPE_UCHAR;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
48 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
49 case NODE_TYPE_SHORT:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
50 A -> type = NODE_TYPE_USHORT;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
51 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
52 case NODE_TYPE_INT:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
53 A -> type = NODE_TYPE_UINT;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
54 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
55 case NODE_TYPE_LONG:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
56 A -> type = NODE_TYPE_ULONG;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
57 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
58 case NODE_TYPE_LONGLONG:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
59 A -> type = NODE_TYPE_ULONGLONG;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
60 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
61 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
62 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
63
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
64 baseint(A) ::= KW_INT. { A = node_create(NODE_TYPE_INT); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
65 baseint(A) ::= KW_LONG. { A = node_create(NODE_TYPE_LONG); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
66 baseint(A) ::= KW_LONG KW_INT. { A = node_create(NODE_TYPE_LONG); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
67 baseint(A) ::= KW_LONG KW_LONG. { A = node_create(NODE_TYPE_LONGLONG); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
68 baseint(A) ::= KW_SHORT. { A = node_create(NODE_TYPE_SHORT); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
69 baseint(A) ::= KW_LONG KW_LONG KW_INT. { A = node_create(NODE_TYPE_LONGLONG); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
70 baseint(A) ::= KW_SHORT KW_INT. { A = node_create(NODE_TYPE_SHORT); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
71 baseint(A) ::= KW_CHAR. { A = node_create(NODE_TYPE_CHAR); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
72
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
73
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
74 fundecl(A) ::= datatype(B) ident(C) arglist(D) statementblock(E). {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
75 A = node_create(NODE_FUNDEF, B, C, D, E);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
76 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
77
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
78 fundecl(A) ::= datatype(B) ident(C) arglist(D) ENDS. {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
79 A = node_create(NODE_FUNDECL, B, C, D);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
80 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
81
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
82 arglist(A) ::= OPAREN CPAREN. { A = node_create(NODE_FUNARGS); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
83
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
84 statementblock(A) ::= OBRACE CBRACE. { A = node_create(NODE_BLOCK); }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
85
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
86 %parse_failure {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
87 fprintf(stderr, "Parse error\n");
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
88 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
89
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
90 %stack_overflow {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
91 fprintf(stderr, "Parser stack overflow\n");
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
92 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
93
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
94 %syntax_error {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
95 fprintf(stderr, "Undexpected token %d: ", TOKEN -> tokid);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
96 tokendata_print(stderr, TOKEN);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
diff changeset
97 }