Silvestrini
Silvestrini

Reputation: 445

Bison - Skipping tokens after a specific token

I'm trying to simulate a comment statement in the language I'm working on. By using the keyword REM, I want to ignore everything that goes after that token until it hits an enter.

Example would be like

REM randominstruction {;}

Where randominstruction has some random instructions in C. I want to ignore the whole randominstruction when the REM token is before it.

Upvotes: 0

Views: 243

Answers (1)

rici
rici

Reputation: 241671

Recognizing and ignoring comments is best done in the lexical scanner, using a rule like:

REM([[:blank:]].*)?$     ;

That's not the same as "Where randominstruction has some random instructions in C", which is a bit misleading. The comment ignores the line of text, without regard to whether it consists of valid language tokens are not.

The complexity of the regular expression above is due to the need for REM to not be part of a longer word. Insisting that it is followed by whitespace might be too strict (is REM(....) a valid comment?), so it is possible that a better one would be:

REM([^[:alnum:]_].*)?$     ;

The intent of ?$ is to also accept REM if it is the only thing on the line. It may well have been clearer to use two patterns:

REM[^[:alnum:]_].*     ;
REM$                   ;

Upvotes: 1

Related Questions