2010-10-05

EnScript parser error in control statements

This will compile:

class MainClass {
  void Main(CaseClass c) {
    forall (EntryClass e in c.EntryRoot()) {
      if (e.IsSelected(), 20) { // a comma is legal???
        Console.WriteLine("so bad");
      }
    }
  }
}


I'm not sure which is worse, that this compiles, or that I found such a syntax error in my own code.

There are free tools available to generate a parser from a formal grammar. To get started, check out the bison manual.

2 comments:

  1. Jon, just found your blog today, didn't know you blogged! Good stuff.

    Regarding the 'comma in if statement', this is a lesser known, almost never used and quite unintuitive feature of C++ (i believe C also has it).

    if (condition1, condition2, ...) will execute all conditions, but only the result of the last one is taken into consideration.

    I didn't know of it myself until one fine day when I too thought it was a bug in enscript and showed it to Shaun McCreight. It did not go down well with the boss who reminded me to get my c++ right.

    Yogesh

    ReplyDelete
  2. It's not often someone teaches me something new about C++. You are going to make me break out my copy of The C++ Standard to double-check this.

    ReplyDelete