C parser document

C

Chris Dollin

Jonas said:
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.
 
A

ajm

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.
 
J

Jonas Geiregat

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!
 
D

Default User

Jonas said:
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
 
K

Keith Thompson

Default User said:
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.
 
K

Keith Thompson

Default User said:
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.
 
E

Eric Sosman

Default said:
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.
 
J

John Bode

Jonas said:
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.
 
D

Default User

Eric said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top