gpt4 book ai didi

C++ 将字符数组解析为脚本文件(语法)

转载 作者:太空宇宙 更新时间:2023-11-04 11:27:51 25 4
gpt4 key购买 nike

我用 C++ 制作了一个简单的脚本阅读类,它允许我阅读和解析脚本。基本上有一个 FILE 类,然后我继续使用“fopen”打开它。

在函数中,我继续调用“fgetc”和“ftell”以根据需要解析脚本文件,请注意这不是解释器。每个脚本文件都应该遵循某种语法,但这就是我在这里寻求解决方案的原因。

这是一个脚本的样子:

# Script File Comment
USERNAME = "Joe"
PASSWORD = "pw0001"
ACCESSLEVEL = 3

DATABASE = ("localhost",3306,"db","user","password")

基本上我有几个功能:

// This function searches for "variables"
nextToken();

// After I have the variable, e.g: USERNAME, PASSWORD, ACCESSLEVEL or DATABASE
// I proceed to call this function
// This function reads the char array for (,-{}()[]=) these are symbols
readSymbol();

// In a condition I check what "token/variable" I got and proceed to read
// it accordingly
// e.g; for USERNAME I do:
readString(); // reads text inside "
// e.g; for ACCESSLEVEL I do:
readNumber(); // reads digits until the next char ain't a digit
// e.g; for DATABASE I do:
readSymbol(); // (
readString(); // 127.0.0.1
readSymbol(); // ,
readNumber(); // 3306
readSymbol(); // ,
readString(); // db
readSymbol(); // ,
readString(); // user
readSymbol(); // ,
readString(); // password
readSymbol(); // )

我希望能够读取这样的变量声明:

DATABASELIST = {"data1","data2","data3"}
or
DATABASELIST = {"data1"}

我可以轻松地执行 readSymbol 和 readString 来读取变量内的 3 个不同的字符串定义,但是这个列表应该具有自定义用户数据,例如 5 个不同的字符串,或 8 个不同的字符串 - 取决于。

而且我真的不知道如何使用我编写的解析器来做到这一点。请注意,我是基于我从脚本编写者那里获取的这种格式的一些伪代码,我有从 IDA 中提取的伪代码,如果你想看到它以便更好地理解这里的帖子

这是我的“readSymbol”函数的示例。

READSYMBOL

int TReadScriptFile::readSymbol()
{
int currentData = 0;
int stringStart = -1;

// Check if we can't read anymore
if (end)
return 0;

while (true)
{
// Basically get chars in the script
currentData = fgetc(File);

// Check for end of file
if (currentData == -1)
{
end = true;
break;
}

if (stringStart == -1)
{
if (isdigit(currentData) || isalpha(currentData))
{
printf("TReadScriptFile::readSymbol: Symbol expected\n");
close();
return 0;
}
else if
(
currentData == '=' || currentData == ',' ||
currentData == '(' || currentData == ')' ||
currentData == '{' || currentData == '}' ||
currentData == '>' || currentData == '<' ||
currentData == ':' || currentData == '-'
)
{
#ifdef __DEBUG__
printf("Symbol: %c\n", currentData);
#endif
stringStart = ftell(File);
break;
}
}
}

return 1;
}

NEXTTOKEN

int TReadScriptFile::nextToken()
{
int currentData = 0;
int stringStart = -1;
int stringEnd = -1;
RecursionDepth = -1;
memset(String, 0, 4000);

// Check if we can't read anymore
if (end)
return 0;

while (true)
{
// ** Syntax **

if (isdigit(getNext()) || getNext() == -1)
{
printf("No more tokens left.\n");
end = true;
close();
return 0;
}
// End
// Basically get chars in the script
currentData = fgetc(File);

// Check for end of file
if (currentData == -1)
{
end = true;
break;
}

// Syntax Checking Part, this really isn't needed but w/e
if (stringStart == -1)
{
if (currentData == '=' || isdigit(currentData))
{
printf("TReadScriptFile::nextToken: Syntax Error: string expected\n");
close();
return 0;
}
}

// End Syntax Checking

// It's a comment line, we should skip
if (currentData == '#')
{
seekNewLn();
continue;
}

// There are no variables, yet
if (stringStart == -1)
{
// We found a letter, we are near a token!
if (isalpha(currentData))
{
stringStart = ftell(File);

// We might as well add the letter to the string
RecursionDepth++;
String[RecursionDepth] = currentData;
continue;
}
}
else if (stringStart != -1)
{
// Let's wait until we get an identifier or space

// We found a digit, error
if (isdigit(currentData))
{
printf("TReadScriptFile::nextToken: string expected\n");
close();
return 0;
}
// We found a space, maybe we should stop looking for tokens?
else if (isspace(currentData))
{
#ifdef __DEBUG__
printf("Token: %s\n", String);
#endif
break;
}

RecursionDepth++;
String[RecursionDepth] = currentData;
}
}

return 1;
}

我在这里找到了一个很好的方法示例:

http://llvm.org/docs/tutorial/LangImpl1.html

最佳答案

一种处理 DATABASE_LIST 的机制是这样的:

找到变量 DATABASE_LIST 后,使用 readSymbol() 读取一个符号,检查它是否是一个 { 然后在循环中执行 readString() 将其添加到 std::vector(或其他一些合适的容器),然后检查 (使用 readSymbol())。如果它是一个 ,(逗号),那么你返回并读取另一个字符串添加到 vector 中等等,直到你最终到达 } 。完成后,您将拥有一个表示 DATABASE_LIST

的字符串 vector (动态数组)

关于C++ 将字符数组解析为脚本文件(语法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25986131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com