Simple Change -> Break

Mar 15, 2012 at 4:55 PM

Hi there,

I got a quite simple grammar that looks like this:

public class DslGrammar : Grammar
{
	public DslGrammar()
	{
		var identifier = new IdentifierTerminal("Identifier");
		var argDeclaration = new NonTerminal("ArgDeclaration");
		var stmtList = new NonTerminal("StmtList");
		var functionDef = new NonTerminal("FunctionDef");
		var paramList = new NonTerminal("ParamList");

		argDeclaration.Rule = identifier + identifier;
		paramList.Rule = MakeListRule(paramList, ToTerm(","), argDeclaration, TermListOptions.StarList);
		functionDef.Rule = (identifier | Empty) + identifier + "(" + paramList + ")" + Eos;
		stmtList.Rule = MakeListRule(stmtList, null, functionDef, TermListOptions.StarList);

		Root = stmtList;
	}

	public override void CreateTokenFilters(LanguageData language, TokenFilterList filters)
	{
		var outlineFilter = new CodeOutlineFilter(
			language.GrammarData,
			OutlineOptions.ProduceIndents,
			null); // "\" is continuation symbol
		filters.Add(outlineFilter);
	}
}

When I test it with the following code, it works fine:

void DoIt(int x)

Now, when I change the code to

DoIt(int x)

it dowsn't work anymore. The parser has an error which says that after "DoIt", another identifier is expected. But when you look at the functionDef rule, the first element can be an identifier OR Empty, so IMHO, it should work...

Can you tell me what's wrong?

 

Thank you very much :)

Coordinator
Mar 15, 2012 at 5:21 PM

When you load the grammar into Grammar explorer, does it show any conflicts in grammar errors page? it should show shift/reduce. One way to go would be to declare the second identifier optional, not the first one

Mar 15, 2012 at 5:48 PM

Your suggestion works - thank you very much :)