[antlr-interest] Simple grammar which hangs the code generator

Gavin Lambert antlr at mirality.co.nz
Tue May 20 12:48:23 PDT 2008


At 03:04 21/05/2008, Jie Lu wrote:
>While experimenting with various rules, I got to a grammar (shown 
>below) which seems to hang the code generator. In ANTLRWorks, if 
>I do Generate->Generate Code, the progress dialog comes up and 
>stays forever. The problem seems to have started when I added 
>(...)+ around the CHAR_CONTENTS rule. Does the Antlr code 
>generator normally hang on certain grammars?

Well, I'm not sure why it's freezing on you (except maybe the DFA 
combination explosion is killing it), but I can tell you that your 
grammar can't possibly work.

>charLiteral : '\'' CHAR_CONTENTS '\'';
>
>stringLiteral :    '"' STRING_CONTENTS? '"';
>
>CHAR_CONTENTS : (~('\''|'\\') | ('\\' .))+;
>
>STRING_CONTENTS : (~('"'|'\\') | ('\\' .))+;

There is no useful distinction betweeen the CHAR_CONTENTS and 
STRING_CONTENTS rules; and since they are non-fragment lexer rules 
they are both viable targets.  This will typically result in 
STRING_CONTENTS never being generated and CHAR_CONTENTS being 
generated in places you weren't expecting it to.

You should change CHAR_CONTENTS and STRING_CONTENTS into fragment 
rules and then change charLiteral and stringLiteral into lexer 
rules, not parser rules.

Perhaps you haven't realised that the lexing phase is entirely 
separate from the parser?  Lexer rules are run and generate tokens 
completely independently of how they're used in parser rules.



More information about the antlr-interest mailing list