Lex - double quote inside string

I have a lex grammar that contains the rules for a double-quoted string:

...
%x DOUBLEQUOTE
...
%%
"\""                { yylval->string = NULL; BEGIN(DOUBLEQUOTE); }
<DOUBLEQUOTE> {
    "\n"            {
                        /* reset column counter on new line */
                        PARSER->linepos = 0;
                        (PARSER->linenum)++;
                        expr_parser_append_string(PARSER, &(yylval->string), yytext);
                    }
    [^\"\n]+        { expr_parser_append_string(PARSER, &(yylval->string), yytext); }
    "\\\""          { expr_parser_append_string(PARSER, &(yylval->string), yytext); }
    "\""            {
                        BEGIN(INITIAL);
                        if ( yylval->string != NULL )
                            string_unescape_c(yylval->string);
                        return ( TOKEN_STRING );
                    }
}

Somehow the escape sequence \ " matches only at the beginning of the line. If \" appears last in the line, it looks like charactersand "are matched separately.

For example:

  • Skips: "\" "

  • Fails: " \" "

  • Failure: "This is string example: \"a string inside of string\""

Why does the escape sequence \ " not match the rule "\\\""when it appears last on the line?

+4
source share
2 answers

, ​​ . :

 "abc\"def"
  ^^^^       First token, longest match of [^"\n]+
      ^      Terminates quoted string

. , , , . :

<DOUBLEQUOTE>{
  \\?\n              { /* Handle newline */ }
  ([^"\\\n]|\\.)+    { expr_parser_append_string(PARSER,
                                                 &yylval->string,
                                                 yytext); }
  \"                 { BEGIN(INITIAL); ... }
}

. , , . . (\\.) , - .

0
[^\"\n]+        { expr_parser_append_string(PARSER, &(yylval->string), yytext); }

. , .

0

Source: https://habr.com/ru/post/1607719/


All Articles