summaryrefslogtreecommitdiff
path: root/grammar.peg
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-07-10 21:34:00 +0100
committerubq323 <ubq323@ubq323.website>2023-07-10 21:34:03 +0100
commitd28f04f44efffd61564951456c061ba14e1921eb (patch)
treee7bb4a68430ea4293e767e3ceef0ca5b8f1f2342 /grammar.peg
parenteeea75fa7820f1a7ee4958b65667d0078e01f3bd (diff)
new grammar using packcc
Diffstat (limited to 'grammar.peg')
-rw-r--r--grammar.peg36
1 files changed, 23 insertions, 13 deletions
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;
}
+