help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "Eating" comments: not with Flex but with Bison


From: Kelly Leahy
Subject: Re: "Eating" comments: not with Flex but with Bison
Date: Tue, 14 Jun 2005 07:27:44 -0700 (PDT)

Frans,

If I understand you correctly, you want to know
whether comments were in the source, but you don't
care about the exact contents of the comments, right?

If this is the case, how much do you need to know
about the comments?  Do you need, for instance, the
line number on which they appeared, and the location,
or just that they were there?

You said that it is an error to have comments in some
of your source files.  How do you want the error
message to look?  Should it just say "comments not
allowed in xxx source", or should it output one error
message for each comment, along with location info as
mentioned above?

If all you need to do is know whether comments existed
(anywhere) in the source, just have your lexer set a
flag (for every comment it sees) and never reset this
flag for a given source file.  Then, when you are
finished parsing, raise an error about the comments. 
If parsing is too intensive to do this, then add a
step in the yyparse to reject the parse at first sign
of this flag being set.

If you need to know where the comments occurred, you
can do this one of several ways.  You could, for
instance, keep some sort of list of the comments and
their locations in the lexer (accessible to the
parser), or you could add comment information to your
token type and wrap your yylex function with another
function that is used by the parser instead of yylex. 
This function can call yylex and if it gets your
COMMENT token, push it on a local stack.  Then, call
yylex again.  Another COMMENT, push it again.  Keep
going until you get a non-COMMENT, and then attach the
stack to the non-COMMENT node and pass it back.

Kelly





reply via email to

[Prev in Thread] Current Thread [Next in Thread]