antlr4 - 想要解析相同的结构
问题描述
我想让 ANTLR4 解析这个:
FSM
name type String
state type State
Relation
name type String
我正在使用这个语法:
grammar Generator;
classToGenerate:
name=Name NL
(attributes NL)+
classToGenerate| EOF;
attributes: attribute=Name WS 'type' WS type=Name;
Name: ('A'..'Z' | 'a'..'z')+ ;
WS: (' ' | '\t')+;
NL: '\r'? '\n';
我想成功阅读,我不知道为什么,但每次我运行我的程序时,我都会收到这个错误:
line 6:18 no viable alternative at input '<EOF>'
有什么修复吗?
解决方案
尾随EOF
为你搞砸了。尝试创建一个与令牌匹配的单独规则EOF
,前面有一个或多个classToGenerate
(parse
我的示例中的规则):
grammar Generator;
parse
: classToGenerate+ EOF
;
classToGenerate
: name=Name NL (attributes NL)+
;
attributes
: attribute=Name WS 'type' WS type=Name
;
Name: ('A'..'Z' | 'a'..'z')+ ;
WS: (' ' | '\t')+;
NL: '\r'? '\n';
你真的需要保留空格和换行符吗?您可以让词法分析器丢弃它们,这使您的语法更容易阅读:
grammar Generator;
parse
: classToGenerate+ EOF
;
classToGenerate
: name=Name attributes+
;
attributes
: attribute=Name 'type' type=Name
;
Name : [a-zA-Z]+;
Spaces : [ \t\r\n] -> skip;
推荐阅读
- azure-devops - 在 azure devops 部署期间覆盖 helm 图表中的配置映射
- javascript - 如何在没有确认按钮的情况下提交 Hcaptcha。(2验证码/数据库)
- oracle - 如何覆盖 Oracle Apex 以上传文件
- c# - 来自 Azure 函数中使用的类库的日志消息
- python - Spacy 在指定单词之前查找文本
- ruby-on-rails - 我们可以在 Ruby on Rails 应用程序中使用 SendGrid 模板和 ActionMailer 吗?
- python - 导入portalocker时出现锁文件语法错误
- wpf - 如何像默认上下文菜单一样为 ScrollBar 的自定义上下文菜单获取默认本地化?
- javascript - ReferenceError - d3 未定义
- office-js - Outlook for Mac 用户的“新 Outlook”中缺少加载项