-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCparser.cpp
98 lines (90 loc) · 2.31 KB
/
Cparser.cpp
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
/****************************************************/
/* File: main.c */
/* Main program for CMINUS compiler */
/* Compiler Construction: Principles and Practice */
/* Kenneth C. Louden */
/****************************************************/
#include "globals.h"
/* set NO_PARSE to TRUE to get a scanner-only compiler */
#define NO_PARSE FALSE
/* set NO_ANALYZE to TRUE to get a parser-only compiler */
#define NO_ANALYZE TRUE
/* set NO_CODE to TRUE to get a compiler that does not
* generate code
*/
#define NO_CODE TRUE
#include "util.h"
#if NO_PARSE
#include "scan.h"
#else
#include "parse.h"
#if !NO_ANALYZE
#include "analyze.h"
#if !NO_CODE
#include "cgen.h"
#endif
#endif
#endif
using namespace std;
/* allocate global variables */
int lineno = 0;
FILE* source;
FILE* listing;
FILE* code;
/* allocate and set tracing flags */
int EchoSource = FALSE;
int TraceScan = FALSE;
int TraceParse = TRUE;
int TraceAnalyze = FALSE;
int TraceCode = FALSE;
int Error = FALSE;
int main(int argc, char* argv[]) {
TreeNode* syntaxTree;
char pgm[120]; /* source code file name */
char* pgm_output = NULL;
/*memset(pgm_output, 0, sizeof(pgm_output));
memset(pgm, 0, sizeof(pgm));*/
if (argc != 2) {
fprintf(stderr,"usage: %s <filename>\n",argv[0]);
exit(1);
}
strcpy(pgm,argv[1]);
if (strchr (pgm, '.') == NULL)
strcat(pgm,".c-");
int len = strlen(pgm);
if (strchr(pgm, '.') == NULL) {
pgm_output = (char*)malloc(len+5);
strcpy(pgm_output, pgm);
strcat(pgm, ".c-");
strcat(pgm_output, ".txt");
}
else {//将.c-替换成.txt
pgm_output = (char*)malloc(len+2);
strcpy(pgm_output, pgm);
pgm_output[len - 2] = 't';
pgm_output[len-1] = 'x';
strcat(pgm_output, "t");
}
source = fopen(pgm, "r");
if (source == NULL) {
fprintf(stderr, "File %s not found\n", pgm);
exit(1);
}
//strncpy(pgm_output, pgm, strlen(pgm) - 3);
//strcat(outfile, ".txt");
printf("\noutputfile:%s\n", pgm_output);
listing = fopen(pgm_output, "w");
#if NO_PARSE
while (getToken() != ENDFILE);
#else
syntaxTree = parse();
if (TraceParse) {
fprintf(listing, "CMINUS PARSING:\n\n");
fprintf(listing, "Syntax tree:\n");
printTree(syntaxTree);
}
#endif
free(pgm_output);
fclose(source);
return 0;
}