Lexing nested comments

dvdeug@x8b4e53cd.iecc.com (David Starner)
11 Oct 1999 02:29:57 -0400

          From comp.compilers

Related articles
Lexing nested comments dvdeug@x8b4e53cd.iecc.com (1999-10-11)
| List of all articles for this month |

From: dvdeug@x8b4e53cd.iecc.com (David Starner)
Newsgroups: comp.compilers
Date: 11 Oct 1999 02:29:57 -0400
Organization: Oklahoma State University
Keywords: lex, question, syntax, comment

I'm working on a lexer for a language that has nested comments ("(*"&"*)")
and nested pragmas ("<*" & "*>"). I understand there is no way to get
flex to properly match nested items? Is there any way to get the contents
of the pragma to the parser intact so I can issue decent warnings?


On the other hand, I've noticed none of gcc's frontends use a flex
based lexer. Do most serious projects end up using a hand written
lexer?


David Starner - dstarner98@aasaa.ofe.org
web, ftp: x8b4e53cd.dhcp.okstate.edu
[It's easy to get lex to match nested comments, you just can't do it solely
with regular expressions. Here's a sketch:


%x COMMENT
  int nesting = 0;
%%
...
"(*" { BEGIN COMMENT; nesting = 0; /* initialized saved string */ }
<COMMENT>"(*" { /* add to saved string for parser */ nesting++; }
<COMMENT>"*)" { if(--nesting <=0 ) {
BEGIN INITIAL;
/* hand string to parser */
}
/* add to saved string for parser */
}


<COMMENT>.|\n { /* add to saved string for parser */ }


-John]


Post a followup to this message

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