[antlr-interest] Strange parse result

Mark Christiaens mark.christiaens at sigasi.com
Sun Jan 16 13:23:57 PST 2011


I simplified my test case further.  The grammar is now:

grammar test_grammar;

start : t*;
t: ASSIGN | NUM ;
ASSIGN : ':=';
//NUM : '0';
NUM : '0' ':0'?;

I would expect this to be able to match the input

0:=0

but it doesn't.  Changing the definition of NUM to the commented version and
the grammar does match the input.

Mark

On Fri, Jan 14, 2011 at 11:37 PM, Mark Christiaens <
mark.christiaens at sigasi.com> wrote:

> Indeed, looks like that should work.  I cannot use this approach though
> since I do not directly generate the ANTLR grammar code myself.  The code is
> generated automatically from an Xtext grammar.  That type of grammar does
> not allow me to embed syntactic/semantic predicates and other advanced
> approaches.
>
> So, I would still like to know why exactly the basic example does not work.
>  Then I can maybe reformulate it to avoid the problem.
>
> Thanks for the help,
>
> Mark
>
>
> On Fri, Jan 14, 2011 at 5:46 PM, Søren Kristiansen <sorenkris at gmail.com>wrote:
>
>> You could try something like:
>>
>> RULE_ABSTRACT_LITERAL : ('0'..'9')+ LITERAL_SUFFIX?;
>>
>> fragment LITERAL_SUFFIX
>>     : {input.LT(1)==':'&& input.LT(2)!='='}?=> (':'  ('0'..'9')+ ':')
>>
>>     ;
>>
>> Regards
>>    Soren
>>
>> 2011/1/14 Mark Christiaens <mark.christiaens at sigasi.com>
>>
>>> Yes, that's right.  It's clearly a conflict with the ':' and the ':='
>>> token and using another character avoids the problem.  I can't do that
>>> though since I'm trying to build a grammar for VHDL; the language is a
>>> given.
>>>
>>> Mark
>>>
>>>
>>> On Fri, Jan 14, 2011 at 4:48 PM, Søren Kristiansen <sorenkris at gmail.com>wrote:
>>>
>>>> Not sure your assumption about look ahead is correct but the gurus will
>>>> have
>>>> to answer that ;-)
>>>>
>>>> Try changing RULE_EQUALS to match  ';=' and  15;=0 as input will give
>>>> the
>>>> expected result.
>>>>
>>>> Regards
>>>>   Soren
>>>>
>>>> 2011/1/14 Mark Christiaens <mark.christiaens at sigasi.com>
>>>>
>>>> > That was my first guess too but I doubt that that is the cause.
>>>> >
>>>> > Without the space, I expect it to see the 15 (corresponding to
>>>> > the ('0'..'9')+ part) and then look ahead and see that the next part
>>>> must
>>>> > start with a ':' followed by a number.  Since it doesn't see a number
>>>> (it
>>>> > sees a '=') it should end the RULE_ABSTRACT_LITERAL.  Then parsing can
>>>> > continue and would start from the ':' to match the RULE_EQUALS and
>>>> then
>>>> > again the RULE_ABSTRACT_LITERAL.
>>>> >
>>>> > Mark
>>>> >
>>>> >
>>>> > On Fri, Jan 14, 2011 at 4:30 PM, Søren Kristiansen <
>>>> sorenkris at gmail.com>wrote:
>>>> >
>>>> >> Hi Mark,
>>>> >> Without the space before ':=', 15: will match your
>>>> >> RULE_ABSTRACT_LITERAL rule and then '=' can't be
>>>> >> matchted.
>>>> >>
>>>> >> Regards
>>>> >>    Soren
>>>> >>
>>>> >> 2011/1/14 Mark Christiaens <mark.christiaens at sigasi.com>
>>>> >>
>>>> >>> I have a small test grammar:
>>>> >>>
>>>> >>> grammar test_grammar;
>>>> >>>
>>>> >>> testrule : t*;
>>>> >>> t: RULE_EQUALS | RULE_ABSTRACT_LITERAL | RULE_WS ;
>>>> >>> RULE_EQUALS : ':=';
>>>> >>> RULE_ABSTRACT_LITERAL : ('0'..'9')+ (':' ('0'..'9')+ ':')?;
>>>> >>> RULE_WS : (' '|'\t')+;
>>>> >>>
>>>> >>>
>>>> >>> When I debug this grammar (with ANTLRWorks 1.4.2 using ANLTR 3.3) I
>>>> feed
>>>> >>> it
>>>> >>> the input (no spaces)
>>>> >>>
>>>> >>>
>>>> >>> 15:=0
>>>> >>>
>>>> >>>
>>>> >>> and it doesn't parse my input correctly.  When I change the input to
>>>> >>> (adding
>>>> >>> space before ':=')
>>>> >>>
>>>> >>> 15 :=0
>>>> >>>
>>>> >>> it parses successfully.  Any ideas what I'm missing here?
>>>> >>>
>>>> >>> Mark
>>>> >>> --
>>>> >>> <http://www.sigasi.com>
>>>> >>>
>>>> >>> Mark Christiaens, PhD
>>>> >>> Expert Research Engineer
>>>> >>> www.sigasi.com
>>>> >>>
>>>> >>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>>> >>> Unsubscribe:
>>>> >>>
>>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>> >>>
>>>> >>
>>>> >>
>>>> >
>>>> >
>>>> > --
>>>> > <http://www.sigasi.com>
>>>> > Mark Christiaens, PhD
>>>> > Expert Research Engineer
>>>> > www.sigasi.com
>>>> >
>>>> >
>>>>
>>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>>> Unsubscribe:
>>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>>
>>>
>>>
>>>
>>> --
>>> <http://www.sigasi.com>
>>> Mark Christiaens, PhD
>>> Expert Research Engineer
>>> www.sigasi.com
>>>
>>>
>>
>
>
> --
> <http://www.sigasi.com>
> Mark Christiaens, PhD
> Expert Research Engineer
> www.sigasi.com
>
>


-- 
<http://www.sigasi.com>
Mark Christiaens, PhD
Expert Research Engineer
www.sigasi.com


More information about the antlr-interest mailing list