|Lex/Yacc: Reading fixed field lengths email@example.com (Ted Christiansen) (1999-01-03)|
|Re: Lex/Yacc: Reading fixed field lengths firstname.lastname@example.org (Keith Thompson) (1999-01-11)|
|From:||Keith Thompson <email@example.com>|
|Date:||11 Jan 1999 14:39:50 -0500|
|Organization:||CTS Network Services|
Ted Christiansen <firstname.lastname@example.org> writes:
> How can I setup lex/yacc to parse lines with fixed field lengths? In
> the example below, the fields are 8 characters wide. Sometimes there is
> white space between, sometimes not - this would cause a typically
> configured scanner to fail. Any help would be appreciated.
> GRID 1 0 124.-346.999 2.99999 0
> GRID 3 0 49.9999-392.999-4.99999 0
Others have pointed out that lex may not be the best tool for this
job. It may still make sense to use it if the syntax of the fields
themselves is sufficiently complex.
A possibility I haven't seen mentioned is to preprocess the fixed-width
input into a more lex-friendly form. Thus the above fixed field input
could very easily be processed into something like the following:
GRID 1 0 124. -346.999 2.99999 0
GRID 3 0 49.9999 -392.999 -4.99999 0
which could then be fed to lex. If necessary, you could define a
token that would stand in for a blank or missing field.
It's slightly more complicated if the fixed-width fields can contain
significant embedded blanks, but you could define a syntax for that as
well (perhaps replacing them with some character not otherwise used).
Keith Thompson (The_Other_Keith) email@example.com <http://www.ghoti.net/~kst> <*>
Qualcomm, San Diego, California, USA <http://www.qualcomm.com>
[Preprocessing like that is not a bad idea. -John]
Return to the
Search the comp.compilers archives again.