Lex/Yacc: Reading fixed field lengths

Ted Christiansen <ted@chriseng.com>
3 Jan 1999 13:21:14 -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 [LONG!] belinfan@cs.utwente.nl (1999-01-06)
Re: Lex/Yacc: Reading fixed field lengths kst@cts.com (Keith Thompson) (1999-01-11)
| List of all articles for this month |
From: Ted Christiansen <ted@chriseng.com>
Newsgroups: comp.compilers
Date: 3 Jan 1999 13:21:14 -0500
Organization: Compilers Central
Keywords: lex, question, comment

Hello,


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




Ted
--
========================
Ted Christiansen, P.E.
Christiansen Engineering, LLC
http://www.chriseng.com
Mechanical Engineering Design and Analysis Services
[Using lex to scan fixed length fields is not unlike hammering
something with a pair of vise-grips. You can do it, but it doesn't
work very well. I suppose you could do something like this, taking
advantage of the fact that atof() skips over white space:


"^GRID " return GRID;
/* this pattern matches 8 chars */
[-0-9. ]{8} {
if(!strcmp(yytext, " ")) warn("blank field");
yylval.fltval = atof(yytext); return NUMBER;
}
. yyerror("Invalid character %s\n, yytext);
\n return EOL;


-John]


Post a followup to this message

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