[antlr-interest] Trouble with statement block,

Bryan Ewbank ewbank at gmail.com
Tue Jun 21 09:45:58 PDT 2005


If I understand, you want to relax this rule:

> statement_block : LCURL (statement)? (STATEMENT_SEP statement)* RCURL

I use the following, more or less:

  statement_block : LCURL^ ( statement_list )? RCURL! ;
  statement_list : statement ( STATEMENT_SEP ( statement_list )? )? ;

It's also handy for allowing a trailing comma in an enum list.

The recursive definition of statement_list is the key to avoiding some
really weird problems and ambiguity.  It also allows the trailing
STATEMENT_SEP, but does not require it, and requires one STATEMENT_SEP
between statements.

Note that since ";" is a statement SEPARATOR, as shown by the token
name and by construction of the grammar, you will need to have ";"
between any pair of statements - even statement blocks.

If you want to make the ";" optional after a statement_block, then you
will need to change your view of ";" from SEPARATOR to TERMINATOR for
those statements that require it - expressions, return, and the like.


On 6/21/05, Craig Main <craig at palantir.co.za> wrote:
> I am struggling to get the statement block to work with extra separators. 
> I need to try and make this more lenient. 
>
> If (cond) { 
> } else { 
>             A = 10; <separator not allowed here> 
> } ; <separator required here> 
> What is a good way to make this more lenient in terms of the input string? I
> am struggling to get it correct without invalid grammar rules.


More information about the antlr-interest mailing list