ANTLR Grammar Question

Amal <akhailtash@gmail.com>
14 May 2007 07:14:27 -0700

          From comp.compilers

Related articles
ANTLR Grammar Question akhailtash@gmail.com (Amal) (2007-05-14)
Re: ANTLR Grammar Question cfc@shell01.TheWorld.com (Chris F Clark) (2007-05-16)
Re: ANTLR Grammar Question idbaxter@semdesigns.com (Ira Baxter) (2007-05-18)
| List of all articles for this month |

From: Amal <akhailtash@gmail.com>
Newsgroups: comp.compilers
Date: 14 May 2007 07:14:27 -0700
Organization: Compilers Central
Keywords: lex, PCCTS, question

I am trying to create a Lexer that recognizes input as follows:


    $comment
        Any text including whitespace here...
    $end


I defined a simple Lexer grammar like:


    WS
        :
        ( ' '
        | '\t'
        | '\r'
        | '\n'
        )+
        {$channel=HIDDEN;}
        ;


    comment : '$comment' TK_TEXT '$end' ;


    TK_TEXT
        :
        ( {input.LA(2)!='e' && input.LA(3)!='n' && input.LA(4)!='d'}?=>
'$'
            | ' '
            | '\t'
            | '\r'
            | '\n'
            | ~('$' | ' ' | '\t' | '\r' | '\n')
        )+
        ;


Note that TK_TEXT can have any character including whitespace.
Somehow, if I input the following text:


    $comment
        Any text including whitespace here...
    $end


and dump all the tokens, I get:


    Token: [$comment
        Any text including whitespace here...
    ]
    line 3:0 no viable alternative at character '$'
    ------------------------------------------------
    Token: [end]


What am I doing wrong? And how do I fix this? Any suggestions or
pointers is appreciated.


-- Amal


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.