Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!rutgers!rochester!pt!ius1.cs.cmu.edu!edw From: edw@ius1.cs.cmu.edu (Eddie Wyatt) Newsgroups: comp.unix.questions Subject: Re: Any LEX gurus? Message-ID: <1031@ius1.cs.cmu.edu> Date: Sat, 8-Aug-87 17:46:20 EDT Article-I.D.: ius1.1031 Posted: Sat Aug 8 17:46:20 1987 Date-Received: Sun, 9-Aug-87 13:10:05 EDT References: <716@umnd-cs.D.UMN.EDU> Organization: Carnegie-Mellon University, CS/RI Lines: 92 Keywords: Ack! In article <716@umnd-cs.D.UMN.EDU>, jwabik@cs.D.UMN.EDU (Jeff Wabik) writes: > I am using (trying to use) LEX (& YACC, of course) to write a parser > for a language, and I'm running into problems defining some regular > expressions that need to be fed to LEX. In short, everything was > going fine and I had my parser working 'til I remembered that I > needed to completely ignore all preprocessor directives. Well.. > I thought that I could tell LEX to toss out all the preprocessor > directives, much the same way I was discarding comments: > > \{[^\n\}]*\}? { ; /* Toss out comments */ } > > The problem here is that (as you'll note) this expression cannot cope with > comments that contain newlines (which is fine, since neither can the > compiler 8^), and for the LIFE of me, I CANNOT write an expression > that WILL accept similar preprocessor directives that NEED to contain > newlines. In short, I need to nuke lines that have "??" as the first > non-white character of a line, and ending somewhere down the code > a bit with another matching "??". I've tried things similar to: > > What I've done in simuliar situations is to detect the first "??" then call a routine to scan to the next "??". Here's an example : comment [\/][\*] .... %% {comment} read_comment(); "\n" linecount++; {separator} ; ..... /* scans til a "*/" is found in the parse file */ static void read_comment() { register int ch; while (TRUE) { switch (getc(yyin)) { case '*' : do { /* end of comment */ if ((ch = getc(yyin)) == '/') return; /* should actually be an error - hit EOF before end of comment */ if (ch == EOF) return; /* found newline - increment line count for error reporting if needed */ if (ch == '\n') { linecount++; break; } } while (ch == '*'); break; case '\n' : /* found newline - increment line count for error reporting if needed */ linecount++; break; case EOF : /* should actually be an error - hit EOF before end of comment */ return; } } } > > =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-= > Jeff A. Wabik > University of Minnesota, Duluth 55812 > > ARPA: jwabik@cs-gw.d.umn.edu UUCP: {umn-cs!umnd-cs-gw!jwabik} > > Live long and program. > =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -- Eddie Wyatt e-mail: edw@ius1.cs.cmu.edu