我正在尝试编写一个词法分析器程序。但是每次我在第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;
}
有人请帮忙......
答案 0 :(得分:2)
(第2行)presentVariables[100]
最多可容纳100个元素;而varCount
可以更进一步。我尝试presentVariables[1000]
,程序在我的电脑上运行良好(输入很少)。
您应该使用vector<string>
吗?