Xref: utzoo comp.lang.c:31041 comp.unix.questions:24572 Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!mcgill-vision!snorkelwacker!usc!rutgers!att!occrsh!akgua!atl2!valha1!jal From: jal@valha1.ATT.COM (Joseph A. Leggio) Newsgroups: comp.lang.c,comp.unix.questions Subject: Re: Using Lex (and Yacc) on a string. Message-ID: <570@valha1.ATT.COM> Date: 12 Aug 90 02:24:55 GMT References: <1990Aug10.012927.5558@basho.uucp> Followup-To: comp.lang.c Organization: AT&T Customer Software Services Center, Valhalla, NY Lines: 70 From article <1990Aug10.012927.5558@basho.uucp>, by john@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