A simple parser

C

CBFalconer

Roland said:
. snip ...

Why are the functions static? That's only confusing for a newbie.

Because they are not used outside that module. This avoids name
space pollution. Routine.
 
B

Ben Bacarisse

jacob navia said:
Well, EBCDIC was a 7 bit code, used for punched cards. The eighth bit
was there to signal the card reader that a character was in that column.

Using only 7 bits, the codes are continuous.

Did not match my recollection so I tried:

echo -n ABCDEFGHIJKLMNOPQRSTUVWXYZ | iconv -f ascii -t ebcdic-us | od -t x1

and got:

0000000 c1 c2 c3 c4 c5 c6 c7 c8 c9 d1 d2 d3 d4 d5 d6 d7
0000020 d8 d9 e2 e3 e4 e5 e6 e7 e8 e9
 
R

Richard Heathfield

jacob navia said:

Using only 7 bits, the codes are continuous.

Rubbish.

EBCDIC encoding of a-z:

From a to i: 81 to 89
8A to 90 Other stuff
From j to r: 91 to 99
9A to A1 Other stuff
From s to z: A2 to A9

EBCDIC encoding of A-Z:

From A to I: C1 to C9
CA to D0 Other stuff
From J to R: D1 to D9
9A to A1 Other stuff
From s to z: E2 to E9
 
S

Spiro Trikaliotis

Hello,

jacob said:
[...]
I believe goto is not bad when used correctly. It is part of the
language anyway, and if used correctly it is perfectly OK.

Yes, if used correctly. Unfortunately, in your case, it can be avoided
*very* easily. Thus, in my view, it is not used correctly here. ;)

Regards,
Spiro.
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:
Richard Heathfield said:
jacob navia said:
I have written this small parser to print out the functions defined in a
C file. This is an example of parsing in C, that I want to add to my
tutorial. Comments (and bug reports) are welcome.

