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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:54:50+00:00 2026-05-17T23:54:50+00:00

I have a file with a specific format that I would like to parse.

  • 0

I have a file with a specific format that I would like to parse. In this file I have a number on a line which specifies the number of lines to follow it.

example excerpt from the file:

3 // number of lines to follow = 3
1 3 // 1st line
3 5 // 2nd line
2 7 // 3rd line

I want to read the number (3 in this example) and then read the following 3 lines only. If there are less or more lines I want to generate an error. How can I do this in javacc?

Thank you

  • 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-17T23:54:51+00:00Added an answer on May 17, 2026 at 11:54 pm

    Here’s a way:

    /* file: Lines.jj */
    options {
        STATIC = false ;
    }
    
    PARSER_BEGIN(Lines)
    
    class Lines {
        public static void main(String[] args) throws ParseException, TokenMgrError {
            Lines parser = new Lines(System.in);
            System.out.println("diff="+parser.parse());        
        }
    }
    
    PARSER_END(Lines)
    
    SKIP  : { " " }
    TOKEN : { <NL : ("\r\n" | "\n" | "\r")> }
    TOKEN : { <NUMBER : (["0"-"9"])+> }
    
    int parse() :
    {
        int linesToRead = 0;
        int linesRead = 0;
        Token n = null;
    }
    {
        n = <NUMBER> <NL> {linesToRead = Integer.parseInt(n.image);}
        (
            (<NUMBER>)+ <NL> {linesRead++;} 
        )*
        <EOF>
        {return linesToRead - linesRead;}
    }
    

    The Lines.parse() : int method returns the difference between the number of lines it is supposed to read, and the actual number of lines that are read.

    Generate the parser like this:

    javacc Lines.jj
    

    compile all classes:

    java *.java
    

    And feed it your test data (test.txt):

    3
    1 3
    3 5
    2 7
    

    like this:

    java Lines < test.txt
    

    which produces the output:

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

Sidebar

Related Questions

I have a CSV-like text file that has about 1000 lines. Between each record
I have a script that searches for a directory containing a specific file, starting
I'm using .NET 3.5. Say I have a method that accesses a specific file,
I have a bat file that I want to run at specific day/time in
I have this code to move my uploaded file to a specific directory: if
I would like to rename a large number of columns (column headers) to have
I have a SQlite3 table that has typeless columns like in this example: CREATE
i have an edited version of a config file specific for my machine. i
I have downloaded an excel file where specific french characters such as é appear
I want to trace the modification history of a specific file. Does linux have

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.