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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:27:39+00:00 2026-05-25T17:27:39+00:00

Example String 023abc7defghij Header Characters 0, 1 = Size of following chunks Chunks First

  • 0

Example String

023abc7defghij

Header

Characters 0, 1 = Size of following chunks

Chunks

First character = length of following string String

Following characters = String with the specified length

Example result

So in the upper example this would mean:

02 -> 2 following chunks

3 -> 3 character String will follow

abc -> the three character string

7 -> 7 character String will follow

defghij -> the seven character string

Question

Can I write a grammar, that describes this form of a string?
I would need to interpret the ‘length’ informations and then build tokens with the specified lenght to fill my objects with the length informations and the strings.

I hope I could describe this comprehensible. I could not find information, describing or solving my problem.

  • 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-25T17:27:39+00:00Added an answer on May 25, 2026 at 5:27 pm

    I’m assuming your actual problem is a bit more complicated, because if "023abc7defghij" is your actual input, I wouldn’t use a parser generator like ANTLR, but just stick with some simple string-operations.

    That said, here’s a possible solution:

    Since your chunks are not known up front, you cannot create any tokens other than a single Digit and an Other token that would be any char other than a digit. Note that you don’t really need the header information: you simply parse "3" and then get the next 3 chars, then parse the "7" and get the next 7 chars, … all the way up to the end of the file.

    A grammar for such a language could look like this:

    grammar T;
    
    parse
      :  file EOF
      ;
    
    file
      :  header chunk*
      ;
    
    header
      :  Digit Digit
      ;
    
    chunk
      :  Digit any*
      ;
    
    any
      :  Digit
      |  Other
      ;
    
    Digit
      :  '0'..'9'
      ;
    
    Other
      :  .
      ;
    

    But now the chunk rule is ambiguous: it does not now when to stop consuming characters. This can be done using a gated semantic predicate that will cause the * from any* to stop consuming when a certain condition has been met (when a counter int n has been counted down, in this case).

    The grammar above including this predicate and some println-statements would look like this:

    grammar T;
    
    parse
      :  file EOF
      ;
    
    file
      :  header {System.out.println("header=" + $header.text);}
         (chunk {System.out.println("chunk=" + $chunk.text);})*
      ;
    
    header
      :  Digit Digit
      ;
    
    chunk
      :  Digit {int n = Integer.valueOf($Digit.text);} ({n > 0}?=> any {n--;})*
      ;
    
    any
      :  Digit
      |  Other
      ;
    
    Digit
      :  '0'..'9'
      ;
    
    Other
      :  .
      ;
    

    which can be tested with the class:

    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        String source = "023abc7defghij";
        TLexer lexer = new TLexer(new ANTLRStringStream(source));
        TParser parser = new TParser(new CommonTokenStream(lexer));
        parser.parse();
      }
    }
    

    If you now generate a lexer and parser, compile all .java file and run the Main class:

    java -cp antlr-3.3.jar org.antlr.Tool T.g
    javac -cp antlr-3.3.jar *.java
    java -cp .:antlr-3.3.jar Main
    

    you would see the following being printed to your console:

    header=02
    chunk=3abc
    chunk=7defghij
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm always struggling with something like the following Java example: String breads[] = {Brown,
I've a Java String with new lines(\n), say for example String value = This
What is the best way to parse time from a string? Example string: this
For example I have a string that looks like the following: var example_string =
This is the example string: User.status <> 'actived' I want this: `User`.`status` <> 'actived'
I have collection of similar strings for example : string 1: Customer's first Name
For every string, I need to print # each 6 characters. For example: example_string
Note: Bash 3.00 How to substitute this example string 123456789 , to look like
My NSDateFormatter is retuning nil. This is an example string that I want to
for example string test = extract substring from a string; // extract this in

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.