0

I am using flex for writing a scanner for verilog code. I want to write a regular expression which takes all characters except words

define, ifdef, //, /* .

I tried negative lookahead concept, but seems like flex doesn't support it.

Ex: If there is a string

assign a = b;  **This should be scanned in the same expression**
assign a =b; //comment **Here line assign a=b should match the regex, but //comment should not match.**
Gary
  • 13,303
  • 18
  • 49
  • 71
Sai
  • 1
  • 2
  • 1
    See https://stackoverflow.com/q/22326399/1256452 and perhaps more importantly https://stackoverflow.com/q/13670165/1256452 – torek Jul 03 '18 at 17:00

1 Answers1

0

To swallow such lines in flex is done by capturing the symbols and then not emitting them. If you only want to remove those tokens and pass everything else, you just have you rules to exclude tokens have higher priority to scan over those tokens, and then have a rule at the end that matches a single character and emits it.

To capture a single line comment in flex you can use something like //.*\n and have the action be not to emit the characters.

For multiline comment "/*".*"*/"

And for the words you just surround them in qoutes like "#ifdef"

Check here for more http://people.cs.aau.dk/~marius/sw/flex/Flex-Regular-Expressions.html

EDIT: You will not need look ahead for removing structures like comments, they are a regular language and therefore can be described by a regular expression.

Garrigan Stafford
  • 1,331
  • 9
  • 20