-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
185 lines (161 loc) · 5.66 KB
/
parser.y
File metadata and controls
185 lines (161 loc) · 5.66 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
%{
/*
* parser.y — Bison Parser + Three-Address Code (TAC) Generator
*
* Grammar (simplified):
* stmt → IDENTIFIER '=' expr
* expr → expr '+' term | expr '-' term | term
* term → term '*' factor | term '/' factor | factor
* factor → '(' expr ')' | NUMBER | IDENTIFIER
*
* Compile & Run:
* flex lexer.l
* bison -d parser.y
* gcc lex.yy.c parser.tab.c -o compiler -lfl
* echo "p = i + r * 60" | ./compiler
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ------------------------------------------------------------------ */
/* Declarations from lexer.l */
/* ------------------------------------------------------------------ */
extern int yylex(void);
extern int yylineno;
extern char *yytext;
void yyerror(const char *msg);
/* Symbol table — defined in lexer.l */
typedef struct { char name[64]; char id[8]; char type[8]; } Symbol;
extern Symbol symtab[];
extern int symcount;
extern int sym_index(const char *);
extern int sym_add(const char *, const char *);
extern void print_symtab(void);
/* ------------------------------------------------------------------ */
/* TAC helpers */
/* ------------------------------------------------------------------ */
static int tmpcnt = 0;
static char *make_tmp(void) {
char *t = malloc(8);
snprintf(t, 8, "t%d", ++tmpcnt);
return t;
}
static void emit(const char *result, const char *arg1,
const char *op, const char *arg2) {
if (op && arg2)
printf("%s = %s %s %s\n", result, arg1, op, arg2);
else
printf("%s = %s\n", result, arg1);
}
%}
/* ------------------------------------------------------------------ */
/* Value type for semantic actions */
/* ------------------------------------------------------------------ */
%union {
char *str; /* holds identifier id-string or literal value */
}
/* ------------------------------------------------------------------ */
/* Token declarations */
/* ------------------------------------------------------------------ */
%token <str> IDENTIFIER NUMBER
%token ASSIGN PLUS MINUS MUL DIV LPAREN RPAREN NEWLINE
/* ------------------------------------------------------------------ */
/* Non-terminal types */
/* ------------------------------------------------------------------ */
%type <str> expr term factor
/* ------------------------------------------------------------------ */
/* Operator precedence (low → high) */
/* ------------------------------------------------------------------ */
%left PLUS MINUS
%left MUL DIV
%right UMINUS
%%
/* ================================================================== */
/* Grammar rules */
/* ================================================================== */
program
: /* empty */
| program stmt
;
stmt
: IDENTIFIER ASSIGN expr NEWLINE
{
/* Find the target variable id */
int idx = sym_index($1); /* $1 is already the id string */
/* Walk symtab to find by id */
char lhs[8];
strcpy(lhs, $1); /* $1 is id string from lexer */
emit(lhs, $3, NULL, NULL);
free($1); free($3);
}
| error NEWLINE { yyerrok; }
;
expr
: expr PLUS term
{
char *t = make_tmp();
emit(t, $1, "+", $3);
free($1); free($3);
$$ = t;
}
| expr MINUS term
{
char *t = make_tmp();
emit(t, $1, "-", $3);
free($1); free($3);
$$ = t;
}
| term { $$ = $1; }
;
term
: term MUL factor
{
char *t = make_tmp();
emit(t, $1, "*", $3);
free($1); free($3);
$$ = t;
}
| term DIV factor
{
char *t = make_tmp();
emit(t, $1, "/", $3);
free($1); free($3);
$$ = t;
}
| factor { $$ = $1; }
;
factor
: LPAREN expr RPAREN { $$ = $2; }
| NUMBER { $$ = $1; }
| IDENTIFIER { $$ = $1; }
| MINUS factor %prec UMINUS
{
char *t = make_tmp();
emit(t, "-", $2, NULL); /* unary minus as: t = - x */
free($2);
$$ = t;
}
;
%%
/* ================================================================== */
/* Supporting C code */
/* ================================================================== */
void yyerror(const char *msg) {
fprintf(stderr, "\nParse Error (line %d): %s\n", yylineno, msg);
}
int main(void) {
char buffer[512];
printf("=============================================================================\n");
printf(" Flex + Bison: Symbol Table, Lexical Analysis & TAC Generator\n");
printf("=============================================================================\n");
printf("Enter expression(s). One per line. Ctrl-D to finish.\n");
printf("Example: p = i + r * 60\n\n");
/* ---- Phase 1 header ---- */
printf("--- Lexical Tokens ---\n");
yyparse(); /* drives yylex(); TAC emitted inside rules */
/* ---- Symbol table (populated during lex) ---- */
print_symtab();
printf("\n(TAC emitted above during parsing.)\n");
printf("=============================================================================\n");
return 0;
}