From 8533d7b87f0a54e4c70094c35b5ebd75ed6f0ff8 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 11 Oct 2013 15:18:37 +0900 Subject: [PATCH] initial parser commit --- include/picrin.h | 2 + src/parse.y | 106 +++++++++++++++++++++++++++++++++++++++++++++++ src/scan.l | 25 +++++++++++ 3 files changed, 133 insertions(+) create mode 100644 src/parse.y create mode 100644 src/scan.l diff --git a/include/picrin.h b/include/picrin.h index f8346e4e..b5c6fece 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -21,6 +21,8 @@ pic_value pic_cdr(pic_state *, pic_value); pic_value pic_intern_cstr(pic_state *, const char *); +pic_value pic_parse(pic_state *, const char *); + void pic_debug(pic_state *, pic_value); #endif diff --git a/src/parse.y b/src/parse.y new file mode 100644 index 00000000..37bb0ded --- /dev/null +++ b/src/parse.y @@ -0,0 +1,106 @@ +%{ +#include +#include + +#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 tSYMBOL + +%type datum simple_datum symbol compound_datum +%type 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; +} diff --git a/src/scan.l b/src/scan.l new file mode 100644 index 00000000..7a11d825 --- /dev/null +++ b/src/scan.l @@ -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; +}