[antlr-interest] Ok I got a working lexer and parser as per the tutorial how do I make it actually generate something?

Jarrod Roberson jarrod at vertigrated.com
Thu Sep 4 14:25:21 PDT 2008


I have this grammar file.

grammar SimpleCalc;

tokens {
    PLUS = '+';
    MINUS = '-';
    MULT = '*';
    DIV = '/';
    }

@members {
    public static void main(final String[] args) throws Exception {
        SimpleCalcLexer lex = new SimpleCalcLexer(new
ANTLRFileStream(args[0]));
           CommonTokenStream tokens = new CommonTokenStream(lex);

        SimpleCalcParser parser = new SimpleCalcParser(tokens);

        try {
            parser.expr();
        } catch (RecognitionException e)  {
            e.printStackTrace();
        }
    }
}

/* Parser Rules */

expr    :    term ((PLUS | MINUS) term)*;

term     :    factor ((MULT | DIV) factor)*;

factor     :     NUMBER;

/* Lexer Rules */

NUMBER     :     (DIGIT)+;

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

fragment DIGIT
    :     '0'..'9' ;

I have a test harness main() function that parses my input file when I call
parser.expr()

What I am having a hard time finding is information on how to make it
actually ACT on what I have parsed.
I have been searching Google for over 2 hours, reading every link that looks
even remotely informative.

I am using the Java target language.

I eventually want to be able to learn enough to write a parser for my own
language syntax to cross "compile" to another language.

So I really need to understand how to emit something from the parser.

Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080904/4eeb2dcf/attachment.html 


More information about the antlr-interest mailing list