-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinteractive_main.c
More file actions
93 lines (76 loc) · 1.8 KB
/
interactive_main.c
File metadata and controls
93 lines (76 loc) · 1.8 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
#include "parser.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern FILE *yyin;
extern int yyparse();
extern void yyrestart(FILE *);
extern int line;
extern int column;
// Function to reset parser state between parses
void reset_parser() {
line = 1;
column = 1;
}
int main(int argc, char *argv[]) {
char buffer[1024];
FILE *temp_file = NULL;
if (argc > 1) {
// Regular file mode
FILE *file = fopen(argv[1], "r");
if (!file) {
fprintf(stderr, "Cannot open file %s\n", argv[1]);
return 1;
}
yyin = file;
printf("Parsing file %s...\n", argv[1]);
yyparse();
fclose(file);
} else {
printf("MiniSoft Interactive Parser\n");
printf("Enter code (type 'exit;' on a new line to quit):\n");
while (1) {
// Create a temporary file for input
temp_file = tmpfile();
if (!temp_file) {
fprintf(stderr, "Error creating temporary file\n");
return 1;
}
printf(">> ");
fflush(stdout);
// Read until "exit;" or EOF
char line_buffer[1024];
int done = 0;
while (!done && fgets(line_buffer, sizeof(line_buffer), stdin)) {
if (strcmp(line_buffer, "exit;\n") == 0) {
done = 1;
break;
}
fputs(line_buffer, temp_file);
// If the line ends with a semicolon, try parsing
if (strchr(line_buffer, ';')) {
break;
}
printf(".. ");
fflush(stdout);
}
if (done) {
break;
}
// EOF encountered
if (feof(stdin)) {
break;
}
// Rewind temp file and parse
rewind(temp_file);
yyin = temp_file;
yyrestart(yyin);
reset_parser();
printf("\n");
yyparse();
printf("\n");
fclose(temp_file);
}
}
return 0;
}