3

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!

Goz
  • 61,365
  • 24
  • 124
  • 204

1 Answers1

2

There are quite a few relevant questions already

What's the best way to write a parser by hand?

Resources for writing a recursive descent parser by hand

How to write a recursive descent parser from scratch?

https://stackoverflow.com/questions/2405623/looking-for-a-tutorial-on-recursive-descent-parsing

Community
  • 1
  • 1
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
  • Cheers, I studied a language processors course at uni .. but that was 13 years ago ... I'd quite forgotten about names for parsers such as "Recursive Descent". That gives me somewhere to start looking from. I'm still open to more explicit kicks up the arse though :D – Goz Nov 18 '10 at 11:35