[antlr-interest] Cannot find lexer rule

Guus Bonnema gbonnema at xs4all.nl
Thu Sep 27 01:14:56 PDT 2012


On Thu, 27 Sep 2012 09:14:04 +0200
wolfgang hämmer <wolfgang at haemmer.net> wrote:

> Dear all,
> 
> I have got an irritating problem with a very simple grammar. May be I can't
> see the forest because I am standing at a tree ;-)
> 

Hi Wolfgang,

The way you defined your expression is not clear to me.
From your example I gather that bracketed expression can be nested.

But you cannot do it that way. You gobble up everything that is not equal to
the opening bracket, including the closing bracket. And nesting seems to be out
of the question too.

Looks like you need a grammar something like:

S : 'PUT' br-expr

br-expr : '[' expr ']'

expr : <some other things> br-expr <other things>

Would that be more like what you want?
Hope this helps.

Guus.

> Here my grammar: (it is an excerpt)
> 
> grammar SRC_REP_FILE;
> 
> options {
>     language=Java;
>     backtrack=true;
>     memoize=true;
>     output=AST;
> }
> 
> start_rule
>     :
>     'PUT' SQUARED_BRACKED_EXPRESSION
>     ;
> 
> SQUARED_BRACKED_EXPRESSION
>     :
>     '[' ~('[')* ']'
>     ;
> 
> WS    :
>     (' '| '\r' | '\t' | '\n')   {$channel=HIDDEN;}
>     ;
> 
> 
> Here my input:
> 
>       PUT [<TR>]
> 
> 
> And here the results:
> 
> line 3:0 mismatched character '<EOF>' expecting ']'
> line 3:0 missing SQUARED_BRACKED_EXPRESSION at '<EOF>'
> 
> 
> What I am trying to do is to parse an expression like
> 
> 
>       PUT
>         [
> <HTML>
>   <HEAD>
>     <TITLE>[ATS_TEST_REP.PAGE_TITLE(ATS_TEST_REP.C_REP_TYPE_TEST_CASE,
> FALSE, TOP.PAR_COMPO_ID)]</TITLE>
>     [ATS_TEST_REP.CSS]
>   </HEAD>
>   <BODY>
>     <H1>[ATS_TEST_REP.PAGE_TITLE(ATS_TEST_REP.C_REP_TYPE_TEST_CASE, FALSE,
> TOP.PAR_COMPO_ID)]</H1>
>     <TABLE>
>       <TR>
>         <TH CLASS="MODULE">MODULE</TH>
>         <TH CLASS="COMPO">COMPO</TH>
>         <TH CLASS="TC">TEST CASE</TH>
>         <TH CLASS="CNT">TEST CASE/STEP #</TH>
>       </TR>
>         ]
> 
> 
> in a code where the '[' and ']' occur in other places. I would like to hide
> the lexer rule
> 
> SQUARED_BRACKED_EXPRESSION
>     :
>     '[' ~('[')* ']'
>     ;
> 
> from the rest of the grammer and call it explicitely from a parser rule like
> 
> put_statement
>     :
>     PUT SQUARED_BRACKED_EXPRESSION
>     ;
> 
> The problem is that I cannot use a 'fragment' as 'fragment' lexer rules can
> only be called from other lexer rules.
> 
> 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