From d28f04f44efffd61564951456c061ba14e1921eb Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 10 Jul 2023 21:34:00 +0100 Subject: new grammar using packcc --- Makefile | 5 +++-- grammar.peg | 36 +++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 71bcbce..012f152 100644 --- a/Makefile +++ b/Makefile @@ -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 #include -#include "ast.h" +#include +} -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; } + -- cgit v1.2.3