09-21-05 07:50 AM
I'm looking through lex and yacc documentaion, and it offers two
different functions input and unput to change the source of where lex
reads from.
They are:
int input(void)
{
char c;
if(targv >= arglim) return 0;
if((c = targv[0][offset++]) != '\0') return c;
targv++;
offset = 0;
return ' ';
}
and
void output(int ch)
{
if(ch == 0) return ;
if(offset) {
offset--;
return ;
}
targv--;
offset = strlen(*targv);
}
but using flex we have to use other means to change the
source from where it reads from.
This is done through modifying the macro YY_INPUT
by first undefing it then defining our own version.
what I am reading provides a modifiable version suitable for solving
the problem they present in the chapter.
It is as follows:
#undef YY_INPUT
#define YY_INPUT(buf, result, max) (result = myinput(buf, max))
int myinput(char *buf, int max)
{
int len, copylen;
if(targv >= arglim)
return 0;
len = strlen(*targv) - offset;
if(len >= max)
copylen = max - 1;
else
copylen = len;
if(len > 0) memcpy(buf, targv[0] + offset, copylen);
if(targv[0][offset + copylen] == '\0') {
buf[copylen] = ' ';
copylen++;
offset = 0;
targv++;
}
return copylen;
}
It doesn't include an unput function because it
says that flex handles unput itself.
But my question is how? If I change the source of input
for flex and just provide it with a chunk of data(in this case
a continuous sequence of multibyte characters), then how does it manage
to handle the unput operation if need be?
--
j0mbolar
[ Post a follow-up to this message ]
|