(f)lex Regular Expression for C++ comment.

pwk@news.eb.ele.tue.nl (Pieter Kuppens)
1 Mar 1996 14:11:30 -0500

          From comp.compilers

Related articles
(f)lex Regular Expression for C++ comment. pwk@news.eb.ele.tue.nl (1996-03-01)
Re: (f)lex Regular Expression for C++ comment. vern@daffy.ee.lbl.gov (1996-03-01)
| List of all articles for this month |

From: pwk@news.eb.ele.tue.nl (Pieter Kuppens)
Newsgroups: comp.compilers
Date: 1 Mar 1996 14:11:30 -0500
Organization: Eindhoven University of Technology, Digital Information Systems Group
Keywords: C++, lex

Just in case anyone needs them:
the C++ style comment: /* this comment */ // and this comment
in flex format (should improve scanner performance).
I never encountered it before, so I'll share it.


--- 8< ---- snip ---- 8< ---
%{ /* (f)lex specification */
%}
A [/]
B [*]
C [^/*]


%%


{A}({A}.*|{B}({A}|{C}|{B}+{C})*{B}+{A}) /* skip comment */
/*
  * {A}{B}({A}|{C}|{B}+{C})*{B}+{A} /* only C-style */
  */


--- 8< ---- snip ---- 8< ---


Regards,
Pieter Kuppens --- pwk@eb.ele.tue.nl
Eindhoven University of Technology --- EH 11.24 --- (+31)40-247 3394
E.S.V.V. Pusphaira on WWW; http://www2.stack.urc.tue.nl/Pusphaira/
[I strongly discourage attempts to handle comments this way, because they
fail as soon as a comment is longer than the token buffer. It's easy to do
this with exclusive start states, e.g.:


%x COMMENT
%%
"/*" begin COMMENT ;
<COMMENT>"*/" begin NORMAL;
<COMMENT>.|\n /* ignore */;


Eating // comments with "//".* is probably OK unless you're using AT&T lex with
its tiny fixed token buffer. -John]
-John]


--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.