-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParser.y
169 lines (137 loc) · 3.27 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
%{
#include "ParseTree.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
extern "C" int yylex();
extern "C" int yyparse();
extern "C" void yyerror(char *s);
// this is the final parse tree that is returned
struct AndList *final;
%}
// this stores all of the types returned by production rules
%union {
struct Operand *myOperand;
struct ComparisonOp *myComparison;
struct OrList *myOrList;
struct AndList *myAndList;
char *actualChars;
}
%token OR
%token AND
%token <actualChars> Name
%token <actualChars> String
%token <actualChars> Float
%token <actualChars> Int
%type <myOrList> OrList
%type <myAndList> AndList
%type <myComparison> Condition
%type <myOperand> Literal
%type <myComparison> Op
%start AndList
//******************************************************************************
// SECTION 3
//******************************************************************************
/* This is the PRODUCTION RULES section which defines how to "understand" the
* input language and what action to take for each "statment"
*/
%%
AndList: '(' OrList ')' AND AndList
{
// here we need to pre-pend the OrList to the AndList
// first we allocate space for this node
$$ = (struct AndList *) malloc (sizeof (struct AndList));
final = $$;
// hang the OrList off of the left
$$->left = $2;
// hang the AndList off of the right
$$->rightAnd = $5;
}
| '(' OrList ')'
{
// just return the OrList!
$$ = (struct AndList *) malloc (sizeof (struct AndList));
final = $$;
$$->left = $2;
$$->rightAnd = NULL;
}
;
OrList: Condition OR OrList
{
// here we have to hang the condition off the left of the OrList
$$ = (struct OrList *) malloc (sizeof (struct OrList));
$$->left = $1;
$$->rightOr = $3;
}
| Condition
{
// nothing to hang off of the right
$$ = (struct OrList *) malloc (sizeof (struct OrList));
$$->left = $1;
$$->rightOr = NULL;
}
;
Condition: Literal Op Literal
{
// in this case we have a simple literal/variable comparison
$$ = $2;
$$->left = $1;
$$->right = $3;
}
| Literal
{
$$ = (struct ComparisonOp *) malloc (sizeof (struct ComparisonOp));
$$->code = EQUALS;
$$->left = $1;
$$->right = $1;
};
Op: '<'
{
// construct and send up the comparison
$$ = (struct ComparisonOp *) malloc (sizeof (struct ComparisonOp));
$$->code = LESS_THAN;
}
| '>'
{
// construct and send up the comparison
$$ = (struct ComparisonOp *) malloc (sizeof (struct ComparisonOp));
$$->code = GREATER_THAN;
}
| '='
{
// construct and send up the comparison
$$ = (struct ComparisonOp *) malloc (sizeof (struct ComparisonOp));
$$->code = EQUALS;
}
;
Literal : String
{
// construct and send up the operand containing the string
$$ = (struct Operand *) malloc (sizeof (struct Operand));
$$->code = STRING;
$$->value = $1;
}
| Float
{
// construct and send up the operand containing the FP number
$$ = (struct Operand *) malloc (sizeof (struct Operand));
$$->code = DOUBLE;
$$->value = $1;
}
| Int
{
// construct and send up the operand containing the integer
$$ = (struct Operand *) malloc (sizeof (struct Operand));
$$->code = INT;
$$->value = $1;
}
| Name
{
// construct and send up the operand containing the name
$$ = (struct Operand *) malloc (sizeof (struct Operand));
$$->code = NAME;
$$->value = $1;
}
;
%%