使上下文无关语法更简单(更漂亮)
我正在使用 CFG,每次我为特定语言编写规则时,我的 CFG 都会变得令人厌恶。它最终变成一行:
S->tooooooo much stuff
我知道将事物放入乔姆斯基范式会将其置于正确的格式,并且事物会更漂亮,但我想知道是否有任何想法可以记住,以使这些看起来不那么混乱。
即,lang:
a^n b^m, where n >= m
我的CFG(粗略):
S -> Sa|Sab|Sba|aS|aSb|abS|bSa|baS|ε
有人可以帮助我改掉我的坏习惯吗?
I'm working with CFG and every time I write up rules to a particular language, my CFG ends up disgusting. It ends up as one line:
S->tooooooo much stuff
I know that putting things into chomsky normal form will put it in the correct format and things will be prettier but I was wondering if there are any ideas to keep in mind for making these look less cluttered.
i.e., lang:
a^n b^m, where n >= m
My CFG (gross):
S -> Sa|Sab|Sba|aS|aSb|abS|bSa|baS|ε
Can anyone help me with my bad habits?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你真的需要用CFG来描述这种简单的语言吗?只计算 a 和 b 会容易得多。
但假设这只是一个例子......
真实解析器中的 CFG 通常将其分成每行一个产生式,并以某种合理的方式将它们分组。
Do you really need to use CFG to describe this simple language? It would be much easier to just count the a's and b's.
But assuming this is just an example...
CFG's in real parsers usually split it up to one production per line, and group them in some reasonable way.