在使用Ragel扫描标识符时,需要遵循以下步骤:
identifier = letter (letter | digit)*;
#include<stdio.h>
#include<string.h>
%%{
machine scanner;
identifier = letter (letter | digit)*;
main := |*
identifier => {
printf("Found identifier: %.*s\n", fc - ts, ts);
};
any => {
printf("Unknown character: %c\n", *ts);
};
*|;
}%%
int main() {
const char *input = "hello world123";
const char *p = input;
const char *pe = input + strlen(input);
const char *eof = pe;
%% write data;
%% write init;
%% write exec;
return 0;
}
ragel -C -o scanner.c scanner.rl
gcc -o scanner scanner.c
./scanner
Found identifier: hello
Unknown character:
Found identifier: world123
通过以上步骤,可以使用Ragel正确扫描标识符。
领取专属 10元无门槛券
手把手带您无忧上云