1/13 I am writing a commandline parser for a class and I could use some
tips for algorithms to use. (The project is over and done so I am
not cheating, but I am dissatisfied with my end result.) I STFW and
didn't come up with too much I liked. I read the source for some
shells like tcsh and that is *WAY* too complicated and relies on
a lot of other code. I know that browsers and other apps have
parsers, too. Is there some simple algorithm they use? I am more
interested in algorithms than any implementation itself.
\_ Are you looking for something like getopt? Or is this more
"read this line and figure out what programs to run/
what outputs to pipe into other outputs"? And what language
are you using?
\_ C language. I am interested in how something like getopt is
designed in terms of its algorithm, particularly for multi-
character options. Like how it distinguishes -v from -vv
from -vvv for example? Does it read ahead? I am trying to
do this without using other functions like strtok() but
actually parsing a character at a time. I know I can go
read the getopt source, but this is not that complicated.
There must be some simple parsing algorithms these parsers
implement.
\_ You probably want a trie and a simple state machine.
\_ Why would you do it a character at a time? Do you mean
you don't know how to implement something like strtok?
\_ Yes, that's what I mean. What algorithm does it use?
Are there better choices? What if I wanted to
implement a better strtok or a better getopt?
\_ Well, that's not really the interesting part. If you
just look at strtok's header it wants you to specify
what constitutes a token delimiter. So -v versus -vv
is pretty obvious I think...
\_ I was once interested in parsing mathematical
expressions. However, rather than rolling my own, I just
made use of python's expression parser. This proved a good
way to parse expressions with very little coding on my part:
all I had to write was a little glue/wrapper.
\_ Greed -- for lack of a better word -- is good.
\_ why does gordon gecko get in more trouble than madoff?
\_ If you're interested in this from a theoretical perspective,
either take CS164 or try reading the Dragon Book. It's way
overkill for basic command line parsing, but you said you were
interested in the theory of parsing... that's what a lot of CS164
is about.
\_ What is the title of CS 164? Compilers?
\_ What is the title of CS 164?
\_ http://letmegooglethatforyou.com/?q=CS+164
\_ Back in Spring '92 it was "Programming Languages and
Compilers". I took it under Fateman. We only learned a lot
about compilers, but very little about programming languages.
Other schools have separate classes for the two. |