Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site hsi.UUCP Path: utzoo!watmath!clyde!akgua!sdcsvax!sdcrdcf!hplabs!hao!kpno!hsi!hogue From: hogue@hsi.UUCP Newsgroups: net.unix-wizards Subject: Re: Comment recognition in Lex, again Message-ID: <124@hsi.UUCP> Date: Fri, 4-May-84 09:24:06 EDT Article-I.D.: hsi.124 Posted: Fri May 4 09:24:06 1984 Date-Received: Wed, 9-May-84 03:20:39 EDT References: <245@uwvax.ARPA> Organization: Health Systems Intl. New Haven,CT Lines: 43 Ok I will attempt to explain the lex script. First a little history. The script was derived while attempting to write a C complier for a class at the University of Arizona. Dr. P. J. Downey and I derived the regular expression from the finite automata and the knowledge of lexs' attempt to match the longest possible match. > I have received several replies to my request for a lex expression > to recognize /* ... */ comments. The only one that works (sent in > by Jim Hogue) is > "/*"([^*]*"*"*"*"[^/*])*[^*]*"*"*"*/" 1. "/*" matches the initial /* of a comment. 2. ([^*]*"*"*"*"[^/*])* matches the stuff in the comment including any *'s or /'s but does not allow the * just prior to the / to be matched. As such it does not match */ 3. [^*]*"*"*"*/" matches more stuff and this time forces the match of the final */ Problems: First the /**/ /***/ ... or /*foo**/ style comments. These are taken care of by 3. Next the /**foo*/ and /*foo*baz*/ style comments. These are taken care of by 2. The final trick is that the first */ is matched and thus comments of the form /*foo*/a = b;/*baz*/ are matched in the same style as PCC. ie. /*foo*/ and /*baz*/ are matched and a = b; is not. Now for the real way to do it. Simply recognize the /* and then call a three line c program that "eats" up the comment! (The c preprocessor does the comment removal for the c complier). The real reason this looks so complicated is because it was derived not thought out! -- Jim Hogue {kpno, ihnp4}!hsi!hogue Health Systems International New Haven, CT 06511