简体   繁体   English

在Java中验证给定Context Free Grammar的字符串

[英]Validate string given Context Free Grammar in Java

How can someone validate if a string is part of a context free Grammar? 有人如何验证字符串是否是无上下文语法的一部分? Not just virtually, but build an algorithm for it? 不只是虚拟,而是为它构建算法?

Given a context free grammar with rules such as 给定具有上下文无关语法的规则,例如

  • V-> v1v2 V-> v1v2
  • v1->1 | v1-> 1 | 1v1 1V1
  • v2-> 2 | v2-> 2 | 2v2 2V2

It is obvious that this is the language 1^n 2^n. 很明显,这是语言1 ^ n 2 ^ n。 But how would you go about with an algorithm to verify if it actually is. 但是你将如何使用算法来验证它是否真的存在。 I am trying to accomplish this in java. 我想在java中完成这个。

You might want to look into Earley's algorithm or the CYK algorithm , which are two algorithms for deciding whether a string is generated by a context-free grammar. 您可能希望查看Earley算法CYK算法 ,这两种算法用于决定字符串是否由无上下文语法生成。 Earley's algorithm runs in time O(n 3 ) for any string of length n regardless of the production rules in the grammar (though the constant term in the big-O notation depends on the grammar), while the CYK algorithm requires that the grammar first be converted to Chomsky normal form to guarantee O(n 3 ) runtime. 无论语法中的生成规则如何,Earley的算法都会在时间O(n 3 )内运行任何长度为n的字符串(尽管big-O表示法中的常量项取决于语法),而CYK算法要求语法优先转换为Chomsky法线形式以保证O(n 3 )运行时间。

Hope this helps! 希望这可以帮助!

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

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