-
Notifications
You must be signed in to change notification settings - Fork 5
/
ParserFunc.y
151 lines (118 loc) · 2.67 KB
/
ParserFunc.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
%{
#include "ParseTree.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
extern "C" int yyfunclex();
extern "C" int yyfuncparse();
extern "C" void yyfuncerror(char *s);
// this is the final parse tree that is returned
struct FuncOperator *finalfunc;
%}
// this stores all of the types returned by production rules
%union {
struct FuncOperand *myOperand;
struct FuncOperator *myOperator;
char *actualChars;
char whichOne;
}
%token <actualChars> Name
%token <actualChars> Float
%token <actualChars> Int
%type <myOperand> SimpleExp
%type <myOperator> CompoundExp
%type <whichOne> Op
%start CompoundExp
//******************************************************************************
// SECTION 3
//******************************************************************************
/* This is the PRODUCTION RULES section which defines how to "understand" the
* input language and what action to take for each "statment"
*/
%%
CompoundExp: SimpleExp Op CompoundExp
{
$$ = (struct FuncOperator *) malloc (sizeof (struct FuncOperator));
$$->leftOperator = (struct FuncOperator *) malloc (sizeof (struct FuncOperator));
$$->leftOperator->leftOperator = NULL;
$$->leftOperator->leftOperand = $1;
$$->leftOperator->right = NULL;
$$->leftOperand = NULL;
$$->right = $3;
$$->code = $2;
finalfunc = $$;
}
| '(' CompoundExp ')' Op CompoundExp
{
$$ = (struct FuncOperator *) malloc (sizeof (struct FuncOperator));
$$->leftOperator = $2;
$$->leftOperand = NULL;
$$->right = $5;
$$->code = $4;
finalfunc = $$;
}
| '(' CompoundExp ')'
{
$$ = $2;
finalfunc = $$;
}
| SimpleExp
{
$$ = (struct FuncOperator *) malloc (sizeof (struct FuncOperator));
$$->leftOperator = NULL;
$$->leftOperand = $1;
$$->right = NULL;
finalfunc = $$;
}
| '-' CompoundExp
{
$$ = (struct FuncOperator *) malloc (sizeof (struct FuncOperator));
$$->leftOperator = $2;
$$->leftOperand = NULL;
$$->right = NULL;
$$->code = '-';
finalfunc = $$;
}
;
Op: '-'
{
$$ = '-';
}
| '+'
{
$$ = '+';
}
| '*'
{
$$ = '*';
}
| '/'
{
$$ = '/';
}
;
SimpleExp:
Float
{
// construct and send up the operand containing the FP number
$$ = (struct FuncOperand *) malloc (sizeof (struct FuncOperand));
$$->code = DOUBLE;
$$->value = $1;
}
| Int
{
// construct and send up the operand containing the integer
$$ = (struct FuncOperand *) malloc (sizeof (struct FuncOperand));
$$->code = INT;
$$->value = $1;
}
| Name
{
// construct and send up the operand containing the name
$$ = (struct FuncOperand *) malloc (sizeof (struct FuncOperand));
$$->code = NAME;
$$->value = $1;
}
;
%%