NFA状态图的语法-ANTLR V4

发布于 2025-01-30 06:53:42 字数 545 浏览 3 评论 0原文

我正在尝试写下以下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
Coffee machine state 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

The error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇年纸鸢 2025-02-06 06:53:42

这项任务可能无法完成。尽管您可以从语法生成状态机(ATN,NFA,DFA),但不可能进行反向,因为状态不是规则,并且不能仅从状态和过渡中创建规则。但是,您正在这里尝试使用语法规则代替州。

让我们看一个示例,您的规则rb

rB: 'Enter_money' rC | 'Cancel' rS;

您的状态图所说的是:

  1. 我们必须处于状态Q2。
  2. 我们输入输入“ Enter_money”。
  3. 我们最终进入州Q3。

或者

  1. 我们输入输入“取消”。
  2. 我们最终进入州Q0。

但是,规则说:

  1. 匹配输入“ enter_money”。
  2. 然后匹配规则RC(需要更多输入)

  1. 匹配输入“ CANCEL”。
  2. 然后匹配规则“ rs”(这需要整个链才能再次匹配)。

虽然您当然可以采用一组节点并将其放入一条规则中以执行某些输入,但您将无法指示解析器解释器(或状态步行者)最终处于特定状态(例如,一旦开始,就开始状态购买已完成或取消)。

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:

rB: 'Enter_money' rC | 'Cancel' rS;

What your state diagram says you want is:

  1. We have to be in state Q2.
  2. We enter the input "Enter_money".
  3. We end up in state Q3.

or

  1. We enter the input "cancel".
  2. We end up in state Q0.

The rule however says:

  1. Match the input "Enter_money".
  2. Then match rule rC (which requires more input)

or

  1. Match the input "Cancel".
  2. Then match rule ' rS' (which requires the whole chain to match again).

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).

玻璃人 2025-02-06 06:53:42

运行此代码时:

LTFALexer lexer = new LTFALexer(CharStreams.fromString(""));
LTFAParser parser = new LTFAParser(new CommonTokenStream(lexer));

System.out.println(parser.rS().toStringTree(parser));

我在控制台中获取以下输出:

(rS <EOF>)

并且没有显示错误。

Python中的测试结果相同:

from antlr4 import *
from LTFALexer import LTFALexer
from LTFAParser import LTFAParser

if __name__ == '__main__':
    lexer = LTFALexer(InputStream(''))
    parser = LTFAParser(CommonTokenStream(lexer))
    print(parser.rS().toStringTree(recog=parser))

然后,(rs&lt; eof&gt;)被打印。

When running this code:

LTFALexer lexer = new LTFALexer(CharStreams.fromString(""));
LTFAParser parser = new LTFAParser(new CommonTokenStream(lexer));

System.out.println(parser.rS().toStringTree(parser));

I get the following output in my console:

(rS <EOF>)

and no errors are displayed.

And a test in Python results in the same:

from antlr4 import *
from LTFALexer import LTFALexer
from LTFAParser import LTFAParser

if __name__ == '__main__':
    lexer = LTFALexer(InputStream(''))
    parser = LTFAParser(CommonTokenStream(lexer))
    print(parser.rS().toStringTree(recog=parser))

then too, (rS <EOF>) is printed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文