lex and yacc help desired

Mike J. Kelly mike at odgate.odesta.com
Fri Apr 26 06:49:08 AEST 1991


In article <1991Apr23.164744.25927 at mnemosyne.cs.du.edu> allen wade writes:
>I am fairly new to Lex and Yacc and I am tring to develop a
>language definition for a small report processer.
> My input lines will generally look like this:
>
>|MEDINA|ANTOINIO|01/14/62|M|(312)778-2540|60629|GARFIELD|LAREN|GP|DC101A|
>

You could use Yacc to parse this, but it's really overkill.  scanf(3)
would work as well, or at worst, just read the line into a string and
use the string package to separate out the components; look at strtok(3)
and strchr(3).  

If you really want to use Yacc, what I'd do is write a simple lex which
returns four types of tokens: TOK_DATE, TOK_PHONE, TOK_NUM and
TOK_STRING.  Then your yacc grammar is:

statement:	TOK_STRING '|' TOK_STRING '|' TOK_DATE '|' TOK_STRING
'|' ...
		{
			last_name = $1;		/* first field */
			first_name = $3;	/* second field */
			bday = $5;		/* third field */
			.
			.
			.
		}



More information about the Comp.unix.wizards mailing list