为函数签名定义上下文无关语法
我正在为我正在参加的编译器课程学习上下文无关语法。我试图为函数签名定义语法。例子是:
int a
int b, int c
Object a, Object d
...
我能达到的最接近的结果是:
Params -> Params, Param
| Param
| lambda
Param -> paramType paramName
但这不是我想要的。此语法允许不正确的字符串如 , int a
。我在这里已经有一段时间了,我想不出更好的方法来获得正确的语法。
任何帮助将不胜感激。
I am learning about Context Free Grammars for a Compilers course I'm attending to. I was trying to define a Grammar for function's signatures. Examples would be:
int a
int b, int c
Object a, Object d
...
The closest I could achieve to something like that was:
Params -> Params, Param
| Param
| lambda
Param -> paramType paramName
Yet this isn't what I want. This grammar allows incorrect string as , int a
. I've been here for a while and I can't think of a better way to get to a correct grammar.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上我们想要的是
(Param,)* Param | lambda
,那么在生产规则中如何做到这一点呢?那么我们可以为(Param,)*
引入另一个规则,如下所示:然后我们可以在
Params
中使用它,如下所示:请注意,我们不需要额外的规则对于单个
Param
,因为ParamCommas
已经可以是lambda
。Basically what we want is
(Param,)* Param | lambda
, so how do that in production rules? Well we can introduce another rule for(Param,)*
, like this:Then we can use it in
Params
like this:Note that we don't need an extra rule for a single
Param
asParamCommas
can already belambda
.这样做怎么样:
What about doing this: