Ragel可以执行单独的命令解析并相应地抛出错误

时间:2017-04-26 20:22:22

标签: regex parsing fsm ragel

我正在尝试解析命令:LoadSdf 12 abc.ldf 我试图在命令的每个阶段(:LoadSdf12abc.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语法非常困难,并且解释非常有限。如果对当前案件提出任何建议,将会有很大的帮助。

1 个答案:

答案 0 :(得分:0)

借助状态机进行更多调试,我可以得出答案。 基本上>!只能在启动条件下用于错误处理。

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