1

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 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...

pflz
  • 1,891
  • 4
  • 26
  • 32
  • possible duplicate of [Regular expression for a string literal in flex/lex](http://stackoverflow.com/questions/2039795/regular-expression-for-a-string-literal-in-flex-lex) – kennytm Mar 16 '11 at 19:42

2 Answers2

3

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
Heng Ye
  • 341
  • 4
  • 17
Chris
  • 31
  • 2
1

You may find these links helpful

Stephan
  • 41,764
  • 65
  • 238
  • 329