-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
81 lines (75 loc) · 1.67 KB
/
main.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
#include<iostream>
#include<cstring>
#include<string>
#include"LexicalAnalyzer.class.h"
#include"SyntacticAnalyzer.class.h"
int main(int argc,char* argv[])
{
LexicalAnalyzer lexical_analyzer;
SyntacticAnalyzer syntactic_analyzer;
std::string outputfilename="",inputfilename="";
int i;
for(i=1;i<argc;++i)
{
if(!strcmp(argv[i],"-o"))
{
if(i+1<argc)
{
outputfilename=argv[i+1];
++i;
}
else
{
std::cerr << "没有输出文件" << std::endl;
return 1;
}
}
else if(inputfilename=="")
{
inputfilename=argv[i];
}
else
{
std::cerr << argv[i] << ": 多余的参数" << std::endl;
return 1;
}
}
if(inputfilename=="")
{
lexical_analyzer.open();
}
else if(!lexical_analyzer.open(inputfilename))
{
std::cerr << inputfilename << ": 文件打开时出错" << std::endl;
return 1;
}
if(syntactic_analyzer.analyze(lexical_analyzer))
{
std::cout << "语法正确" << std::endl;
if(outputfilename!="")
{
std::ofstream file(outputfilename.c_str(),std::ios::out);
if(file.fail())
{
std::cerr << outputfilename << ": 文件打开时出错" << std::endl;
return 1;
}
syntactic_analyzer.print_intermediate_code(file);
file.close();
}
else
{
syntactic_analyzer.print_intermediate_code(std::cout);
}
}
else
{
std::cerr << "语法错误\n"
<< "错误行数: " << lexical_analyzer.get_prevline() << '\n'
<< "错误列数: " << lexical_analyzer.get_prevchar() << '\n'
<< "错误内容: " << lexical_analyzer.getprevsymword() << '\n'
<< "错误原因:" << syntactic_analyzer.get_error_info() << std::endl;
return 1;
}
return 0;
}