[antlr-interest] Q: How to understand 'multiple alternatives'?

Austin Hastings Austin_Hastings at Yahoo.com
Sat Aug 16 10:16:01 PDT 2008


I am trying it now. :)

Whoa.

Okay, at first it got even more confusing. I've learned that:

1. When Antlr talks about "matching input" it means from a particular 
location in the grammar -- not from the beginning of the file. So "can 
match input '&'" really means "can match input '&' AFTER YOU GET HERE."

2. The arrows and diagrams shown in AntlrWorks are also "after you get 
here."

3. The arrows do NOT show "call" behavior, but "code flow" behavior. For 
me, this meant several rules were included that wrapped my inner rules 
only so that the tool could show the code flow leaving those rules. 
There was a lot of "arrow to the end of a rule" that I had to learn to 
ignore.
 
4. When the arrow points to the START of a rule, that's where your 
problem is. In my case, the reason why it was possible to match '&' 
multiple ways, along with 4 other warnings, was because I had forgot to 
specify that variable declarations had to end with a semicolon. It was 
telling me that the end of an initialization (int x = foo) could blur 
into the beginning of an expression statement (&bar ;) forming something 
like "int x = foo &bar;" as either a bitwise-and or as an initialization 
followed by a pointless computation of the address of bar.

Thanks, Johannes!

=Austin





Johannes Luber wrote:
> Austin Hastings schrieb:
>> Howy,
>>
>> I'm parsing a C-like language, and I'm getting this kind of warning:
>>
>> warning(200): perl6/hlasm/Hlasm.g3:245:4: Decision can match input 
>> such as "'&'" using multiple alternatives: 1, 2
>> As a result, alternative(s) 2 were disabled for that input
>>
>> In several places. In fact, the '&' (and several other characters) 
>> can have different roles to play in the language. In this case, & can 
>> either be a bitwise and, or an address operator.
>>
>> However -- and this is what confuses me -- I can't seem to see what 
>> antlr is griping about. According to me, and expression like
>>
>> a & b
>>
>> should not be confusable with
>>
>> &foo
>>
>> so I'm wondering why antlr seems to feel that there are different 
>> alternatives that can parse the &.
>>
>> Obviously, either there's a problem with antlr or a problem with my 
>> grammar. I'd like it to be with my grammar, since I can likely fix that.
>>
>> So my question is, how do I understand what the multiple alternatives 
>> are, here? I tried dumping the nfa/dfas, to little avail.
>>
>> Recommendations, advice?
>>
>> =Austin
>>
> Did you try ANTLRworks? Use the "check grammar" option look for rules 
> highlighted with red. Then look at syntax diagrams and decipher the 
> different paths a grammar can take to get from point A to point B.
>
> Johannes
>



More information about the antlr-interest mailing list