我一直在制作一个语言编译器,但是当我运行编译器时:
./compiler file.ln
它不起作用并返回:
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 77) >= this->size() (which is 76)
Aborted (core dumped)
提前谢谢你
顺便说一下,这是词法分析器代码(返回错误的代码):
int main(int argc, char const *argv[]) {
if(argc == 1) {
std::cout << "ERROR: No input file \n\n\n";
return 1;
}
std::vector<Tokens> tokensArr;
std::vector<Token> tokens;
FILE *in=fopen(argv[1],"r");
std::string fileContent;
std::vector<char> splitCode;
char c;
int codeSize = 0;
while((c=fgetc(in))!=EOF) {
codeSize++;
splitCode.push_back(c);
fileContent += c;
}
int index = 0;
tokens.push_back(token("", Tokens::SOF));
while (index != codeSize)
{
char indexChar = splitCode.at(index);
if(indexChar == '(') {
tokens.push_back(*openparen);
} else if (indexChar == ';') {
tokens.push_back(*eol);
} else if(indexChar == ')') {
tokens.push_back(*closedparen);
} else if(indexChar == '+') {
tokens.push_back(*plus);
} else if(indexChar == '-' ) {
tokens.push_back(*minus);
} else if(indexChar == '*' ) {
tokens.push_back(*multiply);
} else if( indexChar == '/') {
tokens.push_back(*divide);
} else {
if(isdigit(indexChar)) {
std::string num = "";
while (index != codeSize && isdigit(indexChar)) {
indexChar = splitCode.at(index);
num += indexChar;
index++;
}
}
}
index++;
}
fclose(in);
std::cout << "\n[";
int i=0;
while(i != tokens.size()) {
std::cout << "{ val: '" << tokens.at(i).val << "' type: " << tokens.at(i).type << " }, ";
i++;
}
std::cout << "]";
std::cout << "\n";
return 0;
}
注意:这里有一些未提及的包含库我尝试制作一个新文件,它确实有效,但是这个文件呢?
使用此文件:
#config
(
"type": "exec",
"syntax": "standard"
);
#endconfig
1 * 2 / 3
它返回了错误,但使用此文件:
let x;
成功了(这些都是我自定义的编程语言)
while (index != codeSize - 1 && isdigit(indexChar)) {
indexChar = splitCode.at(index);
num += indexChar;
index++;
}