1 This parser is primarily for testing indent and linebreak handling.
2 It reads a very simple code fragment with if/else statements and
3 simple assignments with expressions, and then prints out the same
4 with complete bracketing and indenting.
7 myCFLAGS := -Wall -g -fplan9-extensions
8 CFLAGS := $(filter-out $(myCFLAGS),$(CFLAGS)) $(myCFLAGS)
9 myLDLIBS:= libparser.o libscanner.o libmdcode.o -licuuc
10 LDLIBS := $(filter-out $(myLDLIBS),$(LDLIBS)) $(myLDLIBS)
13 itest.c itest.h : indent_test.mdc parsergen libparser.o libscanner.o libmdcode.o
14 ./parsergen -o itest --LALR --tag indent indent_test.mdc
15 indent_test.mk: indent_test.mdc md2c
16 ./md2c indent_test.mdc
19 doitest: itest itest.code
21 checkitest: itest itest.code
22 @grep -v '^#' itest.out > .tmp.out
23 @./itest itest.code | diff -u - .tmp.out || echo itest FAILED
29 ./parsergen -o itest --LALR indent_test.cgm
30 cc -o itest itest.c lib*.o -licuuc -lgmp
35 struct expression *left, *right;
38 struct statement *next;
39 struct expression *expr;
40 struct statement *thenpart;
41 struct statement *elsepart;
55 static void free_expression(struct expression *e)
59 free_expression(e->left);
60 free_expression(e->right);
64 static void free_statement(struct statement *s)
68 free_statement(s->next);
69 free_expression(s->expr);
70 free_statement(s->thenpart);
71 free_statement(s->elsepart);
75 static void print_expression(struct expression *e)
77 if (e->left && e->right) printf("(");
79 print_expression(e->left);
81 printf("%.*s", e->op.len, e->op.txt);
83 print_expression(e->right);
84 if (e->left && e->right) printf(")");
87 static void print_statement(struct statement *s, int depth)
92 printf("%*.s", depth, "");
93 print_expression(s->expr);
96 printf("%*.sif ", depth, "");
97 print_expression(s->expr);
99 print_statement(s->thenpart, depth+4);
101 printf("%*.selse:\n", depth, "");
102 print_statement(s->elsepart, depth+4);
105 print_statement(s->next, depth);
108 int main(int argc, char *argv[])
110 int fd = open(argv[1], O_RDONLY);
111 int len = lseek(fd, 0, 2);
112 char *file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
113 struct section *s = code_extract(file, file+len, NULL);
114 struct token_config config = {
115 .ignored = (1 << TK_line_comment)
116 | (1 << TK_block_comment),
117 .number_chars = ".,_+-",
121 parse_itest(s->code, &config, argc > 2 ? stderr : NULL);
123 struct section *t = s->next;
135 Program -> Statementlist ${ print_statement($1, 0); }$
141 Statementlist -> Statements ${ $0 = $<1; }$
143 Statements -> Statements Statement ${
145 struct statement **s;
153 | Statement ${ $0 = $<1; }$
154 | ERROR ${ printf("statement ERROR\n"); $0 = NULL; }$
160 Block -> Open Statementlist Close ${ $0 = $<2; }$
161 | Open SimpleStatements } ${ $0 = $<2; }$
162 | : Statementlist ${ $0 = $<2; }$
164 SimpleStatements -> SimpleStatements ; SimpleStatement ${
166 struct statement **s;
174 | SimpleStatement ${ $0 = $<1; }$
175 | SimpleStatements ; ${ $0 = $<1; }$
177 SimpleStatement -> Factor = Expression ${
178 $0 = calloc(1, sizeof(struct statement));
179 $0->expr = calloc(1, sizeof(struct expression));
180 $0->expr->left = $<1;
181 $0->expr->op = $2.txt;
182 $0->expr->right = $<3;
184 Statement -> SimpleStatements NEWLINE ${
187 | IfStatement ${ $0 = $<1; }$
188 | Statement NEWLINE ${ $0 = $<1; }$
192 IfStatement -> if Expression Block OptNL $$else ${
193 $0 = calloc(1, sizeof(struct statement));
197 | if Expression : SimpleStatements $$else ${
198 $0 = calloc(1, sizeof(struct statement));
202 | if Expression Block OptNL else Block ${
203 $0 = calloc(1, sizeof(struct statement));
208 | if Expression Block OptNL else : SimpleStatements ${
209 $0 = calloc(1, sizeof(struct statement));
214 | if Expression Block OptNL else IfStatement ${
215 $0 = calloc(1, sizeof(struct statement));
222 Expression -> Expression + Term ${
223 $0 = calloc(1, sizeof(struct expression));
228 | Expression - Term ${
229 $0 = calloc(1, sizeof(struct expression));
234 | Term ${ $0 = $<1; }$
235 Term -> Term * Factor ${
236 $0 = calloc(1, sizeof(struct expression));
242 $0 = calloc(1, sizeof(struct expression));
247 | Factor ${ $0 = $<1; }$
248 Factor -> IDENTIFIER ${
249 $0 = calloc(1, sizeof(struct expression));
258 hello = yes; mister = no;
279 if true {yes=x;} else : no=x
280 if true: yes = no; no = yes;
303 (foo=((x*x)+(y/two)));