Skip to content

Instantly share code, notes, and snippets.

@awave1
Last active February 26, 2020 17:11
Show Gist options
  • Select an option

  • Save awave1/054243d6242ebf287ad9ec76fb323f70 to your computer and use it in GitHub Desktop.

Select an option

Save awave1/054243d6242ebf287ad9ec76fb323f70 to your computer and use it in GitHub Desktop.
%{
#include ... // your includes
// ensure that string_builder is initialized within the scanner, otherwise reading string tokens will fail
static string_builder_t *sb = sb_init();
%}
...the rest of your lexer stuff
%%
<STRING>{
\" {
BEGIN(INITIAL);
if (!sb_is_empty(sb)) {
yytext = sb_build_str(sb);
sb_reset(sb);
}
return yy::Parser::token::T_STR;
}
{string_char} {
if (YYText()[0] != '\0') {
sb_append(sb, YYText());
}
}
\\t { sb_append(sb, yytext); }
\\n { sb_append(sb, yytext); }
\\r { sb_append(sb, yytext); }
\\b { sb_append(sb, yytext); }
\\f { sb_append(sb, yytext); }
\\' { sb_append(sb, yytext); }
\\\" { sb_append(sb, yytext); }
\\ { sb_append(sb, yytext); }
<<EOF>> {
// in case if string is unterminated and there's EOF
fprintf(stderr, "unterminated string on line %d\n", yylineno);
BEGIN(INITIAL);
warning_count++;
}
{newline} {
fprintf(stderr, "unterminated string on line %d\n", yylineno);
BEGIN(INITIAL);
warning_count++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment