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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:26:43+00:00 2026-05-22T17:26:43+00:00

When I have to parse text (e.g. config files or other rather simple/descriptive languages),

  • 0

When I have to parse text (e.g. config files or other rather simple/descriptive languages), there are several solutions that come to my mind:

  • using library functions, e.g. strtok(), sscanf()
  • a finite state machine which processes one char at a time, tokenizing and parsing
  • using the explode() function I once wrote out of pure boredom
  • using lex/yacc (read: flex/bison) to generate an appropriate parser

I don’t like the “library functions” approach. It feels clumsy and awkward. explode(), while it doesn’t take much new code, feels even more blown up. And flex/bison often seems like sheer overkill.

I usually implement a FSM, but at the same time I already feel sorry for the poor guy that may have to maintain my code at a later point.

Hence my question:

What is the best way to parse relatively simple text files?

Does it matter at all?

Is there a commonly agreed-upon approach?

  • 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-22T17:26:44+00:00Added an answer on May 22, 2026 at 5:26 pm

    I’m going to break the rules a bit and answer your questions out of order.

    • Is there a commonly agreed-upon approach?

    Absolutely not. IMHO the solution you choose should depend on (to name a few) your text, your timeframe, your experience, even your personality. If the text is simple enough to make flex and bison overkill, maybe C is itself overkill. Is it more important to be fast, or robust? Does it need to be maintained, or can it start quick and dirty? Are you a passionate C user, or can you be enticed away with the right language features? &c., &c.

    • Does it matter at all?

    Again, this is something only you can answer. If you’re working closely with a team of people, with particular skills and abilities, and the parser is important and needs to be maintained, it sure does matter! If you’re writing something “out of pure boredom,” I would suggest that it doesn’t matter at all, no. 🙂

    • What is the best way to parse relatively simple text files?

    Well, I don’t know that you’re going to like my answer. Maybe first read some of the other fine answers here.

    No, really, go ahead. I’ll wait.

    Ah, you’re back and relaxed. Let’s ease into things, shall we?

    Never write it in ‘C’ if you can do it in ‘awk’;
    Never do it in ‘awk’ if ‘sed’ can handle it;
    Never use ‘sed’ when ‘tr’ can do the job;
    Never invoke ‘tr’ when ‘cat’ is sufficient;
    Avoid using ‘cat’ whenever possible.
    — Taylor’s Laws of Programming

    If you’re writing it in C, but C feels like the wrong tool…it really might be the wrong tool. awk or perl will likely do what you’re trying to do without all the aggravation. You may even be able to do it with cut or something similar.

    On the other hand, if you’re writing it in C, you probably have a good reason to write it in C. Maybe your parser is a tiny part of a much larger system, which, for the sake of argument, is embedded, in a refrigerator, on the moon. Or maybe you loooove C. You may even hate awk and perl, heaven forfend.

    If you don’t hate awk and perl, you may want to embed them into your C program. This is doable, in principle–I’ve never done it myself. For awk, try libmawk. For perl, there are proably a few ways (TMTOWTDI). You can run perl separately using popen to start it, or you can actually embed a Perl interpreter into your C program–see man perlembed.

    Anyhow, as I’ve said, “the best way to parse” entirely depends on you and your team, the problem space, and your approach to the issue. What I can offer is my opinion.

    I’m going to assume that in your C-only solutions (library functions and FSM (considering your explode to essentially be a library function)) you’ve already done your best at isolating the relevant code, designing the code and files well, and so forth.

    Even so, I’m going to recommend lex and yacc.

    Library functions feel “clumsy and awkward.” A state machine seems unmaintainable. But you say that lex and yacc feel like overkill.

    I think you should approach your complaints differently. What you’re really doing is specifying a FSM. However, you’re also hiring someone to write and maintain it for you, thereby solving most of the maintainability problem. Overkill? Did I mention they’ll work for free?

    I suspect, but do not know, that the reason lex and yacc originally felt like overkill was that your config / simple files just felt too, well, simple. If I’m right (a big if), you may be able to do most of your work in the lexer. (It’s even conceivable that you can do all of your work in the lexer, but I know nothing about your input.) If your input is not only simple but widespread, you may be able to find a lexer/parser combination freely available for what you need.

    In short: if you can do this not in C, try something else. If you want C, use lex and yacc–they have a little overhead, but they’re a very good solution.

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

Sidebar

Related Questions

I'm using html agility pack to parse several text files that I load. I
I have a text template with placehoders that I parse in order to replace
Suppose I have a text that I can easily parse. It consists of text
This is how to parse a config file question. Basically i have a text
I want to parse text from a xml file.Consider that I have a some
I am trying to parse some text files into a database and there is
I have to parse a tab-delimited text file with Ruby to extract some data
I have a string: [\n['-','some text what\rcontains\nnewlines'],\n\n trying to parse: Regex.Split(@[\n['-','some text what contains
I have an XML file which I'd like to parse into a non-XML (text)
I have a binary file that I have to parse and I'm using Python.

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.