ANTLR 中的标记之间没有空格
我正在编写一个非常简单的 C# 语法子集作为练习。
然而,我有一个规则,即空格给我带来了一些麻烦。
我想区分以下内容:
int a;
int? b;
第一个是“常规”int 类型,第二个是可为空的 int 类型。
但是,以我当前的语法,我无法解析它。
type : typeBase x='?' -> { x == null } typeBase
-> ^('?' typeBase)
;
typeBase : 'int'
| 'float'
;
问题是,根据这些规则,它只适用于“?”之前的空格,如下所示:
int ? a;
这是我不想要的。
有什么想法吗?
I'm writing a very simple subset of a C# grammar as an exercise.
However, I have a rule which whitespaces are giving me some troubles.
I want to distinguish the following:
int a;
int? b;
Where the the first is a "regular" int type and the second is a nullable int type.
However, with my current grammar I'm not being able to parse this.
type : typeBase x='?' -> { x == null } typeBase
-> ^('?' typeBase)
;
typeBase : 'int'
| 'float'
;
The thing is that whith these rules, it only works with a whitespace before '?', like this:
int ? a;
Which I'd don't want.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)你对空白的定义似乎有缺陷......你提出的语法应该接受“int?”和“int?”。也许你应该看看空白的定义。
2) 如果你想禁止“int ? a”,你可以定义额外的标记“int?”和“漂浮?” ...通常您允许每个标记之间出现空格,因此您必须将其设为一个标记。
1) Your definition of whitespace seems to be flawed ... the grammar you present should accept "int?" and "int ?". Maybe you should take a look of the definition of whitespace.
2) If you want to disallow "int ? a" you can define extra tokens 'int?' and 'float?' ... normally you allow whitespace to appear between every token, so you have to make it one token.