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 8702213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:35:48+00:00 2026-06-13T02:35:48+00:00

I did find an answer to my question: Barts answer is exactly why I

  • 0

I did find an answer to my question: Barts answer is exactly why I need, but it does not function (see below).

Please can some one either give me a working example or show me the where I am going wrong to implement Barts answer?

This is what I get for the other answer, I get the hereunder error from “part 4”

Lexer lexer = (Lexer)Class.forName(grammarName + "Lexer").newInstance();

I’ve got antlr3.4 complete and JDK libraries. I created it as package createclass; yet I find the answer strange as the class to create has no package???
So I tried adding to the string:

"@lexer::header {\n" +
"   package createclass;\n" +
"}        \n" +
"@parser::header {\n" +
"   package createclass;\n" +
"}\n" +

But still no change.

Here’s the output:

debug:
Exception in thread "main" java.lang.ClassNotFoundException: TLexer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at createclass.Createclass.main(Createclass.java:61)
Java Result: 1
BUILD SUCCESSFUL (total time: 31 seconds)

The example code given is:

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);
    }
}
  • 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-06-13T02:35:50+00:00Added an answer on June 13, 2026 at 2:35 am

    My guess is that you’re using an IDE for this, and don’t have your classpath set properly. The demo from my answer works (also using v3.4). Here’s what I just did:

    I copied the file Main.java in a directory (~/Temp/demo, in my case) and opened a shell in this directory and did the following:

    bart@hades:~/Temp/demo$ java -version
    > java version "1.6.0_24"
    > OpenJDK Runtime Environment (IcedTea6 1.11.4) (6b24-1.11.4-1ubuntu0.12.04.1)
    > OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
    
    bart@hades:~/Temp/demo$ wget http://www.antlr.org/download/antlr-3.4-complete.jar
    > --2012-10-17 19:26:01--  http://www.antlr.org/download/antlr-3.4-complete.jar
    > Resolving www.antlr.org (www.antlr.org)... 138.202.170.10
    > Connecting to www.antlr.org (www.antlr.org)|138.202.170.10|:80... connected.
    > HTTP request sent, awaiting response... 200 OK
    > Length: 2388361 (2.3M) [application/java-archive]
    > Saving to: `antlr-3.4-complete.jar'
    > 
    > 100%[===============================================================================================================>] 2,388,361    317K/s   in 8.9s    
    > 
    > Last-modified header invalid -- time-stamp ignored.
    > 2012-10-17 19:26:11 (261 KB/s) - `antlr-3.4-complete.jar' saved [2388361/2388361]
    
    bart@hades:~/Temp/demo$ javac -cp antlr-3.4-complete.jar *.java
    
    bart@hades:~/Temp/demo$ java -cp .:antlr-3.4-complete.jar Main "a b    c"
    > ANY=a
    > ANY=b
    > ANY=c

    (the > is output printed to the console)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick and simple question I did not find an answer for: How does TFS
I did do some searching, but can't find an answer to simple question. What
I did not find an answer to this question. I have a VOIP application.
I searched the site but did not find the answer I was looking for
I tried searching, but did not find a specific post that could answer my
Greetings, I have the following question. I did not find the exact answer for
I've searched quite a while but I did not find an answer for my
I did not find the answer so i ask the question... Sorry to bother.
Simple question, but one I can't find an answer to on the MSDN subscriber
i searched a lot for an answer to this question but i did not

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.