foo.c:12: parse error before `/'
foo.c:17: stray '\' in program [51 lines deleted]
make: *** [foo.o] Error 1

Every one of those errors is caused by two things: "//" comments and
mixed declarations and statements.

<shrug> I figured it had to be something like that. So - does anyone have a
conforming C99 compiler that we can use to test Mr Navia's code? No? Oh
well.

I don't, but I understand that some people do.
Er, so what? Despite the misleading switch-name, gcc is not a C99-conforming
compiler.

I never said it was. I do however, have access to a compiler that
implements enough of a subset of C99 to cpomile jacob's code. I
suspect you do too; if I recall correctly, the older version of gcc
that you use doesn't support the "-std=c99" option, but it does, in
some modes, support "//" comments and mixed declarations and code.

It also does a lousy job of diagnosing errors introduced by using C99
constructs. Use of "//" is such a common error that the compiler
should recognize it and issue a specific diagnostic. (I just tried
gcc 4.1.1; it does a little better, but it still treats the "//" as a
simple syntax error.)
 
K

Keith Thompson

jacob navia said:
[...]
Mmmm, it *could* be, but I have never found a machine where they aren't
contiguous...

That doesn't mean such machines don't exist. I've spent several
years working on such machines. Look up "EBCDIC" in Google.
[snip]
Well, EBCDIC was a 7 bit code, used for punched cards. The eighth bit
was there to signal the card reader that a character was in that column.

Using only 7 bits, the codes are continuous. When punched cards weren't
so much used (approx beginning of the 80es) IBM added foreign language
characters in those positions.

You didn't look up "EBCDIC" in Google, did you?
 
K

Keith Thompson

Why are the functions static? That's only confusing for a newbie.

Only to a newbie who doesn't know about static functions.

(IMHO, functions should be static by default, and exported to other
translation units only if specified. But of course it's way too late
to change that.)
Hmm, goto in a C tutorial? You could add an exercise for the reader:
'Enhance the clarity of this function by rewriting it with one return
statement and without using goto'.

*Some* gotos are ok; for example, it can be a decent way to jump to
error-handling code at the end of a function. (It makes up for C's
lack of multi-level break or a decent exception-handling mechanism.)

But this particular goto branches backward, which is potentially
dangerous. Fortunately, C has a structured equivalent of a backward
goto: a loop. Translating the code to use a loop is left as an
exercise for the author.
 
M

Mark McIntyre

On Sun, 15 Oct 2006 12:18:11 +0200, in comp.lang.c , jacob navia

(of EBCDIC)
This has a maybe anectodical importance, but its practical impact is ...

Hey, did they shut down all the IBM mainframes? Wow, thats gonna
reduce the carbon deficit, for sure.

:)

Hardly anecdotal.
Hate to say it Jacob, but you're back in the "All the words a Vax"
tunnel.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

Keith Thompson said:


<shrug> I figured it had to be something like that. So - does anyone have a
conforming C99 compiler that we can use to test Mr Navia's code? No? Oh
well.

Don't be childish. It ill becomes you.
Er, so what? Despite the misleading switch-name, gcc is not a C99-conforming
compiler.

Did you spot something in Jacob's code that was not C99-conforming?
Did you bother? Next time, engage your brain before posting, and
recall that if you plan to occupy the moral high ground, you need to
actually stand on it first.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
R

Richard Heathfield

Mark McIntyre said:
Don't be childish. It ill becomes you.

Hmm? Does that mean you /do/ have a C99 conforming compiler? Fabulous - let
us know how it goes.

Did you spot something in Jacob's code that was not C99-conforming?

No. Did you spot something in the code that *needed* a C99 feature, a
feature so vital that it justified making the code non-C90-conforming?

<snip>
 
J

jacob navia

Richard said:
Mark McIntyre said:




Hmm? Does that mean you /do/ have a C99 conforming compiler? Fabulous - let
us know how it goes.





No. Did you spot something in the code that *needed* a C99 feature, a
feature so vital that it justified making the code non-C90-conforming?

<snip>

You change your arguments heathfield. First you tell there isn't any c99
compiler, then you argue that c99 is not needed.

There is nothing in the code that needs to be c90 either. I could have
written it in K&R C, without prototypes syntax.

But I am not in a nostalgia trip, I believe C evolves and I like it like
that.

Can't you contribute anything substantial to the discussion?
Just empty polemic?

So far there is nothing in your arguments worth mentioning...

What about the algorithm? You see any flaws in there?

P.S. And please do not start with EBCDIC, I changed that to use
isalpha() ...
 
R

Richard Heathfield

jacob navia said:
You change your arguments heathfield.

Not so.
First you tell there isn't any c99 compiler,

There are very few.
then you argue that c99 is not needed.

If C99 compilers were commonplace, this would indeed be a pointless
argument. But since they are rarer than rocking-horse food, it makes sense
not to rely on C99 features if you don't need to.
There is nothing in the code that needs to be c90 either. I could have
written it in K&R C, without prototypes syntax.

Sure - but C90 compilers are all over the place, so there wouldn't be any
point in writing it for K&R C.
But I am not in a nostalgia trip,

No, you're on a trip to the future. Have a great time, and let us know when
you get back.
I believe C evolves and I like it like that.

Can't you contribute anything substantial to the discussion?

This /is/ substantial, unless your position is that you like your code to
fail to compile on most extant conforming implementations. If that is the
case, fine, be happy, win awards, whatever.

And my character set point was substantial, too. But you tried to handwave
that away, as usual (by inventing a pseudohistory for EBCDIC that bore no
relation to the facts). And you got caught, as usual.
Just empty polemic?

No, a genuine effort to help you to write code that more people can use. If
you persist in seeing portability as a negative, however, you will continue
to misunderstand me.
So far there is nothing in your arguments worth mentioning...

It is because you believe this that you fail.
What about the algorithm? You see any flaws in there?

When you submit a C90-conforming version that I can compile, I'll take a
look.
P.S. And please do not start with EBCDIC, I changed that to use
isalpha() ...

Well, there's a thing.
 
I

Ian Collins

Richard said:
Keith Thompson said:



That, in my view, is the author's job, not yours or mine. (And it shows that
the C99isms have been adopted gratuitously, presumably to make the code
less portable. Why anyone should wish to do this is beyond me.)
Oh no, the sky is falling, someone has posted code using some C99 features.
 
R

Richard Heathfield

Ian Collins said:
Oh no, the sky is falling,

Yes, it's in free fall, just like the rest of the planet. If this bothers
you, find a planet with less atmosphere. :)

someone has posted code using some C99 features.

....which is fine, as long as they don't expect anyone else to be able to
compile it on a conforming implementation.

Look, if there were some compelling reason for using C99 features, okay,
fair enough: "sorry for leaving Richard H (and most of the conforming
world) behind but these C99 features are just too useful to ignore, and if
that means a portability loss, so be it". But that does not appear to be
the case here.

This is not about C90 vs C99. This is about "works everywhere" vs "works
almost nowhere, unless you use a non-conforming compiler".
 
R

Richard

Ian Collins said:
Oh no, the sky is falling, someone has posted code using some C99 features.

The sooner people adopt the features which are generally supported the
better IMO ... far less porting to do further down the line :-;

Especially nice structural enhancements which improve code readability
and localization such as variable declarations at point of use.
 
R

Richard

Richard Heathfield said:
Ian Collins said:


Yes, it's in free fall, just like the rest of the planet. If this bothers
you, find a planet with less atmosphere. :)



...which is fine, as long as they don't expect anyone else to be able to
compile it on a conforming implementation.

Look, if there were some compelling reason for using C99 features, okay,
fair enough: "sorry for leaving Richard H (and most of the conforming
world) behind but these C99 features are just too useful to ignore, and if
that means a portability loss, so be it". But that does not appear to be
the case here.

This is not about C90 vs C99. This is about "works everywhere" vs "works
almost nowhere, unless you use a non-conforming compiler".

But since gcc supports a subset, and a useful subset, its hardly "almost
nowhere" is it?

And this is c.l.c not c.s.c
 
I

Ian Collins

Richard said:
Ian Collins said:


Yes, it's in free fall, just like the rest of the planet. If this bothers
you, find a planet with less atmosphere. :)
With yes, relative to, only when Jacob posts C99 :)

I thought your original repose was a little melodramatic, considering
C99 is (a) standard C, the topic of this group.
....which is fine, as long as they don't expect anyone else to be able to
compile it on a conforming implementation.

Look, if there were some compelling reason for using C99 features, okay,
fair enough: "sorry for leaving Richard H (and most of the conforming
world) behind but these C99 features are just too useful to ignore, and if
that means a portability loss, so be it". But that does not appear to be
the case here.

This is not about C90 vs C99. This is about "works everywhere" vs "works
almost nowhere, unless you use a non-conforming compiler".
Which will never happen with C99 unless people (want to) use it.

Admittedly I'm biased, my platform's (POSIX) APIs use C99 features and
my stuff tends to be platform specific.
 
R

Richard Heathfield

Richard said:
But since gcc supports a subset, and a useful subset, its hardly "almost
nowhere" is it?

When I invoke my gcc implementation in conforming mode, it (correctly)
diagnoses single-line comments, mixed code/decls, etc. If you are asking me
to turn off conforming mode, the answer is "No".
 
R

Richard

Richard Heathfield said:
Richard said:


When I invoke my gcc implementation in conforming mode, it (correctly)
diagnoses single-line comments, mixed code/decls, etc. If you are asking me
to turn off conforming mode, the answer is "No".

Which conforming mode?

I have the following command line options: Pretty strict it is too.

CFLAGS=-std=c99 -pedantic-errors -Wall -pthread -g $(DEBUGFLAGS)

Nothing particularly evil. OK, we know our target OS.

These features don't suddenly make it weaker C code - this is
comp.lang.c and C99 is the C language too. Like it, or more probably in
your case, not.
 
R

Richard Heathfield

Ian Collins said:
I thought your original repose was a little melodramatic, considering
C99 is (a) standard C, the topic of this group.

Well, I suppose it was, but it wasn't intended to be. (Can melodrama be
accidental? I don't know.) Anyway, the point is this: that, quite often, my
first reaction (to an article asking for code crits) is to run the code
through a compiler - and I might not even read it first, especially if it's
long. The resulting compiler diagnostics give me a place to start the crit.

And that's what I did this time. If the OP had been someone with a track
record for being reasonable and rational and logical, I might have looked
more closely at the source after seeing how many diagnostics it generated,
and realised at that point that it used C99 features. But since it was only
our resident "all the world's a Win32 box running lcc-win32"-er, I was not
highly motivated to investigate the source of the errors.
Which will never happen with C99 unless people (want to) use it.

I'll be happy to use it when it arrives (everywhere or at least nearly
everywhere), but not before.
Admittedly I'm biased, my platform's (POSIX) APIs use C99 features and
my stuff tends to be platform specific.

Which explains your point of view nicely. And it's a perfectly valid one.

My stuff tends to be platform-independent (as far as it can be). Which
explains mine. And it, too, is perfectly valid.
 

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

Similar Threads

How to remove // comments 100
// comments 35
Text processing 29
Serial port 5
Command Line Arguments 0
Deleting first N lines from a text file 26
Working with files 1
Taking a stab at getline 40

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top