diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | grammar.peg | 36 |
2 files changed, 26 insertions, 15 deletions
@@ -1,11 +1,12 @@ CS=parser.c ast.c +HS=ast.h parser.h CFLAGS=-Wall -Wpedantic -std=c99 -badthing: $(CS) +badthing: $(CS) $(HS) $(CC) $(CFLAGS) -o badthing $(CS) parser.c: grammar.peg - minipeg -o parser.c grammar.peg + packcc -o parser grammar.peg clean: rm badthing parser.c diff --git a/grammar.peg b/grammar.peg index fc7db48..bbb5baa 100644 --- a/grammar.peg +++ b/grammar.peg @@ -1,27 +1,37 @@ -%{ +%source { #include <stdio.h> #include <stdlib.h> -#include "ast.h" +#include <string.h> +} -AstVec numbers; -%} +%common { +#include "ast.h" +} +%value "AstNode" -Numberlist = ( n:NUMBER { astvec_append(&numbers, astnode_new_num(n)); } - )+ +numberlist <- { $$ = astnode_new_list(); } + ( n:number _ { astnode_append(&$$, n); } ) + + EOL -NUMBER = < [0-9]+ > [ \t]* { $$ = atoi(yytext); } +number <- < [0-9]+ > { $$ = astnode_new_num(atoi($1)); } +EOL <- '\n' / '\r\n' / '\r' +_ <- [ \t]* %% int main() { - printf("len %ld cap %ld vals %p\n", numbers.len, numbers.cap, numbers.vals); - while (yyparse()) ; - printf("len %ld cap %ld vals %p\n", numbers.len, numbers.cap, numbers.vals); - for (int i = 0; i < numbers.len; i++) { - printf("[%d] ",numbers.vals[i].as.num); + pcc_context_t *ctx = pcc_create(NULL); + AstNode ret; + memset(&ret, 0, sizeof ret); + while (pcc_parse(ctx, &ret)) ; + AstVec *v = &ret.as.list; + printf("%d: %ld %ld\n",ret.ty, v->len,v->cap); + for (int i = 0; i < v->len; i++) { + printf(" %d:[%d]\n", v->vals[i].ty, v->vals[i].as.num); } - + pcc_destroy(ctx); return 0; } + |