comparison lwcc/parse_c.y @ 495:5b8871fd7503

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