【发布时间】:2018-04-03 22:53:02
【问题描述】:
我正在尝试编写一个词法分析器程序。但是每次我在第 152 行增加变量“varCount”时,我的程序都会崩溃。
更具体地说,只有当我将其增加超过 1 时它才会崩溃。我尝试重命名它,但它仍然不起作用。我不知道我做错了什么。
这是我的代码
#include<bits/stdc++.h>
using namespace std;
string presentKeyWords[100], presentVariables[100], operators[100] ;
int pkcount=0, varCount=0, opcount=0;
int main()
{
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
string line;
bool s_cmt = 0;
if(inputFile.is_open())
{
while( getline(inputFile, line) )
{
char arrline[line.length()+1] ;
strcpy( arrline, line.c_str() );
/* Removing comments */
// traverse each character of a line
for(int i=0; i<line.length(); i++)
{
if(arrline[i]=='/' && arrline[i+1]=='/'){
s_cmt = 1;
break; // skip if single-line cmt
}
else if(arrline[i]=='/' && arrline[i+1]=='*'){
while( getline(inputFile, line) ){
char arrline[line.length()+1] ;
strcpy( arrline, line.c_str() );
int x = 0;
for(int i=line.length(); i>0; i--){
if(arrline[i]=='/' && arrline[i-1]=='*'){
x = 1;
break;
}
}
if(x==1){
break;
}
}
break;
}
else{
s_cmt = 0;
outputFile << arrline[i];
}
}
/* Detecting variables and keywords */
if(s_cmt==0)
{
string words[100], var;
// break the line into words
int c=0;
istringstream streamOfString(line);
while( getline(streamOfString, var, ' ') ){
words[c]=var;
c++;
}
// loop through the words
for(int i=0; i<c; i++)
{
// Checking for special characters
int specialCharFound = 0;
string listOfOperators[] = { "+", "Plus",
"-", "Minus",
"*", "Multiplication",
"/", "Division",
"=", "Equal",
"~", "Difference",
"!", "Exclamation",
".", "dot",
"@", "at",
"#", "Hash",
"$", "Dollar sign",
"%", "Modulus",
"^", "Carrot",
"&", "AND",
"?", "Question Mark",
"<", "Greater than",
">", "Less than",
",", "Comma",
":", "Colon",
";", "Semicolon"
};
int sn = sizeof(listOfOperators)/sizeof(listOfOperators[0]);
for(int iter=0; iter<sn; iter+=2)
{
if( listOfOperators[iter]==words[i] )
{
operators[opcount] = listOfOperators[iter] ;
operators[opcount+1] = listOfOperators[iter+1] ;
opcount+=2;
specialCharFound = 1;
}
}
if(specialCharFound==1){
continue;
}
/* check for keyWords */
int keyWordFound = 0;
string keyWords[] = {"bool", "int", "float", "char", "if", "for", "while", "string", "break", "continue"};
for(int j=0; j<10; j++){
if(words[i]==keyWords[j]){
presentKeyWords[pkcount] = words[i] ;
pkcount++;
keyWordFound = 1;
break;
}
}
if(keyWordFound==1){
continue;
}
/* check for variables */
if( isdigit(words[i][0]) ){
//cout << words[i] << endl ;
}
else{
string tempVar = words[i] ;
string dType ;
for(int j=0; j<varCount; j++){
if( presentVariables[j]==tempVar ){
dType = presentVariables[j+1];
}
}
if(dType.empty() && pkcount!=0){
dType = presentKeyWords[pkcount-1];
}
presentVariables[varCount] = tempVar ;
presentVariables[varCount+1] = dType ;
varCount+=2;
}
}
}
outputFile << endl;
}
}
outputFile << "\n\n" << "Keywords \n---------- \n";
for(int i=0; i<pkcount; i++){
outputFile << presentKeyWords[i] << endl;
}
outputFile << "\n\n\n" ;
outputFile << "\n\n" << "Variables \n---------- \n";
for(int i=0; i<varCount; i+=2){
outputFile << presentVariables[i] << " = " << presentVariables[i+1] << endl;
}
outputFile << "\n\n" << "Operators \n---------- \n";
for(int i=0; i<opcount; i+=2){
outputFile << operators[i] << " " << operators[i+1] << endl;
}
return 0;
}
请有人帮忙...
【问题讨论】:
-
char arrline[line.length()+1];- VLA 是非标准的供应商扩展。arrline应该是std::string。您的评论搜索代码在两个单独的arrline变量上运行。当你检测到一个起始/*时,你为什么要回去从inputFile读取更多数据?你不应该这样做,你应该在你已经扫描的行中寻找结束的*/。让下一个循环迭代从inputFile读取。为此,请考虑使用string::find()和string::substr()来优化您的搜索,而不是逐个字符地扫描 -
其实你根本不需要
arrline,你可以直接搜索line,特别是你在处理的时候没有修改line -
尝试将嵌套的 for、while、if 等的深度减少到两个。七个太多了。