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 itestCFLAGS := -Wall -g -fplan9-extensions
8 itestLDLIBS:= libparser.o libscanner.o libmdcode.o -licuuc
11 itest.c itest.h : indent_test.mdc parsergen
12 ./parsergen -o itest --LALR --tag indent indent_test.mdc
13 indent_test.mk: indent_test.mdc md2c
14 ./md2c indent_test.mdc
15 itest: itest.c | $(filter %.o,$(itestLDLIBS))
16 $(CC) $(itestCFLAGS) $^ $(itestLDLIBS) -o $@
18 doitest: itest itest.code
20 checkitest: itest itest.code
21 @grep -v '^#' itest.out > .tmp.out
22 @./itest itest.code | diff -u - .tmp.out || echo itest FAILED
24 ' itest.code > itest2.code
25 @./itest itest2.code| diff -u - .tmp.out || echo itest2 FAILED
31 ./parsergen -o itest --LALR indent_test.cgm
32 cc -o itest itest.c lib*.o -licuuc -lgmp
37 struct expression *left, *right;
40 struct statement *next;
41 struct expression *expr;
42 struct statement *thenpart;
43 struct statement *elsepart;
57 static void free_expression(struct expression *e)
61 free_expression(e->left);
62 free_expression(e->right);
66 static void free_statement(struct statement *s)
70 free_statement(s->next);
71 free_expression(s->expr);
72 free_statement(s->thenpart);
73 free_statement(s->elsepart);
77 static void print_expression(struct expression *e)
79 if (e->left && e->right) printf("(");
81 print_expression(e->left);
83 printf("%.*s", e->op.len, e->op.txt);
85 print_expression(e->right);
86 if (e->left && e->right) printf(")");
89 static void print_statement(struct statement *s, int depth)
94 printf("%*.s", depth, "");
95 print_expression(s->expr);
98 printf("%*.sif ", depth, "");
99 print_expression(s->expr);
101 print_statement(s->thenpart, depth+4);
103 printf("%*.selse:\n", depth, "");
104 print_statement(s->elsepart, depth+4);
107 print_statement(s->next, depth);
110 int main(int argc, char *argv[])
112 int fd = open(argv[1], O_RDONLY);
113 int len = lseek(fd, 0, 2);
114 char *file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
115 struct section *s = code_extract(file, file+len, NULL);
116 struct token_config config = {
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); }$
140 Statementlist -> Statements ${ $0 = $<1; }$
141 | Newlines Statements ${ $0 = $<2; }$
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 | : SimpleStatements ${ $0 = $<2; }$
163 | : StatementBlock ${ $0 = $<2; }$
164 StatementBlock -> Statementlist $$OUT ${ $0 = $<1; }$
166 SimpleStatements -> SimpleStatements ; SimpleStatement ${
168 struct statement **s;
176 | SimpleStatement ${ $0 = $<1; }$
177 | SimpleStatements ; ${ $0 = $<1; }$
179 SimpleStatement -> Factor = Expression ${
180 $0 = calloc(1, sizeof(struct statement));
181 $0->expr = calloc(1, sizeof(struct expression));
182 $0->expr->left = $<1;
183 $0->expr->op = $2.txt;
184 $0->expr->right = $<3;
186 SSline -> SimpleStatements NEWLINE ${ $0 = $<1; }$
187 Statement -> SSline ${ $0 = $<1; }$
188 | IfStatement $$NEWLINE ${ $0 = $<1; }$
189 | Statement NEWLINE ${ $0 = $<1; }$
193 IfHead -> if Expression Block ${
194 $0 = calloc(1, sizeof(struct statement));
198 | IfHead NEWLINE ${ $0 = $<1; }$
200 IfStatement -> IfHead $$else ${ $0 = $<1; }$
201 | IfHead else Block ${
205 | IfHead else IfStatement ${
211 Expression -> Expression + Term ${
212 $0 = calloc(1, sizeof(struct expression));
217 | Expression - Term ${
218 $0 = calloc(1, sizeof(struct expression));
223 | Term ${ $0 = $<1; }$
224 Term -> Term * Factor ${
225 $0 = calloc(1, sizeof(struct expression));
231 $0 = calloc(1, sizeof(struct expression));
236 | Factor ${ $0 = $<1; }$
237 Factor -> IDENTIFIER ${
238 $0 = calloc(1, sizeof(struct expression));
247 hello = yes; mister = no;
268 if true {yes=x;} else : no=x
269 if true: yes = no; no = yes;
270 if false: yes=ok; else: no=ok
293 (foo=((x*x)+(y/two)));