[techtalk] switch function in C (or how to read commandline args?)

James Sutherland jas88 at cam.ac.uk
Mon Jul 9 15:59:19 EST 2001


On Wed, 04 Jul 2001 18:05:21 -0500, you wrote:

>penguina at cosyn.co.nz said:
>> No.
>> What this will accomplish is setting the constants
>> to be strings, which will then require slow strcmp
>> processing as above.  When you tokenize something,
>> you come up with a unique *numerical* constant to
>> represent a particular string.  You can also have
>> several different strings parse to the same token.
>> So I could say
>> #define HOLA   23
>> #define HELLO  23
>> #define AMIGO  24
>> #define FRIEND 24
>
>Exactly, that's what I meant.  I should have been more clear about that.  
>Also, "tokenizing" might be the wrong term, but I don't know of a better one 
>offhand.

That's what I've always heard it called. BBC Basic used to store BASIC
programs in a "tokenised" form, which made "interpreted" execution
very rapid indeed - since each command was represented by a single
byte, in some ways it was more like bytecode than truly interpreted
code...

>The basic idea is you convert string constants to numeric constants early, and 
>from then on, you get to use nice fast integer operations on them, rather than 
>slow string ops.

Yes. If you're writing, say, a language interpreter, or processing
configuration files, tokenising is the way to go - I mentioned I was
doing something similar, where this is not appropriate: any situation
where you only parse each statement/command once (FTP/HTTP/NNTP
server, interactive menuing system, whatever). Here, I think the
character-by-character tree approach is best?


James.




More information about the Techtalk mailing list