MGrammar and more than one valid interpretation
I think I may have a dangling issue yet, but I'm not sure. The language I'm assigned to write must support multi-line if statements as well as a single line ie
if(conditions){
print "multi";
print "line";
}
and
if(conditions)
print "single line"
What in my language matches
if conditions then begin
end
and
if conditions then
I also need to add support for else-if and else using the same syntax as above, from the beginning and end. I spent some time trying to get this to work and I came close, but with the grammar I'm using right now, I get the error "this input has more than one valid interpretation ...". 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 tried different things to get an if / else if / else structure.
Basically, my Main gets a StatementList if it's one of them. sIf tries to find either a multi-line or single line if a statement followed by sElseIf, which is optional (you don't need to have an else).
I think the problem is that the regex compiler can see the else if sElse is followed as sIf and not sElseIf. In preview mode, it actually draws the tree exactly the way I want it, so if there is a way to ignore this message when I parse it in my C # application, that will work too. I think.
a source to share
No one has answered this question yet
Check out similar questions: