akshay mishra
akshay mishra

Reputation: 37

How to detect string during lexical analysis?

I am using some syntax to detect string during lexical analysis

"".*""    return TOK_STRING; 

but this is not working.

Upvotes: -2

Views: 325

Answers (1)

rici
rici

Reputation: 241821

I think you want

\".*\"

but be aware that . in flex does not match newlines. And, as @chqrlie mentions in a comment, it does match ", so it will match to the end of the last string, and not the current one.

So a better pattern might be:

\"[^"]*\"

([^"] matches any character including newlines, except ").

But then you have no way to include a " in a string. So you will have to decide what syntax that should be. If you wanted to implement SQL style, with doubled quotes representing a single quote inside a string, you could use

\"([^"]|\"\")*\"

For the possibly more common backslash escape:

\"([^"]|\\(.|\n))*\"

Upvotes: 2

Related Questions