This project has moved and is read-only. For the latest updates, please go here.

Possible to "break" out of a state?

Jan 9, 2010 at 2:01 PM
Edited Jan 9, 2010 at 2:08 PM


I have a proprietary language I'm creating a grammar for which has commands in the following form:

someCommand <flags> <arguments>

Flags are in the form of -someFlag

Arguments are any literal, so numbers, strings, true, and false are valid values.

Here is an example command:

someCommand -someFlag -someOtherFlag 5 true false 6 -7 "string" "another string";

A problem occurs though when using a unary operator on a numerical argument immediately after the flags. This statement fails to parse correctly, but it is valid syntax:

someCommand -someFlag -someOtherFlag -5 true false 6 -7 "string" "another string";

The issue seems to be that the parser is in the command flags state, so it hits the -5 and gags because it wants an Identifier, not a Number. I had hoped it would just leave the state when it hit something that didn't match. If that were the case, the -5 would be picked up properly by the arguments rule.

Any suggestions for how to make this work would be greatly appreciated.




Grammar code:


CommandStatement.Rule = Identifier + CommandFlags_opt + CommandArgs_opt + SemiColon;
CommandFlags_opt.Rule = MakeStarRule(CommandFlags_opt, CommandFlag);
CommandFlag.Rule = ToTerm("-") + Identifier;
CommandArgs_opt.Rule = MakeStarRule(CommandArgs_opt,  Literal);
Literal.Rule = UnaryOperator_opt + Number | String | ToTerm("true") | "false";
UnaryOperator.Rule = ToTerm("+") | "-";

Jan 9, 2010 at 6:04 PM

Try the following: Remove UnaryOperator non-terminal and add flag AllowSign to Number

Jan 9, 2010 at 6:27 PM

Thank you a ton! That solved the problem :)

I really appreciate you taking your time to help me solve my problem.

-Matt Kerchmar