diff options
[CI/PA1] Added proper parsing of comments
1 files changed, 11 insertions, 2 deletions
| -rw-r--r-- | Year 3/Compilers, Introduction/coolc-spim/my-code/coursework/pa1/cool.flex | 13 |
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]* { } +<MULTILINE,INITIAL>\(\* { openedComments++; BEGIN(MULTILINE); } +<MULTILINE>\*\) { if (--openedComments == 0) BEGIN(INITIAL); } +<MULTILINE>[\n\r] { curr_lineno++; } +<MULTILINE>. { } +<MULTILINE><<EOF>> { 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); } |
