Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我的第一杯Ragel_C_Ragel - Fatal编程技术网

C 我的第一杯Ragel

C 我的第一杯Ragel,c,ragel,C,Ragel,我正在写我的第一个Ragel程序。我的目标是写一个四功能计算器。请不要给我你的代码。这对我来说是一次学习经历 我要做的是将正则表达式与浮点匹配,并打印出值。Ragel程序和C/CPP代码可以编译,但我的返回值始终为零,并且从未执行print语句。下面是我的代码。我做错了什么 /* * This is a four function calculator. */ #include <string.h> #include <stdio.h> #include <s

我正在写我的第一个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;
}
/*
*这是一个四功能计算器。
*/
#包括
#包括
#包括
%%{
机器计算器;
写入数据;
}%%
浮点计算器(字符*字符串)
{
int cs;
char*p;
char*pe;
char*eof;
int法案;
字符*ts;
char*te;
浮动值;
整数长度;
长度=strlen(字符串);
字符串[长度-1]=0;
%%{ 
行动获得价值{
值=atof(字符串);
}
动作指令错误{
printf(“错误\n”);
fhold;
}
main:=([0-9])@get_值;
#初始化并执行。
写初始化;
写exec;
}%%
返回值;
};
#定义bufsize1024
int main()
{
字符buf[BUFSIZE];
浮动增值税;
val=0.0;
while(fgets(buf,sizeof(buf),stdin)!=0){
val=计算器(buf);
printf(“%f\n”,val);
}
返回0;
}

您没有使用要解析的内容设置char*p缓冲区。这就是为什么您的测试没有输出


Ragel需要知道要解析的数据位于何处。char*p必须指向在线报告的第一个示例第6页中所述的要分析的数据

您不仅需要将
p
设置为指向缓冲区的开始,还应该将
pe
eof
指向缓冲区的结尾。此代码应在指定长度后立即覆盖:

p = string;
pe = string + length - 1;
eof = pe;
注意:
act
ts
te
在这种情况下不需要,因为这些变量仅用于扫描仪