forked from evantypanski/ectlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ypp
64 lines (53 loc) · 1.4 KB
/
parser.ypp
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
%{
#include <assert.h>
#include <iostream>
#include "nodes/node.h"
#include "nodes/minusnode.h"
#include "nodes/plusnode.h"
#include "nodes/multnode.h"
#include "nodes/divnode.h"
#include "nodes/expnode.h"
#include "nodes/integernode.h"
#include "nodes/floatnode.h"
#include "nodes/programnode.h"
#include "nodes/statementnode.h"
#include "scanner.c"
using namespace std;
extern "C" int yylex(void);
extern int yylineno;
void yyerror(char *s) {
cerr << s << endl;
}
ProgramNode *program;
%}
%union{
int intVal;
float floatVal;
class Node *node;
class ExpNode *expNode;
class StatementNode *statementNode;
}
%start program
%token <intVal> INTEGER_LITERAL
%token <floatVal> FLOAT_LITERAL
%token SEMI
%type <expNode> exp
%type <statementNode> statement
%type <node> program
%left PLUS MINUS
%left MULT DIV
%%
program: { program = new ProgramNode(yylineno); }
| program statement { assert(program); program->addStatement($2); }
;
statement: exp SEMI { $$ = new StatementNode(yylineno, $1); }
;
exp:
INTEGER_LITERAL { $$ = new IntegerNode(yylineno, $1); }
| FLOAT_LITERAL { $$ = new FloatNode(yylineno, $1); }
| exp PLUS exp { $$ = new PlusNode(yylineno, $1, $3); }
| exp MINUS exp { $$ = new MinusNode(yylineno, $1, $3); }
| exp MULT exp { $$ = new MultNode(yylineno, $1, $3); }
| exp DIV exp { $$ = new DivNode(yylineno, $1, $3); }
;
%%