Re: Lex/Yacc: Reading fixed field lengths

Keith Thompson <kst@cts.com>
11 Jan 1999 14:39:50 -0500

          From comp.compilers

Related articles
Lex/Yacc: Reading fixed field lengths ted@chriseng.com (Ted Christiansen) (1999-01-03)
Re: Lex/Yacc: Reading fixed field lengths kst@cts.com (Keith Thompson) (1999-01-11)
| List of all articles for this month |

From: Keith Thompson <kst@cts.com>
Newsgroups: comp.compilers
Date: 11 Jan 1999 14:39:50 -0500
Organization: CTS Network Services
References: 99-01-015
Keywords: lex, comment

Ted Christiansen <ted@chriseng.com> 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.
>
> -------1-------2-------3-------4-------5-------6-------7
> 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) kst@cts.com <http://www.ghoti.net/~kst> <*>
Qualcomm, San Diego, California, USA <http://www.qualcomm.com>
[Preprocessing like that is not a bad idea. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.