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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:28:00+00:00 2026-06-06T21:28:00+00:00

I’m missing some basic knowledge. Started playing around with ATLR today missing any source

  • 0

I’m missing some basic knowledge. Started playing around with ATLR today missing any source telling me how to do the following:

I’d like to parse a configuration file a program of mine currently reads in a very ugly way. Basically it looks like:

A [Data] [Data]
B [Data] [Data] [Data]

where A/B/… are objects with their associated data following (dynamic amount, only simple digits).
A grammar should not be that hard but how to use ANTLR now?

  • lexer only: A/B are tokens and I ask for the tokens he read. How to ask this and how to detect malformatted input?
  • lexer & parser: A/B are parser rules and… how to know the parser processed successfully A/B? The same object could appear multiple times in the file and I need to consider every single one. It’s more like listing instances in the config file.

Edit:
My problem is not the grammer but how to get informed by parser/lexer what they actually found/parsed? Best would be: invoke a function upon recognition of a rule like recursive descent

  • 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-06T21:28:01+00:00Added an answer on June 6, 2026 at 9:28 pm

    ANTLR production rules can have return value(s) you can use to get the contents of your configuration file.

    Here’s a quick demo:

    grammar T;
    
    parse returns [java.util.Map<String, List<Integer>> map]
    @init{$map = new java.util.HashMap<String, List<Integer>>();}
     : (line {$map.put($line.key, $line.values);} )+ EOF
     ;
    
    line returns [String key, List<Integer> values]
     : Id numbers (NL | EOF)
       {
         $key = $Id.text;
         $values = $numbers.list;
       }
     ;
    
    numbers returns [List<Integer> list]
    @init{$list = new ArrayList<Integer>();}
     : (Num {$list.add(Integer.parseInt($Num.text));} )+
     ;
    
    Num   : '0'..'9'+;
    Id    : ('a'..'z' | 'A'..'Z')+;
    NL    : '\r'? '\n' | '\r';
    Space : (' ' | '\t')+ {skip();};
    

    If you runt the class below:

    import org.antlr.runtime.*;
    import java.util.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        String input = "A 12 34\n" +
                       "B 5 6 7 8\n" +
                       "C 9";
        TLexer lexer = new TLexer(new ANTLRStringStream(input));
        TParser parser = new TParser(new CommonTokenStream(lexer));
        Map<String, List<Integer>> values = parser.parse();
        System.out.println(values);
      }
    }
    

    the following will be printed to the console:

    {A=[12, 34], B=[5, 6, 7, 8], C=[9]}
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.