[antlr-interest] PS: Re: Java API for checking ambiguity in grammars using ANLTR

Stephen Gaito stephen at perceptisys.co.uk
Tue Jun 12 01:21:49 PDT 2012


Sathay,

I forgot to mention that all of the important code for your actual
programmatic use is located in the org.antlr.tool package... to start
tracing through it see:
> https://github.com/antlr/antlr3/blob/master/tool/src/main/java/org/antlr/tool/Grammar.java

Again I hope this helps!

Regards,
Stephen Gaito


On 12/06/12 08:15, Stephen Gaito wrote:
> Sathay,
>
> An interesting puzzle ;-)
>
> So AntlrWorks detects ambiguous grammars in the analyze method in the
>> org/antlr/works/grammar/antlr/ANTLRGrammarEngineImpl.java
> class. (see:
>> https://github.com/antlr/antlrworks/blob/master/src/org/antlr/works/grammar/antlr/ANTLRGrammarEngineImpl.java
>  and search for "analyze" ).
>
> It does this by setting an error listener to catch all errors coming
> from the ANTLR tool's compilation of the grammar. Essentially across the
> following code:
>> Grammar g = getDefaultGrammar();
>> ...
>> List rules = g.checkAllRulesForLeftRecursion();
>> ...
>> if ( g.nfa==null ) {
>>   g.composite.createNFAs();
>> }
>> g.createLookaheadDFAs();
>> if(engine.isCombinedGrammar()) {
>>   // If the grammar is combined, analyze also the lexer
>>   if(lexerGrammar != null) {
>>     lexerGrammar.composite.createNFAs();
>>     lexerGrammar.createLookaheadDFAs();
>>   }
>> }
> AntlrWorks reports the ambiguous grammar messages by looking for the
> following messages/exceptions which have been caught and stored in a
> list by the ErrorListener:
>> private void buildError(Object o) {
>>   if(o instanceof *GrammarUnreachableAltsMessage*)
>>    
>> errors.add(buildUnreachableAltsError((GrammarUnreachableAltsMessage)o));
>>   else if(o instanceof *GrammarNonDeterminismMessage*)
>>     
>> errors.add(buildNonDeterministicError((GrammarNonDeterminismMessage)o));
>>   else if(o instanceof *NonRegularDecisionMessage*)
>>     
>> errors.add(buildNonRegularDecisionError((NonRegularDecisionMessage)o));
>> }
> *SO....*
>
> If you wanted to programatically check for ambiguous grammars, you could
> simulate the code in this ANTLRGrammarEngineImpl class in a try { }
> "block" and catch the above messages.
>
> You could also infer this on the command line by looking at the output
> of the antlr -Xdfa command.... but this is rather harder as there is no
> /*explicit*/ statement that the grammar is ambiguous....
>
> I hope this helps!
>
> Regards,
> Stephen Gaito
>
> On 12/06/12 04:13, sathya moorthy wrote:
>> I want to know whether the grammar is ambiguous by calling some ANTLR API
>> but I want to know is there such API?
>> The purpose is not to generate files from the grammar if it is ambiguous.
>>
>> Antlr generates files even if it is ambiguous(in some cases)
>>
>> On Mon, Jun 11, 2012 at 2:53 PM, Loring Craymer <lgcraymer at yahoo.com> wrote:
>>
>>> You want code generated even if the grammar is reported to be ambiguous.
>>>  ANTLR tells you how it handles ambiguity (some alternatives are disabled);
>>> if this is not what you want, fix your grammar.
>>>
>>> --Loring
>>>
>>>
>>>   ------------------------------
>>> *From:* sathya moorthy <brsmoorthy at gmail.com>
>>> *To:* antlr-interest at antlr.org
>>> *Sent:* Monday, June 11, 2012 1:54 AM
>>> *Subject:* Re: [antlr-interest] Java API for checking ambiguity in
>>> grammars using ANLTR
>>>
>>> Guys any help would be great
>>> thanks
>>> Sathya
>>>
>>> On Sat, Jun 9, 2012 at 1:40 PM, sathya moorthy <brsmoorthy at gmail.com>
>>> wrote:
>>>
>>>> Hi,
>>>>
>>>> When i use ANTLR IDE, it suggests that the grammar is ambiguous but still
>>>> i am able to generate code using that grammar.
>>>> Is there a specific API in ANTLR which will tell whether the grammar is
>>>> ambiguous?
>>>> If the grammar is ambiguous i dont want to generate the code for that
>>>> specific file.
>>>>
>>>> Regards,
>>>> Sathya
>>>>
>>>>
>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>> Unsubscribe:
>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>
>>>
>>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list