-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.y
67 lines (51 loc) · 1.21 KB
/
parse.y
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
60
61
62
63
64
65
66
67
%locations
%{
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "sexpr.h"
#include "sinc.h"
extern int yylineno;
int yylex(void);
void yyerror(char *s, ...);
sexpr *ast;
%}
%union {
double f;
int i;
char *s;
sexpr *u;
}
%token NIL
%token <f> FLOAT
%token <i> INT
%token <s> ID
%token <s> STRING
%token BRANCH
%type <u> sexpr
%type <u> lexpr
%%
cmds:
| cmds sexpr { handle($2); }
sexpr: '(' sexpr ';' sexpr ')' { $$ = new_node($2, $4, yylloc); }
| '(' sexpr sexpr ')' { $$ = new_node($2, $3, yylloc); }
| '[' lexpr { $$ = $2; }
| INT { $$ = new_int($1, yylloc); }
| FLOAT { $$ = new_float($1, yylloc); }
| ID { $$ = new_id($1, yylloc); }
| NIL { $$ = 0; }
| STRING { $$ = new_str($1, yylloc); }
lexpr: ']' { $$ = 0; }
| sexpr ';' lexpr { $$ = new_node($1, $3, yylloc); }
| sexpr lexpr { $$ = new_node($1, $2, yylloc); }
%%
void yyerror(char *s, ...) {
va_list ap;
va_start(ap, s);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
fprintf(stderr, "%d: error: ", yylineno);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
#pragma clang diagnostic pop
}