Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!sunybcs!boulder!hao!husc6!mit-eddie!uw-beaver!tektronix!orca!tekecs!frip!andrew From: andrew@frip.UUCP Newsgroups: comp.unix.wizards,comp.lang.c,comp.unix.questions Subject: Re: Lex Macros Message-ID: <9399@tekecs.TEK.COM> Date: Fri, 20-Nov-87 12:00:30 EST Article-I.D.: tekecs.9399 Posted: Fri Nov 20 12:00:30 1987 Date-Received: Sun, 22-Nov-87 13:23:30 EST References: <48@wvucswv.UUCP> Sender: nobody@tekecs.TEK.COM Organization: Tektronix, Wilsonville, Oregon Lines: 36 Xref: utgpu comp.unix.wizards:5136 comp.lang.c:5216 comp.unix.questions:4460 [] "I would like some help in interpreting some of the macros used in Lex. Below is the relevant portion of the C code produced by Lex. The macros are quite "slick", but also somewhat difficult to understand. Could someone please describe - in detail - how the macros "input()" and "unput()" operate?????" Yysbuf is the unget buffer. It's organized as a stack, growing and shrinking at the right (high address). To unget a character, append it; to get a character, first look to see if yysbuf is nonempty, and if so take (and remove) the last character. Yysptr points to the position beyond the last character in the stack. When yysptr==yysbuf, then stack is empty. So: # define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==10? (yylineno++,yytchar):yytchar)==EOF?0:yytchar) yysptr>yysbuf means the unget stack is nonempty, so set yytchar to the last character in that stack and remove it by decrementing yysptr. Otherwise the unget stack is empty, so invoke getc to read the next character from the input file, and, if that character is '\n' (decimal 10 in ASCII), increment the line number in yylineno; if it's EOF, return 0, else return the character. # define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;} Just append the character to the unget stack, and decrement the line number if ungetting a newline. -=- Andrew Klossner (decvax!tektronix!tekecs!andrew) [UUCP] (andrew%tekecs.tek.com@relay.cs.net) [ARPA]