2013-10-11 02:18:37 -04:00
|
|
|
%{
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2013-10-19 23:34:57 -04:00
|
|
|
#include "picrin/pair.h"
|
2013-10-11 02:18:37 -04:00
|
|
|
|
2013-10-17 04:57:12 -04:00
|
|
|
#define YYERROR_VERBOSE 1
|
|
|
|
|
2013-10-11 02:18:37 -04:00
|
|
|
struct parser_control {
|
|
|
|
pic_state *pic;
|
|
|
|
pic_value value;
|
2013-10-17 07:48:50 -04:00
|
|
|
bool incomp;
|
2013-10-11 02:18:37 -04:00
|
|
|
};
|
|
|
|
|
2013-10-19 14:11:08 -04:00
|
|
|
void init_scanner(const char *);
|
|
|
|
void final_scanner(void);
|
|
|
|
|
|
|
|
void yyerror(struct parser_control *, const char *);
|
|
|
|
int yylex(struct parser_control *);
|
2013-10-11 02:18:37 -04:00
|
|
|
%}
|
|
|
|
|
|
|
|
%parse-param {struct parser_control *p}
|
|
|
|
%lex-param {struct parser_control *p}
|
|
|
|
|
|
|
|
%union {
|
|
|
|
pic_value datum;
|
|
|
|
}
|
|
|
|
|
|
|
|
%token tLPAREN tRPAREN tDOT
|
2013-10-16 00:17:01 -04:00
|
|
|
%token <datum> tSYMBOL tNUMBER tBOOLEAN
|
2013-10-11 02:18:37 -04:00
|
|
|
|
|
|
|
%type <datum> datum simple_datum symbol compound_datum
|
2013-10-19 14:05:42 -04:00
|
|
|
%type <datum> number boolean list list_data
|
2013-10-11 02:18:37 -04:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
program
|
|
|
|
:
|
|
|
|
{
|
2013-10-17 07:48:50 -04:00
|
|
|
p->value = pic_undef_value();
|
2013-10-11 02:18:37 -04:00
|
|
|
}
|
|
|
|
| datum
|
|
|
|
{
|
|
|
|
p->value = $1;
|
|
|
|
}
|
2013-10-17 07:48:50 -04:00
|
|
|
| incomplete_datum
|
|
|
|
{
|
|
|
|
p->incomp = true;
|
|
|
|
p->value = pic_undef_value();
|
|
|
|
}
|
2013-10-11 02:18:37 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
datum
|
|
|
|
: simple_datum
|
|
|
|
| compound_datum
|
|
|
|
;
|
|
|
|
|
|
|
|
simple_datum
|
|
|
|
: symbol
|
2013-10-11 23:07:28 -04:00
|
|
|
| number
|
2013-10-16 00:17:01 -04:00
|
|
|
| boolean
|
2013-10-11 02:18:37 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
symbol
|
|
|
|
: tSYMBOL
|
|
|
|
{
|
|
|
|
$$ = $1;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2013-10-11 23:07:28 -04:00
|
|
|
number
|
2013-10-15 07:05:12 -04:00
|
|
|
: tNUMBER
|
2013-10-11 23:07:28 -04:00
|
|
|
;
|
|
|
|
|
2013-10-16 00:17:01 -04:00
|
|
|
boolean
|
|
|
|
: tBOOLEAN
|
|
|
|
;
|
|
|
|
|
2013-10-11 02:18:37 -04:00
|
|
|
compound_datum
|
|
|
|
: list
|
|
|
|
;
|
|
|
|
|
|
|
|
list
|
2013-10-17 05:14:18 -04:00
|
|
|
: tLPAREN list_data tRPAREN
|
2013-10-11 02:18:37 -04:00
|
|
|
{
|
|
|
|
$$ = $2;
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2013-10-17 05:14:18 -04:00
|
|
|
list_data
|
|
|
|
: /* none */
|
2013-10-11 02:18:37 -04:00
|
|
|
{
|
|
|
|
$$ = pic_nil_value();
|
|
|
|
}
|
2013-10-17 05:14:18 -04:00
|
|
|
| datum tDOT datum
|
2013-10-11 02:18:37 -04:00
|
|
|
{
|
|
|
|
$$ = pic_cons(p->pic, $1, $3);
|
|
|
|
}
|
2013-10-17 05:14:18 -04:00
|
|
|
| datum list_data
|
2013-10-11 02:18:37 -04:00
|
|
|
{
|
|
|
|
$$ = pic_cons(p->pic, $1, $2);
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
2013-10-17 07:48:50 -04:00
|
|
|
incomplete_datum
|
|
|
|
: tLPAREN incomplete_data
|
|
|
|
;
|
|
|
|
|
|
|
|
incomplete_data
|
|
|
|
: /* none */
|
|
|
|
| datum tDOT
|
|
|
|
| datum incomplete_datum
|
|
|
|
| datum tDOT incomplete_datum
|
|
|
|
| datum incomplete_data
|
|
|
|
;
|
|
|
|
|
2013-10-11 02:18:37 -04:00
|
|
|
%%
|
|
|
|
|
2013-10-19 14:05:42 -04:00
|
|
|
void
|
2013-10-11 02:18:37 -04:00
|
|
|
yyerror(struct parser_control *p, const char *msg)
|
|
|
|
{
|
|
|
|
puts(msg);
|
|
|
|
}
|
|
|
|
|
2013-10-17 07:48:50 -04:00
|
|
|
bool
|
|
|
|
pic_parse(pic_state *pic, const char *str, pic_value *v)
|
2013-10-11 02:18:37 -04:00
|
|
|
{
|
|
|
|
struct parser_control p;
|
|
|
|
|
|
|
|
p.pic = pic;
|
2013-10-17 07:48:50 -04:00
|
|
|
p.incomp = false;
|
2013-10-11 02:18:37 -04:00
|
|
|
|
2013-10-19 14:11:08 -04:00
|
|
|
init_scanner(str);
|
2013-10-11 02:18:37 -04:00
|
|
|
yyparse(&p);
|
2013-10-19 14:11:08 -04:00
|
|
|
final_scanner();
|
2013-10-11 02:18:37 -04:00
|
|
|
|
2013-10-17 04:57:12 -04:00
|
|
|
if (yynerrs > 0) {
|
|
|
|
p.value = pic_undef_value();
|
|
|
|
}
|
|
|
|
|
2013-10-17 07:48:50 -04:00
|
|
|
if (! p.incomp) {
|
|
|
|
*v = p.value;
|
|
|
|
}
|
|
|
|
return ! p.incomp;
|
2013-10-11 02:18:37 -04:00
|
|
|
}
|