From 5d36bfc9517c9655c34b3690f3d125839acb375f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 19 Oct 2023 11:49:19 +0300 Subject: (CI/PA1) Fully implemented strings --- .../coolc-spim/my-code/coursework/pa1/cool.flex | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex b/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex index b64e368..acc81ba 100644 --- a/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex +++ b/Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex @@ -108,11 +108,18 @@ yylval types: . { } <> { cool_yylval.error_msg = "EOF in comment"; BEGIN(INITIAL); return (ERROR); } -\"(\\([^\0]|\n)|[^\0\\\n"])*\" { cool_yylval.symbol = inttable.add_string(escapeString(yytext)); return (STR_CONST); } -\"(\\(.|\n)|[^\0\\\n"])*\" { cool_yylval.error_msg = "String contains escaped null character."; return (ERROR); } -\"(\\([^\0]|\n)|[^\\\n"])*\" { cool_yylval.error_msg = "String contains null character."; return (ERROR); } -\"(\\([^\0]|\n)|[^\0\\\n"])*\n { curr_lineno++; cool_yylval.error_msg = "Unterminated string constant"; return (ERROR); } -\"(\\([^\0]|\n)|[^\0\\\n"])* { cool_yylval.error_msg = "EOF in string constant"; return (ERROR); } +\"([^"\0\n\\]|\\[^\0])*\" { char* str = escapeString(yytext); + if (strlen(str) >= MAX_STR_CONST) { + cool_yylval.error_msg = "String constant too long"; return (ERROR); + } + else { + cool_yylval.symbol = inttable.add_string(str); return (STR_CONST); + } + } +\"([^"\0\n\\]|\\[^\0])*{ENDL} { cool_yylval.error_msg = "Unterminated string constant"; curr_lineno++; return (ERROR); } +\"([^"\0\n\\]|\\{ALL})*(\"|{ENDL}) { cool_yylval.error_msg = "String contains escaped null character."; return (ERROR); } +\"([^"\n\\]|\\[^\0])*(\"|{ENDL}) { cool_yylval.error_msg = "String contains null character."; return (ERROR); } +\"([^"\0\n\\]|\\[^\0])* { cool_yylval.error_msg = "EOF in string constant"; return (ERROR); } \*\) { cool_yylval.error_msg = "Unmatched *)"; return (ERROR); } -- cgit v1.3.1-26-gb76a