summaryrefslogtreecommitdiff
path: root/grammar.peg
blob: 12847e568d0975d875da4b7109bf986c6909c434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
%source { 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

}

%common { 
#include "ast.h"
}

%value "AstNode" 

expr <- 
	  l:list { $$ = l; }
	/ n:number { $$ = n; }
	/ s:symbol { $$ = s; }

list <- { $$ = astnode_new_list(); }
	'(' 
	( e:expr { astnode_append(&$$, e); }
	 )*
	')' _ 

number <- < [0-9]+ > _ (! ident_char)  { $$ = astnode_new_num(atoi($1)); }
symbol <- < ident_char+ > _ { $$ = astnode_new_symbol(strdup($1)); }

ident_char <- [-_a-zA-Z'+*0-9]
_ <- [ \t]*

%%

int main() {
	pcc_context_t *ctx = pcc_create(NULL);
	AstNode ret;
	memset(&ret, 0, sizeof ret);
	pcc_parse(ctx, &ret);

	astnode_disp(&ret);
	putchar('\n');

	pcc_destroy(ctx);
	return 0;
}