Bison:单个规则中的可选标记
我正在使用 GNU Bison 2.4.2 为我正在研究的新语言编写语法,我有一个问题。 当我指定规则时,假设:
statement : T_CLASS T_IDENT '{' T_CLASS_MEMBERS '}' {
// create a node for the statement ...
}
如果我对该规则有变体,例如
statement : T_CLASS T_IDENT T_EXTENDS T_IDENT_LIST '{' T_CLASS_MEMBERS '}' {
// create a node for the statement ...
}
Where(来自flex扫描仪规则):(
"class" return T_CLASS;
"extends" return T_EXTENDS;
[a-zA-Z\_][a-zA-Z0-9\_]* return T_IDENT;
并且T_IDENT_LIST是逗号分隔标识符的规则)。
有没有办法仅在一个规则中指定所有这些,以某种方式将“T_EXTENDS T_IDENT_LIST”设置为可选? 我已经尝试过
T_CLASS T_IDENT (T_EXTENDS T_IDENT_LIST)? '{' T_CLASS_MEMBERS '}' {
// create a node for the statement ...
}
但是 Bison 给了我一个错误。
谢谢
I'm using GNU Bison 2.4.2 to write a grammar for a new language I'm working on and I have a question.
When I specify a rule, let's say:
statement : T_CLASS T_IDENT '{' T_CLASS_MEMBERS '}' {
// create a node for the statement ...
}
If I have a variation on the rule, for instance
statement : T_CLASS T_IDENT T_EXTENDS T_IDENT_LIST '{' T_CLASS_MEMBERS '}' {
// create a node for the statement ...
}
Where (from flex scanner rules) :
"class" return T_CLASS;
"extends" return T_EXTENDS;
[a-zA-Z\_][a-zA-Z0-9\_]* return T_IDENT;
(and T_IDENT_LIST is a rule for comma separated identifiers).
Is there any way to specify all of this only in one rule, setting somehow the "T_EXTENDS T_IDENT_LIST" as optional?
I've already tried with
T_CLASS T_IDENT (T_EXTENDS T_IDENT_LIST)? '{' T_CLASS_MEMBERS '}' {
// create a node for the statement ...
}
But Bison gave me an error.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
长话短说,没有。 Bison 只处理 LALR(1) 语法,这意味着它只使用一个前瞻符号。您需要的是这样的:
不过还有其他解析器生成器可以处理更通用的语法。如果没记错的话,其中一些支持像您所要求的那样相对直接的可选元素。
To make a long story short, no. Bison only deals with LALR(1) grammars, which means it only uses one symbol of lookahead. What you need is something like this:
There are other parser generators that work with more general grammars though. If memory serves, some of them support optional elements relatively directly like you're asking for.
为什么不使用选择 (
|
) 运算符来拆分它们呢?我认为你不能仅仅因为这是一个 LALR(1) 自下而上的解析器就可以做到这一点,你需要一些不同的东西,比如 LL(k) (ANTLR?) 来做你想做的事情。
Why don't you just split them using the choice (
|
) operator?I don't think you can do it just because this is a LALR(1) bottom-up parser, you would need something different like a LL(k) (ANTLR?) to do what you want to do..
我想你最多能做的就是
I think the most you can do is