views:

142

answers:

1

I am building up a MGrammar spec to parse some pseudo code looking for particular bits of information. I have most of the spec working except for 1 cruical element.

The pseudo code supports an if-then-else syntax and I have been unable to find a satisfactory way of parsing it. The exact construct is...

IF expression operator expression THEN
Statement1
Statement2
Statementn
ELSEIF expression operator expression THEN
Statement1
Statement2
Statementn
ELSE
Statement1
Statement2
Statementn
ENDIF

...Where the Else and Elseif are optional.

What I have so far is: `syntax Statement = r:ReturnClause => r |
i:IfClause => i |
ei:ElseifClause => ei |
e:ElseClause => e |
end:EndClause => end |
v:Expression => v ;

syntax IfClause = If name:Identifier operator:Operator Then statement:Statement => If[name, operator, Then[statement]];
syntax ElseifClause = Elseif name:Identifier operator:Operator Then statement:Statement => ElseIf[name, operator, Then[statement]];
syntax ElseClause = Else statement:Statement => Else[statement];
syntax EndClause = Endif; `

However, the Statement after the 'Then' and 'Else' is not greedy enough and only captures the first statement in the parse tree.

Has anyone tried to implement the parsing of an If statement using MGrammar or have any suggestions??

A: 

You can find a (almost) complete C# 4.0 grammar in the archetype project on codeplex. Dan Vanderboom wrote it in preparation of his new language code named "Archetype".

Maybe that helps: http://archetype.codeplex.com/

Lars Corneliussen