[antlr-interest] My parsing brackets logic fails somewhere

Imre András iar73 at freemail.hu
Wed Nov 5 17:18:05 PST 2008


Hello,

I'd like to extract definitions from free text for further processing by another grammar. All I care about are the definition sections. I consider all lines containing '::=' as the beginning of a definition. A definition section ends at either the end of the line, or if there is an opening bracket on the first line, at the outermost closing bracket (and might span multiple lines).

Please find my grammar attempt at the bottom.

My bracketed rule does not work. The {p>0}? conditions always fail, and I don't have a clue why. This should be able to track the recursion level, but it does not work.

What am I getting wrong here?


Thanks,
  András

--- grammar file start ---

grammar specreader;

options { backtrack = true; }

@members {
    int p;
}

start: {p = 0; System.out.println("@line "+p);} line*;
                
line : singleLineDef | multiLineDef | anyline;

anyline : nonlineends LINEEND ;

singleLineDef : nondefines DEFINE nonlbrackets LINEEND ;

multiLineDef : nondefines DEFINE nonlbrackets bracketed nonlineends LINEEND ;

bracketed :  LBRACKET {++p; System.out.println("@bracketed "+p);}
             nonbrackets
             bracketed* {System.out.println("@bracketed after embedded "+p);}
             {p>0}? => RBRACKET {--p; System.out.println("@bracketed after RBRACKET "+p);}
             {p>0}? => nonbrackets ;

nonlbrackets : (~LBRACKET)*;

nonbrackets : (~(LBRACKET|RBRACKET))*;

nonlineends : (~('\n'|'\r'))*;

nondefines : (~DEFINE)*;


DEFINE : '::=';

LINEEND : '\n'|'\r'|EOF;

LBRACKET : '{';

RBRACKET : '}';

ANY : .;

--- grammar file end ---


More information about the antlr-interest mailing list