C parser document

Discussion in 'C Programming' started by Chris Dollin, Sep 2, 2005.

  1. Chris Dollin

    Chris Dollin Guest

    Jonas Geiregat wrote:

    > I'm trying to parse a configuration file,
    > this is the first time I'm trying something like this, and it seems hard.
    > C's I/O functions are a mess and hard to use , that is if you want to
    > write code in a good way.


    I've written parsers for one style of config file in C; the I/O
    functions were pretty much a non-issue.

    Be more specific about what you're trying to parse and the problems
    you've had.

    --
    Chris "electric hedgehog" Dollin
    Almost all predictions about life in 2015 are wrong in some fundamental
    respect.
     
    Chris Dollin, Sep 2, 2005
    #1
    1. Advertising

  2. Chris Dollin

    ajm Guest

    I hardly think the I/O functions "are a mess" (not at all sure though
    what you mean by that ;) but guess you still have a bit of reading
    ahead of you...ho hum so give the following a try:

    http://www.gnu.org/software/libc/manual/html_node/

    for a start (I/O Overview and I/O Streams are what you might be looking
    for and the rest probably doesn't hurt), then of course there are the
    manpages etc. etc.

    if you ever get around to a specific question you might want to post
    that too ;)

    hth,
    ajm.
     
    ajm, Sep 2, 2005
    #2
    1. Advertising

  3. I'm trying to parse a configuration file,
    this is the first time I'm trying something like this, and it seems hard.
    C's I/O functions are a mess and hard to use , that is if you want to
    write code in a good way.
    I've tried google many times looking for some good document article that
    talks about C I/O and parsing files, most of the docs that I came across
    just talk about basic I/O things I can also learn from reading my man pages.

    Any kind of advise is welcome!
     
    Jonas Geiregat, Sep 2, 2005
    #3
  4. Chris Dollin

    Default User Guest

    Jonas Geiregat wrote:

    > I'm trying to parse a configuration file,
    > this is the first time I'm trying something like this, and it seems
    > hard. C's I/O functions are a mess and hard to use , that is if you
    > want to write code in a good way. I've tried google many times
    > looking for some good document article that talks about C I/O and
    > parsing files, most of the docs that I came across just talk about
    > basic I/O things I can also learn from reading my man pages.
    >
    > Any kind of advise is welcome!


    Here's my advice. This is probably one of the worst posts I've seen to
    this group. You don't describe what you are trying to do. You don't say
    what problems you have. You gratuitously insult the programming
    language you are trying to get help in.

    Please read the following. It is not from our FAQ list, rather from
    comp.lang.c++, but I cribbed it anyway:

    Post compileable code: avoid ellipses, such as void f() { ... }

    Post complete code: put in all necessary #includes and declarations of
    needed types and functions

    Post minimal code: just enough to demonstrate the problem

    Post one compilation unit: if possible, combine Foo.h into Foo.c

    Post the exact messages you received; differentiate between compiler,
    linker, and runtime messages

    Make sure main() has a return type of int, not void!



    Brian
     
    Default User, Sep 2, 2005
    #4
  5. "Default User" <> writes:
    > Jonas Geiregat wrote:
    >
    >> I'm trying to parse a configuration file,
    >> this is the first time I'm trying something like this, and it seems
    >> hard. C's I/O functions are a mess and hard to use , that is if you
    >> want to write code in a good way. I've tried google many times
    >> looking for some good document article that talks about C I/O and
    >> parsing files, most of the docs that I came across just talk about
    >> basic I/O things I can also learn from reading my man pages.
    >>
    >> Any kind of advise is welcome!

    >
    > Here's my advice. This is probably one of the worst posts I've seen to
    > this group. You don't describe what you are trying to do. You don't say
    > what problems you have. You gratuitously insult the programming
    > language you are trying to get help in.


    I've seen *much* worse posts than that.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 2, 2005
    #5
  6. Chris Dollin

    Default User Guest

    Keith Thompson wrote:

    > "Default User" <> writes:


    > > Here's my advice. This is probably one of the worst posts I've seen
    > > to this group. You don't describe what you are trying to do. You
    > > don't say what problems you have. You gratuitously insult the
    > > programming language you are trying to get help in.

    >
    > I've seen much worse posts than that.


    Worseness is in the eye of the beholder.



    Brian
     
    Default User, Sep 2, 2005
    #6
  7. "Default User" <> writes:
    > Keith Thompson wrote:
    >> "Default User" <> writes:
    >> > Here's my advice. This is probably one of the worst posts I've seen
    >> > to this group. You don't describe what you are trying to do. You
    >> > don't say what problems you have. You gratuitously insult the
    >> > programming language you are trying to get help in.

    >>
    >> I've seen much worse posts than that.

    >
    > Worseness is in the eye of the beholder.


    Sure, but things like that tend to reinforce comp.lang.c's
    (undeserved, IMHO) reputation as a bunch of overly picky topicality
    police.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 2, 2005
    #7
  8. Chris Dollin

    Eric Sosman Guest

    [OT] Re: C parser document

    Default User wrote:
    > Jonas Geiregat wrote:
    >
    >
    >>I'm trying to parse a configuration file,
    >>this is the first time I'm trying something like this, and it seems
    >>hard. C's I/O functions are a mess and hard to use , that is if you
    >>want to write code in a good way. I've tried google many times
    >>looking for some good document article that talks about C I/O and
    >>parsing files, most of the docs that I came across just talk about
    >>basic I/O things I can also learn from reading my man pages.
    >>
    >>Any kind of advise is welcome!

    >
    >
    > Here's my advice. This is probably one of the worst posts I've seen to
    > this group. [...]


    For your information, it is considered polite to lurk
    on a newsgroup for a few weeks before starting to post. If
    you have seen nothing worse than the O.P., you cannot have
    been here more than a few seconds. Patience, Grasshopper.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Sep 3, 2005
    #8
  9. Chris Dollin

    John Bode Guest

    Jonas Geiregat wrote:
    > I'm trying to parse a configuration file,
    > this is the first time I'm trying something like this, and it seems hard.
    > C's I/O functions are a mess and hard to use , that is if you want to
    > write code in a good way.
    > I've tried google many times looking for some good document article that
    > talks about C I/O and parsing files, most of the docs that I came across
    > just talk about basic I/O things I can also learn from reading my man pages.
    >
    > Any kind of advise is welcome!


    It would help a bit if you could tell us what kind of problems you're
    running into; what's the structure of the file you're trying to parse,
    for example?

    Advice on C I/O routines:

    1. Avoid gets() like the plague. Better yet, just pretend gets()
    never existed. Using it *will* introduce a point of failure in your
    code.

    2. Unless your input is relatively simple in structure and *always*
    well-formed, don't use scanf() to read and assign items directly from
    the stream; scanf() doesn't give you the ability to easily recognize or
    recover from malformed input.

    3. Use fgets() to read in lines of input, and then process the
    resulting buffer with strtok(), strstr(), strchr(), sscanf(), or some
    combination of the above. Alternately, use fgetc() to read in a single
    character at a time and process via state tables, but unless you're
    writing a compiler front-end or something similar, that's probably
    overkill. And if you *are* writing a compiler front-end, use lex and
    yacc or something similar instead of rolling your own.

    4. Break your parsing code into several layers of abstraction, with
    the C I/O routines wrapped in more abstract operations like
    GetNextLine() or GetNextToken(), which are in turn called by
    progressively more abstract operations like GetLogfileName() or
    GetStartupParams(). This makes code a bit easier to write; you're only
    focusing on as much complexity as you need to at any particular level.
    It also allows you to make changes at the native input level without
    hacking the application logic.

    5. Before starting *any* of this, make sure there isn't a tool already
    out there to do what you're doing. For example, if your input is in
    XML format, there are already a number of parser libraries available
    for parsing XML documents. And there are tools like the previously
    mentioned lex and yacc (flex and bison in the GNU world) that allow you
    to build robust and sophisticated parsers.

    6. Of course, make sure you're using the right tool for the job in the
    first place. The old joke goes something like "when confronted with a
    problem, a programmer says, 'I know, I'll use XML.' The programmer now
    has two problems. " XML's wonderful, but do you *really* need to link
    in a heavyweight DOM parser to handle 5 lines of text? Do you really
    need to go through the hassle of generating code from lex and yacc if
    all you're dealing with is a comma-delimited list of name-value pairs?
    This is why it's important for you to give us some idea of what you're
    trying to do.
     
    John Bode, Sep 3, 2005
    #9
  10. Chris Dollin

    Default User Guest

    Eric Sosman wrote:
    > Default User wrote:


    > > Here's my advice. This is probably one of the worst posts I've seen to
    > > this group. [...]

    >
    > For your information, it is considered polite to lurk
    > on a newsgroup for a few weeks before starting to post. If
    > you have seen nothing worse than the O.P., you cannot have
    > been here more than a few seconds.


    Ha ha.

    Believe me, I'm sincere. I rate this worse than troll posts or even
    ERT's bad advice posts, because this person's post so utterly fails to
    accomplish anything useful for him.



    Brian
     
    Default User, Sep 4, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bernd Oninger
    Replies:
    0
    Views:
    793
    Bernd Oninger
    Jun 9, 2004
  2. ZOCOR

    XML Parser VS HTML Parser

    ZOCOR, Oct 3, 2004, in forum: Java
    Replies:
    11
    Views:
    848
    Paul King
    Oct 5, 2004
  3. Bernd Oninger
    Replies:
    0
    Views:
    843
    Bernd Oninger
    Jun 9, 2004
  4. Joel Hedlund
    Replies:
    2
    Views:
    558
    Joel Hedlund
    Nov 11, 2006
  5. Joel Hedlund
    Replies:
    0
    Views:
    328
    Joel Hedlund
    Nov 11, 2006
Loading...

Share This Page