简体   繁体   English

为单词列表生成 CFG

[英]Generate a CFG for a list of words

It is possible to write an algorithm that generates a CFG that parses a list of words, with maxim m rules, or if it is imposible to parse all words, to parse maximum words possible?是否可以编写一个算法来生成解析单词列表的 CFG,使用 maxim m规则,或者如果不可能解析所有单词,则解析可能的最大单词? Do you have any ideas about how should I start the algorithm?你对我应该如何启动算法有什么想法吗? All input words will be in the [az] alphabet.所有输入词都将在[az]字母表中。

It is definitely possible to write such an algorithm.绝对有可能写出这样的算法。 Without going through the details, you can enumerate all possible CFGs (unique up to renaming of the nonterminals) with m or fewer rules over the desired alphabet, and check each one to see whether it accepts your strings.无需详细说明,您可以在所需字母表上使用 m 或更少规则枚举所有可能的 CFG(在重命名非终结符之前是唯一的),并检查每个 CFG 以查看它是否接受您的字符串。 If you do this in order of number of rules, you will stop on the first smallest CFG that works, if any.如果您按照规则数量的顺序执行此操作,您将在第一个有效的最小 CFG(如果有)处停止。 To see whether a CFG works, you can roll your own or derive a PDA from the CFG and then run the PDA on all inputs to see whether it accepts them.要查看 CFG 是否有效,您可以自己运行或从 CFG 派生出一个 PDA,然后在所有输入上运行 PDA 以查看它是否接受它们。 There are algorithms to generate PDA parsers from CFGs which you should be able to use.有一些算法可以从您应该能够使用的 CFG 生成 PDA 解析器。

This process is not particularly efficient but it is effective and suffices to show that it is possible.这个过程不是特别有效,但它是有效的,足以表明它是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM