Using Lex (and Yacc) on a string.
Joseph A. Leggio
jal at valha1.ATT.COM
Sun Aug 12 12:24:55 AEST 1990
>From article <1990Aug10.012927.5558 at basho.uucp>, by john at basho.uucp (John Lacey):
> Normally, of course, one wants a scanner (and a parser) to work from
> a file, perhaps stdin. Sigh. Well, I want one that works from a string.
>
> Has anyone done this, or see a way to do it, or know a way to do it, or ....
>
> --
> John Lacey,
I have used these "input" and "unput" routines in
many programs where I wanted complete control of the
input stream. The example here uses fgets to fill
a character array from stdin, but you could fill it
from any source you wish. You only need point pointer "p"
to the start of the array each time you read a new line.
Only restriction: unput cannot back up past the start of a line.
(I have not found this to be a problem as I do not usually try
to match patterns which span multiple lines.)
I use System V Release 3 AT&T lex, "flex" might work the same, look
for the #defines for "input" and "unput" in your code.
==================================================
%%
Lex reg-expr's go here
%%
#define BUFFER_SIZE 1024
char *p;
char buf[BUFFER_SIZE];
main(){
p = buf; /* point "p" to start of buf for first line */
while( fgets(buf, sizeof(buf), stdin) != NULL ) { /* read line */
yylex(); /* parse line */
p = buf; /* point "p" back to start of buf for next line */
}
exit(0);
}
#ifdef input
#undef input
#endif
#ifdef unput
#undef unput
#endif
/* replacement "input" routine for lex, uses char array "buf" */
char input()
{
if ( p < buf + ( BUFFER_SIZE - 1 ) )
return(*p++);
else
return((char)0);
}
/* replacement "unput" routine for lex, uses char array "buf" */
unput(c)
char c;
{
if ( p > buf )
*(--p) = c;
}
=============================================================
Joe Leggio WB2HOL
AT&T Customer Software Services
Valhalla, NY
att!valha1!jal
More information about the Comp.unix.questions
mailing list