Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4554262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:03:56+00:00 2026-05-21T17:03:56+00:00

I want to generate an antlr lexer at runtime — that is, generate the

  • 0

I want to generate an antlr lexer at runtime — that is, generate the grammar and from the grammar generate the lexer class, and its supporting bits at runtime. I am happy to feed it into the the java compiler, which is accessible at runtime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:03:57+00:00Added an answer on May 21, 2026 at 5:03 pm

    Here’s a quick and dirty way to:

    1. generate a combined (!) ANTLR grammar .g file given a String as grammar-source,
    2. and create a Parser & Lexer from this .g file,
    3. compile the these Parser & Lexer .java files,
    4. create instances of the Parser & Lexer classes and invoke the entry point of the parser.

    Main.java

    import java.io.*;
    import javax.tools.*;
    import java.lang.reflect.*;
    import org.antlr.runtime.*;
    import org.antlr.Tool;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
    
            // The grammar which echos the parsed characters to theconsole,
            // skipping any white space chars.
            final String grammar =
                    "grammar T;                                                  \n" +
                    "                                                            \n" +
                    "parse                                                       \n" +
                    "  :  (ANY {System.out.println(\"ANY=\" + $ANY.text);})* EOF \n" +
                    "  ;                                                         \n" +
                    "                                                            \n" +
                    "SPACE                                                       \n" +
                    "  :  (' ' | '\\t' | '\\r' | '\\n') {skip();}                \n" +
                    "  ;                                                         \n" +
                    "                                                            \n" +
                    "ANY                                                         \n" +
                    "  :  .                                                      \n" +
                    "  ;                                                           ";
            final String grammarName = "T";
            final String entryPoint = "parse";
    
            // 1 - Write the `.g` grammar file to disk.
            Writer out = new BufferedWriter(new FileWriter(new File(grammarName + ".g")));
            out.write(grammar);
            out.close();
    
            // 2 - Generate the lexer and parser.
            Tool tool = new Tool(new String[]{grammarName + ".g"});
            tool.process();
    
            // 3 - Compile the lexer and parser.
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            compiler.run(null, System.out, System.err, "-sourcepath", "", grammarName + "Lexer.java");
            compiler.run(null, System.out, System.err, "-sourcepath", "", grammarName + "Parser.java");
    
            // 4 - Parse the command line parameter using the dynamically created lexer and 
            //     parser with a bit of reflection Voodoo :)
            Lexer lexer = (Lexer)Class.forName(grammarName + "Lexer").newInstance();
            lexer.setCharStream(new ANTLRStringStream(args[0]));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            Class<?> parserClass = Class.forName(grammarName + "Parser");
            Constructor parserCTor = parserClass.getConstructor(TokenStream.class);
            Parser parser = (Parser)parserCTor.newInstance(tokens);
            Method entryPointMethod = parserClass.getMethod(entryPoint);
            entryPointMethod.invoke(parser);
        }
    }
    

    Which, after compiling and running it like this (on *nix):

    java -cp .:antlr-3.2.jar Main "a b    c"
    

    or on Windows

    java -cp .;antlr-3.2.jar Main "a b    c"
    

    , produces the following output:

    ANY=a
    ANY=b
    ANY=c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want generate static pages from database. What is a better suggestion that a
Let's say I have a class of 30 students and want generate every possible
I want to generate a password reset token for a User model that I
I want to generate geoJson data for an country and its all the states.
I am writhing a simple language with antlr, I defined a Lexer grammar in
I'm an ANTLR novice that's trying to update an early ANTLR 3.1 grammar to
I want generate a list of numbers from 0000 to 9999. I would then
I want to generate heat map from a set of data which is latitude
I want to generate PDF files or convert the existing PDF files that uses
I want to generate raw xml from a REST service using Spring. Something is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.