blob: 29031a44c6d27c013e958ae291760e5950af6c4c (
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "lisp.h"
int yylex(void);
int yyerror(const char *s);
static int is_integer(double x) {
return floor(x) == x && isfinite(x);
}
%}
%union {
int ival;
double dval;
const char *strval;
value *val;
}
%token <dval> NUMBER
%token <strval> STRING
%token <ival> LPAREN RPAREN DOT WHITESPACE
%token <ival> PLUS MINUS MULT DIV
%type <val> sexpr atom sexprs
%start sexprs
%%
opt_ws: /* matches no whitespace */
| WHITESPACE;
sexprs: sexpr
| sexprs opt_ws sexpr;
sexpr: atom
| LPAREN opt_ws sexpr opt_ws DOT opt_ws sexpr opt_ws RPAREN
{
$$ = cons($3, $7);
print_val($$);
printf("\n");
};
atom: NUMBER
{
if(is_integer($1)) {
$$ = make_int($1);
} else {
$$ = make_double($1);
}
}
| LPAREN opt_ws RPAREN { $$ = make_nil(); }
| STRING { $$ = make_string($1); };
%%
int yyerror(const char *s) {
printf("%s\n",s);
}
|