我正在写我的第一个Ragel程序。我的目标是编写一个四功能计算器。请不要将您的代码发送给我。这是为了我的学习体验。我的第一杯Ragel
我想要做的是匹配一个正则表达式与一个浮点数并打印出来的值。 Ragel程序和C/CPP代码编译,但是我的返回值始终为零,并且print语句从不执行。以下是我的代码。我做错了什么?
/*
* This is a four function calculator.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
%%{
machine calculator;
write data;
}%%
float calculator(char* string)
{
int cs;
char* p;
char* pe;
char* eof;
int act;
char* ts;
char* te;
float value;
int length;
length = strlen(string);
string[length -1] = 0;
%%{
action get_value {
value = atof(string);
}
action cmd_err {
printf("Error\n");
fhold;
}
main := ([0-9])@get_value;
# Initialize and execute.
write init;
write exec;
}%%
return value;
};
#define BUFSIZE 1024
int main()
{
char buf[BUFSIZE];
float val;
val = 0.0;
while (fgets(buf, sizeof(buf), stdin) != 0) {
val = calculator(buf);
printf("%f\n", val);
}
return 0;
}