Skip to content

Instantly share code, notes, and snippets.

@lgaff
Created October 16, 2012 04:16
Show Gist options
  • Save lgaff/3897208 to your computer and use it in GitHub Desktop.
Save lgaff/3897208 to your computer and use it in GitHub Desktop.
Lexical analysis of strings for the Cool programming language
<INITIAL>{QUOTE} { BEGIN STRCONST; string_buf_ptr = string_buf; }
<STRCONST>{QUOTE} { BEGIN 0;
cool_yylval.symbol = stringtable.add_string(string_buf);
reset_buffer();
return STR_CONST;
}
<STRCONST>[^\\\"\n\0]+ { add_to_buffer(yytext); }
<STRCONST>\\\n { if(!add_to_buffer("\n")) { BEGIN 0; return ERROR; } }
<STRCONST>\\n { if(!add_to_buffer("\n")) { BEGIN 0; return ERROR; } }
<STRCONST>\\\\ { if(!add_to_buffer("\\")) { BEGIN 0; return ERROR; } }
<STRCONST>\\t { if (!add_to_buffer("\t")) { BEGIN 0; return ERROR; } }
<STRCONST>\\b { if(!add_to_buffer("\b")) { BEGIN 0; return ERROR; } }
<STRCONST>\\f { if(!add_to_buffer("\f")) { BEGIN 0; return ERROR; } }
<STRCONST>\\0 { if(!add_to_buffer("\0")) { BEGIN 0; return ERROR; } }
<STRCONST>\\/[^ntbf\n] { if(!add_to_buffer(yytext)) { BEGIN 0; return ERROR; } }
<STRCONST>\n { BEGIN 0; cool_yylval.error_msg = "Unterminated string constant."; return ERROR; }
<STRCONST><<EOF>> { BEGIN 0; cool_yylval.error_msg = "EOF in string constant."; return ERROR; }
<STRCONST>\0 { BEGIN 0; cool_yylval.error_msg = "String contains null character."; return ERROR; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment