08-19-04 10:58 PM
Billy N. Patton <b-patton@ti.com> wrote:
> I have an old flex/bison version of a parser and I have a version where
> i read c char at a time and enter a LARGE switch (in c) that has nested
> switch statements.
> I wrote the bison several years ago and don't remember why I quit using it.[/vbcol
]
[vbcol=seagreen]
> Your opinion of pro's and cons for flex/bison would be appreciated.
> I know the switch I can step into 2 years from now and understand.
> The flex/bison is like learning to speak a new language each time.
> What are the benefits of lex/yacc?
Because, at least in complex situations, it's a lot easier to write.
When you just have e.g. single chars where each triggers a different
action it's probably easier to use just a switch or something else,
but of you tokenize and parse something more complex like a language
then lex/yacc are probably going to save you a lot of time and
headache. If you have e.g. a syntax were after TOKEN_A either
TOKEN_B, TOKEN_C or TOKEN_D, followed by a comma and then TOKEN_E
is allowed and in each case you would have different actions to
execute, keeping that straight (especially if the requirements
change) with lots and lots of state variables would probably
become a nightmare to maintain in a rather short time when you
try to write that in C only. yacc/bison will do all the work for
you of remembering previously read tokens (and the associated
values), which state you're currently in etc. while you just have
to specify the syntax and the associated actions. And I would guess
that it's also a lot easier to write (and understand later) when
you have e.g.
INT [0-9]+
EXPO [Ee][+-]?{INT}
FLOAT ((({INT}"."[0-9]*)|([0-9]*"."{INT})){EXPO
}?)|({INT}{EXPO})
{INT} do_something_with_an_int( yytext );
{FLOAT} do_something_with_a_float( yytext );
than having to determine if something is really an number and if
it's a float, where it ends etc. manually with some C functions
(ok, in that case you could use strtod() and strtol(), but you
still would have to distinguish between an int and a float).
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
[ Post a follow-up to this message ]
|