[antlr-interest] Inserting a token in the input stream

Filipe David Manana fdmanana at ieee.org
Sat Feb 28 03:59:26 PST 2009


Hello,

Is it possible to do something in a rule's action that either makes the
parser finish (with success status) or insert a token in the input stream?

I need something like that for a JavaScript grammar. Considering the
following rules:

program
    : sourceElements EOF
    ;

sourceElements
    : LT* ( sourceElement LT* )*
    ;

sourceElement
    : functionDeclaration
    | statement
    ;

statement
    : emptyStatement
    | variableStatement
    | blockStatement
    ;

variableStatement
    : 'var'  LT*  variableDeclarationList  ( LT | ';'  )
    ;


If the input stream is for e.g. "var test" (no newline or semi-colon before
the EOF), my parser fails. ANTLRWorks debuggers shows "NoViableAltException"
nodes coming from the "variableDeclarationList" rule.

If I change my rules to:

program
    : sourceElements
    ;

/* rules in between remain unchanged */

variableStatement
    : 'var'  LT*  variableDeclarationList  ( LT | ';' | EOF )
    ;

The same thing happens.

So my initial thought is to add a ';' for e.g. into the input stream if an
EOF is following the "variableDeclarationList" ub tge "variableStatement"
rule.

As a complete beginner to ANTLR, any suggestions are very welcome.

cheers


-- 
Filipe David Manana,
fdmanana at ieee.org
PGP key - http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xC569452B

"Reasonable men adapt themselves to the world.
Unreasonable men adapt the world to themselves.
That's why all progress depends on unreasonable men."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090228/fe0bd573/attachment.html 


More information about the antlr-interest mailing list