For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2005 > flex









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author flex
j0mbolar

2005-09-21, 3:56 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

Thomas Maier-Komor

2005-09-27, 7:57 am

j0mbolar schrieb:
[snip]
>
> 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
>


just take a look at the output of flex. It uses an intermediate buffer,
where it puts the data it gets from input. So it can handle unput,
by modifying the access offset to this buffer...

Tom
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com