views:

58

answers:

0

I think what I may have is the dangling else problem, but I'm not really sure. The language I'm tasked with writing needs to support multi-line if statements as well as single line i.e.

if(conditions){
   print "multi";
   print "line";
}

and

if(conditions)
  print "single line"

Which in my language corresponds to

if conditions then begin
end

and

if conditions then

I also need to add in support for else-if's and else's, both using the same syntax as above with the begin's and end's. I've spent a while on trying to get this to work and I've come close, but with the grammar I'm using right now I'm getting the "this input has more than one valid interpretation..." error. Here's the MGrammar code I'm using:

syntax sIf = tIf conditions:Common.List(sBooleanExpression) tThen sBegin statements:sStatements2 (tEnd | tEnd2) next:Common.List(sElseIf)?
              => If{Conditions{conditions}, Body{statements}, Next{next}}
              | tIf conditions:Common.List(sBooleanExpression) tThen statement:sSingleStatement next:Common.List(sElseIf)?
              => SingleIf{Conditions{conditions}, Body{statement}, Next{next}}
              ;

syntax sElseIf = tElseIf conditions:Common.List(sBooleanExpression) tThen sBegin statements:sStatements2 (tEnd | tEnd2) next:Common.List(sElseIf)?
                => ElseIf{Conditions{conditions}, Body{statements}, Next{next}}
                | tElseIf conditions:Common.List(sBooleanExpression) tThen statement:sSingleStatement next:Common.List(sElseIf)?
                => ElseIf{Conditions{conditions}, Body{statement}, Next{next}}
                | e:sElse => e;


syntax sElse         = tElse tThen sBegin es:sStatements2 (tEnd | tEnd2) => Else{Body{es}}
                     | tElse statement:sSingleStatement => Else{Body{statement}}
                     ;

This is probably the 10th version of this, as I've tried various things to get the if/else if/else structure to work.

Basically, my Main gets a StatementList of Statements, sIf being one of them. sIf tries to find either the multi-line or the single line if statement followed by an sElseIf which is optional(you don't have to have an else).

I think the problem is the regex compiler may be seeing the else if as an sElse followed by an sIf statement, instead of an sElseIf statement. In preview mode it actually draws the tree exactly how I want it, so if there's a way to ignore this message when I parse it in my C# app then that would work too I guess.