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 tests:: itest itest.code
24 ./parsergen -o itest --LALR indent_test.cgm
25 cc -o itest itest.c lib*.o -licuuc -lgmp
30 struct expression *left, *right;
33 struct statement *next;
34 struct expression *expr;
35 struct statement *thenpart;
36 struct statement *elsepart;
50 static void free_expression(struct expression *e)
54 free_expression(e->left);
55 free_expression(e->right);
59 static void free_statement(struct statement *s)
63 free_statement(s->next);
64 free_expression(s->expr);
65 free_statement(s->thenpart);
66 free_statement(s->elsepart);
70 static void print_expression(struct expression *e)
72 if (e->left && e->right) printf("(");
74 print_expression(e->left);
76 printf("%.*s", e->op.len, e->op.txt);
78 print_expression(e->right);
79 if (e->left && e->right) printf(")");
82 static void print_statement(struct statement *s, int depth)
87 printf("%*.s", depth, "");
88 print_expression(s->expr);
91 printf("%*.sif ", depth, "");
92 print_expression(s->expr);
94 print_statement(s->thenpart, depth+4);
96 printf("%*.selse:\n", depth, "");
97 print_statement(s->elsepart, depth+4);
100 print_statement(s->next, depth);
103 int main(int argc, char *argv[])
105 int fd = open(argv[1], O_RDONLY);
106 int len = lseek(fd, 0, 2);
107 char *file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
108 struct section *s = code_extract(file, file+len, NULL);
109 struct token_config config = {
110 .ignored = (1 << TK_line_comment)
111 | (1 << TK_block_comment),
112 .number_chars = ".,_+-",
116 parse_itest(s->code, &config, argc > 2 ? stderr : NULL);
118 struct section *t = s->next;
131 Program -> Statementlist ${ print_statement($1, 0); }$
137 Statementlist -> Statements ${ $0 = $<1; }$
139 Statements -> Statements Statement ${
141 struct statement **s;
149 | Statement ${ $0 = $<1; }$
150 | ERROR ${ printf("statement ERROR\n"); $0 = NULL; }$
156 Block -> Open Statementlist Close ${ $0 = $<2; }$
157 | Open SimpleStatements } ${ $0 = $<2; }$
158 | : Statementlist ${ $0 = $2; $2 = NULL; }$
160 SimpleStatements -> SimpleStatements ; SimpleStatement ${
162 struct statement **s;
170 | SimpleStatement ${ $0 = $<1; }$
171 | SimpleStatements ; ${ $0 = $<1; }$
173 SimpleStatement -> Factor = Expression ${
174 $0 = calloc(1, sizeof(struct statement));
175 $0->expr = calloc(1, sizeof(struct expression));
176 $0->expr->left = $<1;
177 $0->expr->op = $2.txt;
178 $0->expr->right = $<3;
180 Statement -> SimpleStatements NEWLINE ${
183 | IfStatement ${ $0 = $<1; }$
184 | Statement NEWLINE ${ $0 = $<1; }$
186 IfStatement -> if Expression Block OptNL ${
187 $0 = calloc(1, sizeof(struct statement));
191 | if Expression : SimpleStatements ${
192 $0 = calloc(1, sizeof(struct statement));
196 | if Expression Block OptNL else Block ${
197 $0 = calloc(1, sizeof(struct statement));
202 | if Expression Block OptNL else : SimpleStatements ${
203 $0 = calloc(1, sizeof(struct statement));
208 | if Expression Block OptNL else IfStatement ${
209 $0 = calloc(1, sizeof(struct statement));
216 Expression -> Expression + Term ${
217 $0 = calloc(1, sizeof(struct expression));
222 | Expression - Term ${
223 $0 = calloc(1, sizeof(struct expression));
228 | Term ${ $0 = $1; $1 = NULL; }$
229 Term -> Term * Factor ${
230 $0 = calloc(1, sizeof(struct expression));
236 $0 = calloc(1, sizeof(struct expression));
241 | Factor ${ $0 = $1; $1 = NULL; }$
242 Factor -> IDENTIFIER ${
243 $0 = calloc(1, sizeof(struct expression));
252 hello = yes; mister = no;
273 if true {yes=x;} else : no=x
274 if true: yes = no; no = yes;