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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:27:51+00:00 2026-05-22T22:27:51+00:00

I have a text file which I’m trying to parse. The file looks like

  • 0

I have a text file which I’m trying to parse. The file looks like this:

A_1: - A_10:
Some text.
----------
Some more text.
__________

B_1: - B_5:
Still more text - it may contain dashes as well.
----------
Even more text. Could be multiple sentences with newlines.
Like this.
__________

And so on.

I’m trying to separate parsing/tokenization between bison and flex. I’ve managed to parse the header (A_1: - A_10:) using the following regular expressions in flex:

[ \t]+ ;               // ignore whitespace
[A-Z]_[0-9]+(_[0-9]+)? { ... return ID; }

in combination with a rule in my grammar to combine the two IDs:

header:        ID ':' '-' ID ':'

However, the next segment of text is causing some trouble. I’m pretty sure I need to include start conditions in the lexer (e.g. to only ignore whitespace when parsing the header). I’ve tried to define a token TEXT and parse the whole thing up until ---------- as a single token. Still can’t figure out if this is a reasonable thing to do.

The other possibility I can think of is to have a rule in the grammar which would combine the text segment using tokens like WORD, SPACE, DASH, NEWLINE and every other possible character. Does it even make sense?

So now I’m stuck trying to parse those text segments. Am I using the right tools for the job. I will appreciate your help, thanks.

  • 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-22T22:27:52+00:00Added an answer on May 22, 2026 at 10:27 pm

    This is what lex start states were designed for. Basically, you declare a start state for each different language you need to deal with (two in your case — headers and bodies) and then tag the rules based on which state they apply to. So you’d want something like:

    %s header
    %s body
    %%
    <header>[ \t\n]                   ; /* ignore */
    <header>[_a-zA-Z][_a-zA-Z0-9]*    { ... return ID; }
    <header>[-:,.;()]                 { return *yytext; }
    
    <body>^----------$                { yylval.text = GetStoredText(); return SECTION_SPLIT; }
    <body>^__________$                { yylval.text = GetStoredText(); return SECTION_END; }
    <body>.                           { StoreText(*yytext);
    %%
    void BeginHeader() { BEGIN header; }
    void BeginBody() { BEGIN body; }
    

    Where StoreText is a function that stores characters into a buffer (something like a std::stringstream if you’re using C++) and GetStoredText returns all the text stored since the last call and clears the buffer. Then your yacc/bison code will look something like:

    input: entry | input entry ;
    entry: header body ;
    header: ..something to match a header.. { BeginBody(); };
    body: sections SECTION_END { BeginHeader(); };
    sections: /*empty*/ | sections SECTION_SPLIT ;
    

    Of course, you’ll also want code to do whatever its you want with the contents of the body sections…

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

Sidebar

Related Questions

I have a text file which looks like this: blah blah foo1 bar1 foo1
i have a text file which looks like this: random useless text <!-- this
I have a text file which looks like: name1 1 0 1 0 1
I have text file(test.data), which include(this is just example other files have more values....)
I have a .rc file which is used to include some text data in
I have a text file which was created using some Microsoft reporting tool. The
I have to create some sort of text file in which there are numbers
I have XML file which is looks like: <Report> <Total> <RecordValues> <Record> <FieldValue fieldName=index
I have text file(test.data), which include some values and class name, for example 4.5,3.5,U1
I have a ini file which contains some sample values like: [Section1] Value1 =

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.