LEX Question

T. William Wells bill at twwells.uucp
Thu Feb 16 05:30:01 AEST 1989


I have this at the top of one of my .l files:

%{
#undef input
#undef unput
# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr): \
    nextchar())==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
# define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--; \
    *yysptr++=yytchar;}
%}

[These ugly macros are made from the ones that are created by lex.]

And one of my source files contains this bit of code:

int
nextchar()
{
	if (*Bufptr == 0) {
		return ('\n');
	} else {
		return (*Bufptr++);
	}
}

Replace nextchar with whatever function you need for returning
characters.

It does work, I use the program containing it all the time.  I don't
recall any of the design decisions I made when doing this; what it is
used for is lexing out of a buffer. I do not know if the details will
be applicable to your system. (It works on a Sun and on my Microport
V/386 3.0e).

Consider it as a clue as to what to do.

---
Bill
{ uunet!proxftl | novavax } !twwells!bill



More information about the Comp.lang.c mailing list