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

  • Home
  • SEARCH
  • 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 6599397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:29:09+00:00 2026-05-25T18:29:09+00:00

I want to use parser actions with basic file io (Java), e. g. PrintWriter

  • 0

I want to use parser actions with basic file io (Java), e. g. PrintWriter in an ANTLR grammar. Must I use the superClass option or can I use @header? In both cases how can I declare the PrintWriter-object and how must I handle the exceptions?

  • 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-25T18:29:09+00:00Added an answer on May 25, 2026 at 6:29 pm

    The option superClass=... is used to let your Parser extend a custom class. So, I don’t think that is what you’re after.

    Everything inside the @header section will be placed at the start of your Parser class. This is used to import classes:

    @header {
      import java.io.PrintWriter;
    }
    

    Note that @header {...} is short for @parser::header {...}. You can also define: @lexer::header {...} for your lexer.

    And inside @member {...} (or: @parser::member {...}, @lexer::member {...}) sections, you can add instance variables and methods that can be used inside either the Parser or Lexer:

    @header {
      import java.io.PrintWriter;
    }
    
    @members {
      PrintWriter writer;
    }
    

    A small demo of a grammar whose parser will write the parsed numbers to a specific writer:

    grammar T;
    
    @header {
      import java.io.PrintWriter;
    }
    
    @members {
      PrintWriter writer;
    
      public TParser(TokenStream input, String fileName) {
        super(input);
        try {
          writer = new PrintWriter(fileName);
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
    
    parse
      :  numbers EOF
      ;
    
    numbers
      :  (NUM {
                writer.println("parsed: " + $NUM.text);
                writer.flush();
              }
         )+
      ;
    
    NUM : '0'..'9'+;
    WS  : ' ' {skip();};
    

    which can be tested with:

    import java.io.File;
    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        String source = "42 500000000 666";
        TLexer lexer = new TLexer(new ANTLRStringStream(source));
        TParser parser = new TParser(new CommonTokenStream(lexer), "log.txt");
        parser.parse();
      }
    }
    

    If you run the class above, a file called log.txt has been created containing:

    parsed: 42
    parsed: 500000000
    parsed: 666
    

    Note that there is a strict order of all these @... and options {...} etc. instances:

    1. grammar definition
    2. options block (no @ sign!)
    3. tokens block (no @ sign!)
    4. @header block
    5. @members block

    grammar T;
    
    options {
      // options here
    }
    
    tokens {
      // imaginary tokens here
    }
    
    @header  { 
      // ... 
    }
    
    @members { 
      // ... 
    }
    

    EDIT

    ANTLRStarter wrote:

    How can I add code which is executed at the end of the the lexer/parser class?

    There’s no built-in functionality for such a thing. But you can easily create a custom method wrapUp() in your parser:

    @members {
    
      // ...
    
      private void wrapUp() {
        // wrap up your parser in here
      }
    }
    

    and then automatically call that method from the entry point of your grammar like this:

    parse
    @after {this.wrapUp();}
      :  numbers EOF
      ;
    

    Any code placed in the @after {...} block of a rule is executed when everything in the rule is properly matched.

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

Sidebar

Related Questions

I want to use an html parser that does the following in a nice,
I want to use the php simple HTML DOM parser to grab the image,
I would like to use Parsec's makeTokenParser to build my parser, but I want
I have a transaction log file in CSV format that I want use to
I want to parser a simple matlab-like for-loop, using ANTLR. The loop is like
I'm trying to use Bison to generate a parser, in C++. The grammar is
I want use groovy findAll with my param to filtering closure filterClosure = {
i want use some data from a website with web service. i have a
Below is my stored procedure. I want use stored procedure select all row of
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom

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.