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 --- grammar.peg | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'grammar.peg') 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