-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
211 lines (182 loc) · 5.07 KB
/
parser.y
File metadata and controls
211 lines (182 loc) · 5.07 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* externs from Flex */
extern int line_num;
extern int col_num;
extern FILE *yyin;
int yylex(void);
void yyerror(const char *s);
/* AST Node Types */
typedef enum {
NODE_INTEGER,
NODE_VARIABLE,
NODE_BINARY_OP,
NODE_ASSIGNMENT
} NodeType;
/* AST Node Structure */
typedef struct ASTNode {
NodeType type;
union {
int int_value;
char *var_name;
struct {
char op;
struct ASTNode *left;
struct ASTNode *right;
} binary_op;
struct {
char *var_name;
struct ASTNode *expr;
} assignment;
} data;
} ASTNode;
/* Function declarations */
ASTNode* create_integer_node(int value);
ASTNode* create_variable_node(char *name);
ASTNode* create_binary_op_node(char op, ASTNode *left, ASTNode *right);
ASTNode* create_assignment_node(char *var_name, ASTNode *expr);
void print_ast(ASTNode *node, int indent);
void free_ast(ASTNode *node);
%}
/* Union for semantic values */
%union {
int int_val;
char *str_val;
struct ASTNode *node;
}
/* Tokens */
%token <int_val> INTEGER
%token <str_val> VARIABLE
%token ASSIGN PLUS MINUS MULT DIV LPAREN RPAREN
/* Non-terminals */
%type <node> statement expression term factor integer
/* Operator precedence */
%left PLUS MINUS
%left MULT DIV
%start input
%%
/* ===================== GRAMMAR ===================== */
input:
/* empty */
| input statement '\n'
| input statement
;
statement:
VARIABLE ASSIGN expression {
ASTNode *stmt = create_assignment_node($1, $3);
printf("\n=== Abstract Syntax Tree ===\n");
print_ast(stmt, 0);
free_ast(stmt);
}
| expression {
ASTNode *expr = $1;
printf("\n=== Abstract Syntax Tree ===\n");
print_ast(expr, 0);
free_ast(expr);
}
;
expression:
term
| expression PLUS term { $$ = create_binary_op_node('+', $1, $3); }
| expression MINUS term { $$ = create_binary_op_node('-', $1, $3); }
;
term:
factor
| term MULT factor { $$ = create_binary_op_node('*', $1, $3); }
| term DIV factor { $$ = create_binary_op_node('/', $1, $3); }
;
factor:
LPAREN expression RPAREN { $$ = $2; }
| integer
| VARIABLE { $$ = create_variable_node($1); }
;
integer:
INTEGER { $$ = create_integer_node($1); }
;
%%
/* ===================== IMPLEMENTATION ===================== */
ASTNode* create_integer_node(int value) {
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_INTEGER;
node->data.int_value = value;
return node;
}
ASTNode* create_variable_node(char *name) {
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_VARIABLE;
node->data.var_name = strdup(name);
return node;
}
ASTNode* create_binary_op_node(char op, ASTNode *left, ASTNode *right) {
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_BINARY_OP;
node->data.binary_op.op = op;
node->data.binary_op.left = left;
node->data.binary_op.right = right;
return node;
}
ASTNode* create_assignment_node(char *var_name, ASTNode *expr) {
ASTNode *node = malloc(sizeof(ASTNode));
node->type = NODE_ASSIGNMENT;
node->data.assignment.var_name = strdup(var_name);
node->data.assignment.expr = expr;
return node;
}
void print_ast(ASTNode *node, int indent) {
if (!node) return;
for (int i = 0; i < indent; i++) printf(" ");
switch (node->type) {
case NODE_INTEGER:
printf("INTEGER: %d\n", node->data.int_value);
break;
case NODE_VARIABLE:
printf("VARIABLE: %s\n", node->data.var_name);
break;
case NODE_BINARY_OP:
printf("OPERATOR: %c\n", node->data.binary_op.op);
print_ast(node->data.binary_op.left, indent + 1);
print_ast(node->data.binary_op.right, indent + 1);
break;
case NODE_ASSIGNMENT:
printf("ASSIGNMENT: %s =\n", node->data.assignment.var_name);
print_ast(node->data.assignment.expr, indent + 1);
break;
}
}
void free_ast(ASTNode *node) {
if (!node) return;
switch (node->type) {
case NODE_VARIABLE:
free(node->data.var_name);
break;
case NODE_BINARY_OP:
free_ast(node->data.binary_op.left);
free_ast(node->data.binary_op.right);
break;
case NODE_ASSIGNMENT:
free(node->data.assignment.var_name);
free_ast(node->data.assignment.expr);
break;
case NODE_INTEGER:
break;
}
free(node);
}
void yyerror(const char *s) {
fprintf(stderr, "Parse error at line %d, column %d: %s\n", line_num, col_num, s);
}
int main(int argc, char **argv) {
if (argc > 1) {
FILE *file = fopen(argv[1], "r");
if (!file) {
fprintf(stderr, "Error: Cannot open file '%s'\n", argv[1]);
return 1;
}
yyin = file;
}
printf("Enter expressions (Ctrl+D to end):\n");
yyparse();
return 0;
}