gpt4 book ai didi

c - ragel如何从文件中读取源代码?

转载 作者:行者123 更新时间:2023-12-04 04:58:54 25 4
gpt4 key购买 nike

我不知道 ragel 如何从文件中读取源代码。我看到的所有示例都是从标准输入读取的。

请您给我看一个与 Ragel 接口(interface)的 C 语言示例,其中程序不从标准中读取?

最佳答案

Ragel 期望一些变量在范围内并指向它正在扫描的缓冲区的各个重要部分。您可以在 Ragel User Guide 中了解它期望它们被调用的内容以及如何更改它们。 .第 5 节,主机程序的接口(interface),是你的 friend 。

手册中的一个示例在第一个参数上运行扫描仪:

#include <stdio.h>
#include <string.h>

%%{
machine foo;
write data;
}%%

int main( int argc, char **argv )
{
int cs;
if ( argc > 1 ) {
char *p = argv[1];
char *pe = p + strlen( p );
%%{
main := [0-9]+ ( '.' [0-9]+ )?;
write init;
write exec;
}%%
}
printf("result = %i\n", cs >= foo_first_final );
return 0;
}

基本上 cs保存机器状态和 ppe分别指向缓冲区的开头和结尾。

手册中的定义:

• cs - Current state. This must be an integer and it should persist across invocations of the machine when the data is broken into blocks that are processed independently. This variable may be modied from outside the execution loop, but not from within.

• p - Data pointer. In C/D code this variable is expected to be a pointer to the character data to process. It should be initialized to the beginning of the data block on every run of the machine. In Go, Java and Ruby it is used as an oset to data and must be an integer. In this case it should be initialized to zero on every run of the machine.

• pe - Data end pointer. This should be initialized to p plus the data length on every run of the machine. In Go, Java and Ruby code this should be initialized to the data length.

• eof - End of file pointer. This should be set to pe when the buer block being processed is the last one, otherwise it should be set to null. In Go, Java and Ruby code -1 must be used instead of null. If the EOF event can be known only after the nal buer block has been processed, then it is possible to set p = pe = eof and run the execute block.

• data - This variable is only required in Go, Java and Ruby code. It must be an array containting the data to process.

• stack - This must be an array of integers. It is used to store integer values representing states. If the stack must resize dynamically the Pre-push and Post-Pop statements can be used to do this (Sections 5.6 and 5.7).

• top - This must be an integer value and will be used as an oset to stack, giving the next available spot on the top of the stack.

• act - This must be an integer value. It is a variable sometimes used by scanner code to keep track of the most recent successful pattern match.

• ts - This must be a pointer to character data. In Go, Java and Ruby code this must be an integer. See Section 6.3 for more information.

关于c - ragel如何从文件中读取源代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16398828/

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