Recognizing different identifiers

Jan 13, 2010 at 1:08 AM
Edited Jan 13, 2010 at 1:09 AM

Hi Rivantsov,

Quick question... Giving this grammar:

    public class TestBed: Grammar {

        public TestBed( ) {
            var identifier1 = new IdentifierTerminal( "identifier1" );
            var identifier2 = new IdentifierTerminal( "identifier2" );

            var rule1 = new NonTerminal( "rule1" );
            var rule2 = new NonTerminal( "rule2" );
            var ruletest = new NonTerminal( "ruletest" );

            rule1.Rule = "model" + identifier1;
            rule2.Rule = "property" + identifier2;
            ruletest.Rule = "resource" + identifier1;

            var rules = new NonTerminal( "rules" );
            rules.Rule = rule1 | rule2 | ruletest;

            var rulelist = new NonTerminal( "rulelist" );
            rulelist.Rule = MakePlusRule( rulelist, rules );

            this.Root = rulelist;



This target program parses without error:

model thismodel
property thisproperty
resource thisproperty

The identifier after resource can only be "thismodel", but it gets parsed without error.

Should the parser catch this situation? or is beyond the scope of Irony?







Jan 13, 2010 at 2:16 AM

why exactly you expect the last line to fail?! "thismodel" and "thisproperty" are both valid identifiers for both of your IdentifierTerminals. 

Look at parse tree and you'r probably see for the last line: "identifier1" node (term) with text value "thisproperty"



Jan 13, 2010 at 3:09 AM
I will assume that the question was not hypothetical, so I'll answer
the why.
In the grammar I have defined that after model goes an identifier. I
also defined that after property goes an identifier. But i did it in
two separate variables. My hope was to communicate that they have to
be different identifiers, that they cannot be the same. Further more,
after resource only one of the identifiers is valid, but not the
other, because i used the second variable.
So for example, in c# once i put an identifier after the word int, it
is an error to put the same identifier after the word string in the
same context.
Now, I have a step after that will check for logical errors; I was
just wondering if this particular check can be done by Irony; or if I
am totally off topic, and this check is beyond the realm of the
parser, and it should be done by the consumer of the AST.
Base on your answer, I guess it should be done after parsing.
Or maybe there is a way to write the grammar to force this kind of
check, if this is the case, would you please point me in the right

Jan 13, 2010 at 3:38 AM

yes, this kind of analysis is not in parser domain. First, x or y are indistinguishable for parser, they are the same term, "identifier". It is the semantic analysis step that actually looks into their content and recognizes them as different identifiers.