initial parser commit

This commit is contained in:
Yuichi Nishiwaki 2013-10-11 15:18:37 +09:00
parent 2a6978a3b9
commit 8533d7b87f
3 changed files with 133 additions and 0 deletions

View File

@ -21,6 +21,8 @@ pic_value pic_cdr(pic_state *, pic_value);
pic_value pic_intern_cstr(pic_state *, const char *); pic_value pic_intern_cstr(pic_state *, const char *);
pic_value pic_parse(pic_state *, const char *);
void pic_debug(pic_state *, pic_value); void pic_debug(pic_state *, pic_value);
#endif #endif

106
src/parse.y Normal file
View File

@ -0,0 +1,106 @@
%{
#include <stdlib.h>
#include <stdio.h>
#include "picrin.h"
struct parser_control {
pic_state *pic;
pic_value value;
};
#define YYDEBUG 1
%}
%parse-param {struct parser_control *p}
%lex-param {struct parser_control *p}
%union {
pic_value datum;
}
%token tLPAREN tRPAREN tDOT
%token <datum> tSYMBOL
%type <datum> datum simple_datum symbol compound_datum
%type <datum> list list_tail
%%
program
:
{
p->value = pic_nil_value(p->pic);
}
| datum
{
p->value = $1;
}
;
datum
: simple_datum
| compound_datum
;
simple_datum
: symbol
;
symbol
: tSYMBOL
{
$$ = $1;
}
;
compound_datum
: list
;
list
: tLPAREN list_tail
{
$$ = $2;
}
;
list_tail
: tRPAREN
{
$$ = pic_nil_value();
}
| datum tDOT datum tRPAREN
{
$$ = pic_cons(p->pic, $1, $3);
}
| datum list_tail
{
$$ = pic_cons(p->pic, $1, $2);
}
;
%%
int
yyerror(struct parser_control *p, const char *msg)
{
pic_debug(p->pic, yylval.datum);
puts(msg);
abort();
}
pic_value
pic_parse(pic_state *pic, const char *str)
{
struct parser_control p;
p.pic = pic;
yy_scan_string(str);
yyparse(&p);
yylex_destroy();
return p.value;
}

25
src/scan.l Normal file
View File

@ -0,0 +1,25 @@
%{
#include "picrin.h"
#include "y.tab.h"
struct parser_control {
pic_state *pic;
pic_value value;
};
#define YY_DECL int yylex (struct parser_control *p)
%}
%%
"(" return tLPAREN;
")" return tRPAREN;
[a-z]+ { yylval.datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; }
%%
int
yywrap()
{
return 1;
}