I'm trying to write an MSBuild condition parser. The conditions are described here.
So I've come up with a grammar that appears to work fine:
S -> !S
S -> S == S
S -> S != S
S -> S && S
S -> S || S
S -> Fn( str )
S -> str == str
S -> str != str
S -> n < n
S -> n <= n
S -> n > n
S -> n >= n
This seems to fit my needs and I've come up with a set of C++ classes that define this simple language. ie I can create the classes such that they fit above and then I can call "run" on the base statement and I get a boolean value out of the other end.
Using this language the following:
(!Exists( "C:\\config.sys" ) && 14 < 17) || (AString == AString2 && HasTrailingSlash( "C:" ))
becomes defined as (And forgive the huge line of code! ;)):
Statement baseStatement( new StatementOrStatement( new StatementAndStatement( new NotStatement( new ExistsFunctionStatement( "C:\\Config.sys" ) ), new NumberLessThanNumberStatement( 14.0, 17.0 ) ), new StatementAndStatement( new StringEqualStringStatement( "AString", "AString2" ), new HasTrailingSlashFunctionStatement( "C:\\" ) ) ) );
I can simply run the above statement as follows:
const bool result = baseStatement.Run();
So thus far all is fine. It seems I can represent the whole "language" using my grammar and i can build the grammar into a valid full statement.
However now I have the next problem. I need to actually parse the string. I have no idea of where to start on this. Can anyone help me on how I write the actual string parser to parse the original statement into the set of C++ classes above? I'm rather lost on this front.
I'm doing this purely for my own learning reasons so as far as possible I don't want to use someone else's parser library.
Thanks in advance!