summaryrefslogtreecommitdiff
path: root/parse.y
blob: 1810c5105f117f47d026716c82817dd7d17c7112 (plain) (blame)
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
%{

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#include "basic.h"

extern int yylineno;
int yylex();
void yyerror(char *s, ...);

%}

%union {
    float f;
    int i;
    char *s;
    sexpr *u;
}

%token     NIL
%token <f> FLOAT
%token <i> INT
%token <s> ID
%token     BRANCH

%type  <u> sexpr

%%

cmds:
    | cmds sexpr { print_sexpr($2); }

sexpr: '(' sexpr ';' sexpr ')' { $$ = new_node($2, $4); }
     | '(' sexpr     sexpr ')' { $$ = new_node($2, $3); }
     | INT   { $$ = new_int($1); }
     | FLOAT { $$ = new_float($1); }
     | ID    { $$ = new_id($1); }
     | NIL   { $$ = 0; }

%%

void yyerror(char *s, ...) {
    va_list ap;
    va_start(ap, s);

    fprintf(stderr, "%d: error: ", yylineno);
    vfprintf(stderr, s, ap);
    fprintf(stderr, "\n");
}