In LEX, how to parse some code until a special sym

2019-07-30 16:13发布

问题:

I was wondering that in LEX, if i want to parse some code until a special symbol.

For example: $display("Hello World");

Here, I want to get $display("Hello World") but except ;.

I've already tried this: \$"display".*[^;], it didn't seem to work.

Any help will be appreciated. Thanks.

回答1:

. matches any character, and the Kleene star * will match many of whatever precedes it. In this case, you're matching display followed by a list of any other character. That'll happen to include the bit you want to capture and also the semicolon.

Obvious fix is to remove that part of the expression, and move the Kleene star to after the [^;] expression. Which will say "match multiple instances of any character which is not ;". This might need parens around the sub-expression (I can't remember pricese syntax as it varies between lexers).

Either \$"display"([^;])* or \$"display"[^;]*



标签: verilog lex