Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  • v1->1 | 1v1
  • v2-> 2 | 2v2

It is obvious that this is the language 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.

like image 327
Iordanis Avatar asked Mar 23 '13 23:03

Iordanis


1 Answers

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's algorithm runs in time O(n3) 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(n3) runtime.

Hope this helps!

like image 146
templatetypedef Avatar answered Nov 05 '22 04:11

templatetypedef