你能修改这个 BNF 语法以始终包含奇数只狗吗?
你能修改这个 BNF 语法以始终包含奇数只狗吗?
<pets> ::= <pets> <pet> | <pet>
<pet> ::= dog | cat
“宠物”的示例:
dog cat
cat dog
dog dog dog
dog dog cat cat dog
dog cat dog dog
不是“宠物”的示例:
cat
dog cat dog
cat cat
Can you modify this BNF grammar to always contain an odd number of dogs?
<pets> ::= <pets> <pet> | <pet>
<pet> ::= dog | cat
Examples of 'pets':
dog cat
cat dog
dog dog dog
dog dog cat cat dog
dog cat dog dog
Not examples of 'pets':
cat
dog cat dog
cat cat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您希望在概念上拥有一个状态机。您处于两种状态之一:您看到了奇数条狗,或者您看到了偶数条狗。
尝试:
它可能需要一些清理工作,但它应该可以工作。关键要注意的是<猫>并且不会匹配任何内容。唯一必须有代币的生产是< one_dog>.
You want to conceptually have a state machine. You are in one of two states: you have seen an odd number of dogs, or you have seen an even number of dogs.
Try:
It could use some cleaning up, but it should work. The key thing to note that < cats > and will match against nothing. The only thing that production that must have a token is < one_dog >.