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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:02:50+00:00 2026-05-28T20:02:50+00:00

I want to parse a data file like this one (fictive example): Name: bob

  • 0

I want to parse a data file like this one (fictive example):

Name: bob
Age: 14
-----
Name: alice
-----

Let’s suppose, for this example, that the format of the file is complex enough that I don’t want to code it directly. I prefer using ANTLR to get a better parser.

Here is the question: how, with ANTLR, can I map this data to a structure, like a list ? I don’t need the complete grammar but only an explanation of how I can map data like that to a data struture.

  • 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-28T20:02:51+00:00Added an answer on May 28, 2026 at 8:02 pm

    I couldn’t find the answer that BA-S posted in a comment under your question, so I started writing a new answer. Without much explanation (read the other answer for more info), here is an example of how you could parse that simple input using ANTLR into a List<Person>.

    The grammar describing your input:

    grammar T;
    
    parse
     : person* EOF
     ;
    
    person
     : Name Word (Age Number)? Separator
     ;
    
    Name
     : 'Name:'
     ;
    
    Age
     : 'Age:'
     ;
    
    Word
     : ('a'..'z')+
     ;
    
    Number
     : ('0'..'9')+
     ;
    
    Separator
     : '-----'
     ;
    
    Space
     : (' ' | '\t' | '\r' | '\n') {skip();}
     ;
    

    The same grammar but then including embedded code:

    grammar T;
    
    parse returns [List<Person> persons]
    @init{$persons = new ArrayList<Person>();}
     : (person {$persons.add($person.p);})* EOF
     ;
    
    person returns [Person p]
     : Name Word (Age Number)? Separator {$p = new Person($Word.text, $Number.text);}
     ;
    
    Name
     : 'Name:'
     ;
    
    Age
     : 'Age:'
     ;
    
    Word
     : ('a'..'z')+
     ;
    
    Number
     : ('0'..'9')+
     ;
    
    Separator
     : '-----'
     ;
    
    Space
     : (' ' | '\t' | '\r' | '\n') {skip();}
     ;
    

    And a small test class (with the class Person):

    import org.antlr.runtime.*;
    import org.antlr.runtime.tree.*;
    import org.antlr.stringtemplate.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        TLexer lexer = new TLexer(new ANTLRFileStream("test.txt"));
        TParser parser = new TParser(new CommonTokenStream(lexer));
        java.util.List<Person> persons = parser.parse();
        System.out.println(persons);
      }
    }
    
    class Person {
    
      final String name;
      final int age;
    
      public Person(String nm, String num) {
        name = nm;
        age = num == null ? -1 : Integer.valueOf(num);
      }
    
      @Override
      public String toString() {
        return String.format("{name=%s, age=%d}", name, age);
      }
    }
    

    where test.txt contains:

    Name: bob
    Age: 14
    -----
    Name: alice
    -----
    

    If you now run Main, the following will be printed:

    [{name=bob, age=14}, {name=alice, age=-1}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having one XML file and I want to parse only useful data
i want parse xml file, which does't have xml extension, like this: http://bizonek.wrzuta.pl/xml/plik/1ANdXCgTOit/unknow/undefined/643/ my
I want to parse and filter a file that looks like this: @@1 Row
Scenario I want to parse a large CSV file and inserts data into the
I want to parse a config file sorta thing, like so: [KEY:Value] [SUBKEY:SubValue] Now
I've got a string like this: #################### Section One #################### Data A Data B
I have a XML file like this: <Document> <Tests> <Test> <Name>A</Name> <Value>1</Value> </Test> <Test>
I want to get, via ajax, a collection of data objects and parse them
I want to parse an Apache access.log file with a python program in a
Structs seem like a useful way to parse a binary blob of data (ie

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.