NFA状态图的语法-ANTLR V4
我正在尝试写下以下NFA图的语法
我的代码是
grammar LTFA;
rS: 'Start' rA | EOF;
rA: 'Select_coffe' rB;
rB: 'Enter_money' rC | 'Cancel' rS;
rC: 'More_money_needed' rC | 'Refund' rE | 'Right_amount_of_money' rD;
rD: 'Change' rC | 'Done' | 'Done' rS;
rE: 'End' | 'End' rS;
WS: ( ' ' | '\t' | '\n')->skip;
语言不接受null字符串(空),我该如何制作,我如何制作接受null吗?
这是我得到的错误,我得到了
I am trying to write the grammer of the following NFA diagram
My code is
grammar LTFA;
rS: 'Start' rA | EOF;
rA: 'Select_coffe' rB;
rB: 'Enter_money' rC | 'Cancel' rS;
rC: 'More_money_needed' rC | 'Refund' rE | 'Right_amount_of_money' rD;
rD: 'Change' rC | 'Done' | 'Done' rS;
rE: 'End' | 'End' rS;
WS: ( ' ' | '\t' | '\n')->skip;
The problem is the language does not accept a null string(empty), how can I make is accept null??
here is the error I got
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这项任务可能无法完成。尽管您可以从语法生成状态机(ATN,NFA,DFA),但不可能进行反向,因为状态不是规则,并且不能仅从状态和过渡中创建规则。但是,您正在这里尝试使用语法规则代替州。
让我们看一个示例,您的规则
rb
:您的状态图所说的是:
或者
但是,规则说:
RC
(需要更多输入)或
虽然您当然可以采用一组节点并将其放入一条规则中以执行某些输入,但您将无法指示解析器解释器(或状态步行者)最终处于特定状态(例如,一旦开始,就开始状态购买已完成或取消)。
This task is probably not possible to accomplish. While you can generate a state machine (ATN, NFA, DFA) from a grammar, it's not possible to do the reverse, because states are not rules and you cannot create rules from only states and transitions. However, you are trying here to use grammar rules in place of states.
Let's take a look at an example, your rule
rB
:What your state diagram says you want is:
or
The rule however says:
rC
(which requires more input)or
While you can certainly take a set of nodes and put them in a rule to execute for certain input, you will not be able to direct the parser interpreter (or state walker) to end up in a specific state (like the start state once a purchase was finished or cancelled).
运行此代码时:
我在控制台中获取以下输出:
并且没有显示错误。
Python中的测试结果相同:
然后,
(rs< eof>)
被打印。When running this code:
I get the following output in my console:
and no errors are displayed.
And a test in Python results in the same:
then too,
(rS <EOF>)
is printed.