Fork me on GitHub

COFGRATX

Context Free Grammars are really neat. The can be seen in action in the parsers of many higher level programming languages, such as Ruby, C++, Java, and even LISP. The purpose of this website is to allow the users to encode CFG's using BNF in order to learn and further study CFGs.

This project came about after discovering that by upgrading Ruby on Rails from an older version, the methods find_first and find_all, were no longer supported. Instead of manually going through all the old functions and updating them by hand, which could take weeks as these two functions appeared several hundred times across many files, the decision was made to spend more time to create a translator. Some sort cuts were taken, such as using regular expressions to denote terminal symbols, and using a script to first ignore all lines that didn't have the old function, and then only sending part of the old line to be parsed and translated by the lazy set of rules defined for the grammar.

This replacement is more tricky to accomplish if using just a regular expression. Granted it could be done, but the expression becomes more complex and possibly repetitive, since find_first and find_all take in a hash containing zero or more pairs. To illustrate, lets assume we have only one pair as parameter to find_first. User.find_first(:age => "25") A regular expression in Perl might look something like this: $line =~ s/(.*?\.)find_(first)\(:(.*?)\s*=>\s*(.*?)\s*\)/\1find\(:\2,:conditions=>['\3=?',\4]\)/; But say we have two pairs as parameter to find_first. The regular expression above no longer fixes the old function. It is not practical to write a long chain of regular expression to solve this problem, even if one were to write it such that it handled zero through infinite (which is not possible given today's capability of computers and the nature of regular expressions) parameters to find_first.

This problem is more clearly solved with treating the old line as a context free grammar and performing some mutations upon it.

COntext Free GRAmmar Translator(X)