0

我试图解析一个命令:LoadSdf 12 abc.ldf ,我试图在命令的每个阶段(,,,:LoadSdf12得到错误abc.ldf。但是我得到了不同的行为,如问题的后面部分所示。

以下是我的 Ragel 伪代码

    %%
    machine ldf;

    LOAD_CMD = ":LoadSdf" $!(cmd_error);
    CHANNEL_NR = [digit]+ $!(channel_error);
    FILENAME = [a-zA-Z0-9\.\-\_]+ $!(filename_error);
    SPACE = ' ';

    main:= (LOAD_CMD.SPACE+.CHANNEL_NR.SPACE.FILENAME) %/job_done;
    %%

预期输出:

    ./a.out ":Load"
    incorrect command  //corresponding actions contain these error messages

    ./a.out ":LoadSdf"
    whitespace missing //corresponding actions contain these error messages

    ./a.out ":LoadSdf "
    channel number missing //corresponding actions contain these error messages

    ./a.out ":LoadSdf 12 "
    file name missing  //corresponding actions contain these error messages

    ./a.out ":LoadSdf 12 abc.ldf"
    parsing success  //corresponding actions contain these messages

观察到的输出:

    ./a.out ":Load"
    incorrect command

    ./a.out ":LoadSdf"      
    whitespace missing

    ./a.out ":LoadSdf "
    whitespace missing
    channel number missing

    ./a.out ":LoadSdf 12 "
    whitespace missing
    file name missing

    ./a.out ":LoadSdf 12"
    channel number missing

    ./a.out ":LoadSdf 12 abc.ldf"
    parsing success 

用于错误处理的 Ragel 语法非常困难并且解释非常有限。如果对当前案例提供任何建议,将有很大帮助。

4

1 回答 1

0

在状态机的帮助下进行更多调试,我可以想出答案。基本上>!可用于仅在启动条件的情况下进行错误处理。

LOAD_CMD = ":LoadSdf" @!(cmd_error);
CHANNEL_NR = [digit]+ >!(channel_error);
FILENAME = [a-zA-Z0-9\.\-\_]+ $!(filename_error);
SPACE = ' ' >!(whitespace_error);
于 2017-04-27T08:07:03.473 回答