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

Ambiguous Grammar

Jun 17, 2010 at 6:38 PM

New to Irony. I hope this will be an easy one for veterans to answer.

My grammar looks like this:

            var questionnaire = new NonTerminal("questionnaire");
            var questions = new NonTerminal("questions");
            var question = new NonTerminal("question");
            var text = new NonTerminal("text");
            var type = new NonTerminal("type");
            var choices = new NonTerminal("choices");
            var choiceList = new NonTerminal("choicelist");
            var choice = new NonTerminal("choice");

            var id = new IdentifierTerminal("id", " ", "0123456789");
            var value = new IdentifierTerminal("value", " ?!&(),.", "0123456789");

            var startQuestionnaire = ToTerm("Questionnaire:");
            var startQuestion = ToTerm("Question:");
            var startText = ToTerm("Text:");
            var startType = ToTerm("Type:");
            var startChoices = ToTerm("Choices:");
            var separator = ToTerm(":");

            startQuestion.Priority = 100;

            questionnaire.Rule = startQuestionnaire + id + questions;
            questions.Rule = MakeStarRule(questions, question);

            question.Rule = startQuestion + id + text + type + choices;
            text.Rule = startText + value;
            type.Rule = startType + value;

            choices.Rule = (startChoices + choiceList) | Empty;
            choiceList.Rule = MakePlusRule(choiceList, null, choice);
            choice.Rule = id + separator + value;

Example input...

Questionnaire: Holy Grail

 Question: Your Name
Text: What is your name?
Type: Text

Question: Favourite Colour
Text: What is your favourite colour?
Type: Radio
FF0000: Red
FFFF00: Yellow
0000FF: Red! No! Blue!

Question: Air Speed
Text: What is the air speed of an unladen swallow?
Type: Number

My problem is that the choice rule is matching more than it should, so my parser thinks that "Question: Air Speed" is a choice when it should actually be a new question.
I have tried every permutation of raising and lowering priorities and precedences that I can think of.


What am I missing?

Thanks in advance,





Jun 17, 2010 at 6:52 PM

Setting priorities on keywords (created by ToTerm)  does not work currently, it is a bug, will be fixed. So instead lower the priority of "id" element:

id.Priority = Terminal.LowestPriority;

On another note: do not use IdentifierTerminal for "value", this is a bit improper use, use FreeTextLiteral instead. Use NewLine terminal explicitly as list separator in MakePlus/StarRule calls. 

Jun 17, 2010 at 7:08 PM

another suggestion - do not declare all these keyword terms explicitly with ToTerm(), use string literals in grammar rules directly. That makes grammar shorter and in fact easier to read. It's worth doing it only when you use a keyword in multiple places


Jun 17, 2010 at 9:10 PM

That worked! Thanks!

I added all the ToTerm() calls when I was trying to mess with the priority. I can back them out now. Thanks for the pointer on the FreeTextLiteral - I had not seen that.

I am loving Irony so far. It's a joy to use.