How to recognize a string literal for a scanner in lex?
Ok, I have been trying to find a regular expression in lex that will recognize a C-like string literal in an input string. eg. in printf("stackoverflow")
, "stackoverflow" should be recognized as a 开发者_StackOverflow社区string literal.
I have been trying the following:
"[.]+"
["][.]+["]
\"[.]+\"
[\"][.]+[\"]
"""[.]+"""
None of these is working. The lexeme recognized each time is " alone. What should I do? Thanks in advance...
Simple, try this:
\".*\" printf("STRING[%s]", yytext);
\'.*\' printf("STRING[%s]", yytext);
When compiled and run, a quick test shows it correctly parses strings like
"hello world!"
STRING["hello world!"]
'hello world!'
STRING['hello world!']
'john\'s cat'
STRING['john\'s cat']
'mary said "hello!"'
STRING['mary said "hello!"']
"frank said \"goodbye!\""
these are words "which contain" a string
these are words STRING["which contain"] a string
You may find these links helpful
ANSI C grammar, Lex specification
ANSI C Yacc grammar
精彩评论