From 60ef31d7b8ef34acf54cd82777e11828297dbdff Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 19 Oct 2023 11:16:56 +0300 Subject: [CI/PA1] Added proper parsing of comments --- .../coolc-spim/my-code/coursework/pa1/cool.flex | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 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 39a15c3..942b3a7 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 @@ -67,6 +67,7 @@ char* escapeString(char* str) { } bool strStarted = false; +unsigned openedComments = 0; %} @@ -96,13 +97,21 @@ yylval types: char *error_msg; */ +%x MULTILINE + %% \"(\\(.|\n)|[^\\\n"])*\" { cool_yylval.symbol = inttable.add_string(escapeString(yytext)); return (STR_CONST); } \"(\\(.|\n)|[^\\\n"])*\n { curr_lineno++; cool_yylval.error_msg = "Unterminated string constant"; return (ERROR); } \"(\\(.|\n)|[^\\\n"])* { cool_yylval.error_msg = "EOF in string constant"; return (ERROR); } ---[^\n\r]* { } -\(\*.*\*\) { } +--[^\n\r]* { } +\(\* { openedComments++; BEGIN(MULTILINE); } +\*\) { if (--openedComments == 0) BEGIN(INITIAL); } +[\n\r] { curr_lineno++; } +. { } +<> { cool_yylval.error_msg = "EOF in comment"; BEGIN(INITIAL); return (ERROR); } + +\*\) { cool_yylval.error_msg = "Unmatched *)"; return (ERROR); } (?i:class) { return (CLASS); } (?i:else) { return (ELSE); } -- cgit v1.3.1-23-gd82d