diff options
author | ubq323 <ubq323@ubq323.website> | 2023-07-10 21:34:00 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2023-07-10 21:34:03 +0100 |
commit | d28f04f44efffd61564951456c061ba14e1921eb (patch) | |
tree | e7bb4a68430ea4293e767e3ceef0ca5b8f1f2342 | |
parent | eeea75fa7820f1a7ee4958b65667d0078e01f3bd (diff) |
new grammar using packcc
-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; } + |