[antlr-interest] Syntactic predicate - simple case

Loring Craymer lgcraymer at yahoo.com
Tue Sep 5 22:20:57 PDT 2006


1.) With ANTLR 2, STARTCHUNK should be a "protected" rule; with ANTLR 3, it should be a "fragment.

2.)  synpreds distinguish alternatives--you need a rule that looks something like

CHUNK
    :
    ( STARTCHUNK (WS)? "/>" )=> ( STARTCHUNK(WS)? "/>" ) 
        { $setType(EMTYTAG); }
    |
       ( STARTCHUNK (WS)? '>' ) 
        { $setType(STARTTAG); }
    ;

but it is still better to simply left factor:

LCHUNK
    :
    STARTCHUNK (WS)?
    (    "/>"     { $setType(EMTYTAG); }
        |
            '>'     { $setType(STARTTAG); }
    ;

--Loring

Timothy Washington <timothyjwashington at yahoo.ca> wrote: 
Hey all, I'm trying to write some lexer rules for XML
 and . Now both tags can start
with "" while
an empty tag ends with "/>". Having similar lexer
rules for both gives me a lexical non-determinism for
the first chunk of text. I think I can use a syntactic
predicate for this problem, but I'm still getting
errors with the following syntax:

I'missing something basic here. If I want to match on
the ">" for the EMTYTAG, should my rule look like A.
B. or C.

A.        ( STARTCHUNK (WS)? "/>" )=>  g:( STARTCHUNK
(WS)? "/>" ) 
B.        ( STARTCHUNK )=>     g:( STARTCHUNK (WS)?
"/>" ) 
C.        ( STARTCHUNK )=>     g:( "/>" ) 



Lexer Rules: 
EMPTYTAG: 
        ( STARTCHUNK (WS)? "/>" )=> g:( STARTCHUNK
(WS)? "/>" )   //*** line 150
        { System.out.println("EMTYTAG: "+g.getText());
 };

STARTTAG: 
        ( STARTCHUNK (WS)? '>' )=> g:( STARTCHUNK
(WS)? '>' ) 
        { System.out.println("STARTTAG:
"+g.getText()); };

STARTCHUNK: 
        ( '<' g:NAME ( WS )? ( ATTR ( WS )? )* )      

        { System.out.println("STARTCHUNK:
"+g.getText()); } ;


Errors:
grammar/bookkeeping.lexer.g: warning:lexical
nondeterminism between rules EMPTYTAG and STARTCHUNK
upon
...
grammar/bookkeeping.lexer.g: warning:lexical
nondeterminism between rules EMPTYTAG and STARTTAG
upon
...
grammar/bookkeeping.lexer.g: warning:lexical
nondeterminism between rules STARTCHUNK and STARTTAG
upon
...
grammar/bookkeeping.lexer.g:150: warning:Syntactic
predicate ignored for single alternative ()


Tim



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


 		
---------------------------------
 All-new Yahoo! Mail - Fire up a more powerful email and get things done faster.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060905/0898f104/attachment.html 


More information about the antlr-interest mailing list