Here I am again, presenting to you another 'OH MI GOD, SO MUCH MATH' article.
TL; DR;
I made a simple JavaScript library that allows you to validate complex user input whenever regular expressions can't help you (validating formulae for example). Unfortunately, due to the algorithm's generality it ain't the quickest. Go to last section to see how to use the library if you are interested.
Quick link to demo:
Validating words in sample grammars
WTF is a contextfree grammar?
Theoretical computer scientists spent an awful lot of time examining properties of certain languages (actually that's how they created programming languages). They've discovered that there are a few classes of languages that are useful in practice. The latter can be arranged into a hierarchy called Chomsky's hierarchy named after the famous Noam Chomsky. Two of the most important of those classes are the regular languages (languages that you can use regular expressions to match) and the contextfree languages about which this article is all about. Now what exactly is a language? Let's take a look at a few definitions.
We'll call an alphabet any set of characters and we'll denote it with Σ.
We'll call a word (string) any sequence of characters over a given alphabet. Empty word will be the word with length 0. Denote any word with w and denote the empty word with ε.
We'll call a language any set (be it finite or infinite) of words (strings) over a given alphabet. Denote it with L.
Languages are just a formal way to say 'Here's a collection of strings that have the same structure / look alike / fall under the same rules'. Now given a language, how do we:
 Generate a random string in the language?
 Tell whether a specific string is in the language?
Well, there is no general algorithm that can solve the tasks above for any language (no such algorithm can even exist!) but for certain classes of languages there are. Having said that, we arrive at our final definition which I know looks quite confusing, so read it twice if you need to.
Firstly, call a nonterminal a special character that will allow us to do kewl thingz (think of it as a variable). We'll denote nonterminals with uppercase letters.
Secondly, consider an object which consists of the following:
 A set of nonterminals N
 An alphabet Σ – call the alphabet's characters terminals
 A special nonterminal that is contained in the set N, call it the start symbol, denote it with S
 A set of derivation rules R that allow us to substitute a nonterminal for a mixed sequence of nonterminals and terminals
Formally, our object is the quadruple. We just gave birth to a context – free grammar (CFG).
That's cool, now what?
Let us look at an example grammar:
, where our rules are:
This means that we have a single nonterminal – the starting symbol S and the alphabet consists solely from the letters a and b. Let's put our grammar to practice.
We'll start with… well – the starting symbol. Apply on the start symbol any of our two rules by substituting S with the right hand side of the rule until there's nothing to be replaced. The algorithm goes like that:
Or we might choose to apply rule 2 immediately:
It is obvious that the algorithm terminates whenever only terminals are left (which explains where did terminals and nonterminals got their names from).
Using the above derivations, we generated two words – namely aaabbb and ab over the alphabet . In other words, we've just discovered an algorithm for generating words in the language that consists of all strings that begin with a number of a and end with the same number of b.
Consider another grammar:
, where the rules are:
(the disjunction symbol  allows us to conveniently write several rules on one line  is equivalent to )
Applying rules at random we can generate. Clearly, the grammar just presented can construct valid English sentences. Even more, you can construct incredibly complex grammars that generate an astonishing amount of words. Without formal proof, we can conclude that a CFG is a great language generator. Let us call any language that can be produced by CFG a context – free language.
Recognizing context – free languages
A great deal of languages are context – free. For example, almost all programming languages you've learnt or heard of are CF. Furthermore, the compilers and interpreters for those languages make heavy use of CFGs. Here's the grammar used to produce the JavaScript language.
All languages created by regular expressions are context – free. Unfortunately, most context – free languages can't be described with regular expressions and this is why we must use grammars. There are a few algorithms that deal with the question 'Does this string belong to this CF language?'. Compilers usually use LR parsers but the latter only work on a special subset of CFGs. The library I wish to present to you uses the more general (and thus slower) CYK algorithm for solving the same problem. When saying slower, I mean really slow – worst time is  that is cubic in the length of the word being validated and linear in the size of grammar (the grammar's size is the length of all rules combined)
The algorithm works in two steps:
 Preprocess the grammar – normalize it, precompute some important sets
 Given a word, use CYK to test whether the word is valid
I won't go into details but you can read more in this paper by Lange and Leiss.
Here's a really simple and ugly – looking demo to see that CYK actually works.
Did I succeed in convincing you that there are cases in which regular expressions are powerless and you MUST fall to CFGs?
If not, come back if you feel the urge.
If yes, go to the repository @ github, download the library and validate stuff like a champ.
Using the CfgSolver library
To use CfgSolver you simply follow the steps I outlined above. Begin by creating your grammar as a standard JS object. Here's an example grammar and its JS representation:
, with rules :
( stands for expression, stands for operation and N stands for number)
The rules effectively say that
 an expression is either an operation over two other expressions or simply a number
 an operation is either +, , *, /
 a number is either two numbers concatenated or a single digit
This grammar generates all arithmetic expressions without parentheses (the only problem here is that there may be leading 0s but for simplicity we won't deal with it).
Here's the JavaScript object that represents the grammar:
var grammar = {
"E": ["EOE", "N"],
"O": ["+", "", "*", "/"],
"N": ["NN", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",],
}
Pretty much the same thing, eh? Use it like that:
// Preprocess the grammar and save the returned container in a variable
var container = CfgSolver.preprocess(grammar);
// Use the container to test whether a word is valid
var word = "5+3*4";
var isValid = CfgSolver.recognizeWord(container, word); // isValid is now true
That's it, you now have a Boolean that shows you whether the word is valid.
Notes
There are a few things I couldn't find where to fit, so I'll just drop them all here.
<
ul>

( stands for type, stands for array square brackets and is optional, stands for name – for simplicity names can only be strings containing the letters a, b and c)
This grammar allows to have both array and normal variables declared. In case an array is declared use the rule, else use the rule . To write rule deriving the empty word in your JavaScript, use CfgSolver.epsilon
Applying the rules derives the empty word and CfgSolver will throw an exception when preprocessing your grammar.
CfgSolver.printer.printGrammar(grammar);