-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.y
76 lines (61 loc) · 1.57 KB
/
Parser.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
68
69
70
71
72
73
74
75
76
%{
#include "Symbol.h"
#include "Struct.h"
#include "Nat.h"
#include "Str.h"
#include "Pair.h"
#include "SymbolValue.h"
#include "StructValue.h"
#include "Parser.h"
#include "Lexer.h"
int yyerror(Struct** expr, yyscan_t scanner, const char* msg) {
//fprintf(stderr, "Parser: %s\n", msg);
return 1;
}
%}
%code requires {
typedef void* yyscan_t;
}
%output "Parser.c"
%defines "Parser.h"
%define api.pure
%lex-param { yyscan_t scanner }
%parse-param { Struct** expr }
%parse-param { yyscan_t scanner }
%union {
int value;
char* string;
Symbol name;
Struct* expr;
}
%token TOKEN_LPAREN "("
%token TOKEN_RPAREN ")"
%token TOKEN_LSQBRK "["
%token TOKEN_RSQBRK "]"
%token <value> TOKEN_NUMBER "number"
%token <string> TOKEN_STRING "string"
%token <name> TOKEN_NAME "name"
%type <expr> expr
%type <expr> apply
%type <expr> formArgs
%%
input
: expr { *expr = $1; }
;
expr
: TOKEN_LPAREN apply[A] TOKEN_RPAREN { $$ = $A; }
| TOKEN_LSQBRK TOKEN_NAME[N] formArgs[A] TOKEN_RSQBRK { $$ = structFromNameAndPairs($N, $A); }
| TOKEN_LSQBRK TOKEN_NAME[N] TOKEN_RSQBRK { $$ = structFromName($N); }
| TOKEN_NUMBER { $$ = newNat($1); }
| TOKEN_STRING { $$ = newStr($1); }
| TOKEN_NAME { $$ = newSymbol($1); }
;
formArgs
: expr[H] formArgs[T] { $$ = newPair($H, $T); }
| expr[H] { $$ = newPair($H, NULL); }
;
apply
: apply[L] expr[R] { $$ = newPair($L, $R); }
| expr[L] expr[R] { $$ = newPair($L, $R); }
;
%%