-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua.y
98 lines (71 loc) · 1.84 KB
/
lua.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/********************************* lua.y ***************************************
Universidade Federal Fluminense
Instituto de Computacao - Departamento de Ciencia da Computacao
TUTORIAL DE COMPILADORES - 2017
Mini Compilador de Lua - luacc
Autor: Augusto C S Sales
*******************************************************************************/
%{
#include <stdio.h>
#include "tree.h"
#include "lex.yy.c"
void yyerror(const char *s);
%}
%union {
TREE *tval;
int ival;
char *sval;
}
%type <tval> lua chunk statlist stat varlist var explist exp
//Tokens
%token <sval> NAME
%token NIL
%token <ival> INTEGER
%start lua
%%
lua:
chunk {$$ = add_node(NULL, $1, NULL, CHUNK); writeCode($$); destroy_tree($$);}
;
chunk:
statlist {$$ = add_node(NULL, $1, NULL, STATLIST);}
;
statlist:
statlist stat {$$ = add_node($1, NULL, $2, STATLIST);}
| stat {$$ = add_node(NULL, $1, NULL, STAT);}
;
stat:
varlist '=' explist {$$ = add_node($1, NULL, $3, ATRIB);}
;
varlist:
var {$$ = add_node(NULL, $1, NULL, VAR);}
;
var:
NAME {$$ = add_node(NULL, NULL, NULL, TNAME);}
;
explist:
exp {$$ = add_node(NULL, $1, NULL, EXP);}
;
exp:
NIL {$$ = add_node(NULL, NULL, NULL, TNIL);}
| INTEGER {$$ = add_node(NULL, NULL, NULL, TINTEGER); set_value($$, yylval.ival);}
;
%%
int main(int argc, char* argv[])
{
// Reads a file OR reads from standard input
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
// parse through the input until the end:
do {
yyparse();
} while (!feof(yyin));
return 0;
}
void yyerror(const char *s) {
printf("EEK, parse error! Message: %s\n", s);
// might as well halt now:
// exit(-1);
}