diff --git a/assignments/PA2/cool.flex b/assignments/PA2/cool.flex index 4a34985..c13b195 100644 --- a/assignments/PA2/cool.flex +++ b/assignments/PA2/cool.flex @@ -44,7 +44,6 @@ extern YYSTYPE cool_yylval; */ int comment_nest_level; -int string_recover_error; %} /* @@ -83,7 +82,7 @@ TRUE t[Rr][Uu][Ee] FALSE f[Aa][Ll][Ss][Ee] -%x NCOMMENT SCOMMENT STRING +%x NCOMMENT SCOMMENT STRING STRINGREC %% @@ -193,38 +192,64 @@ FALSE f[Aa][Ll][Ss][Ee] * \n \t \b \f, the result is c. * */ +\\\n { + curr_lineno += 1; + } + /* + * In either case(null char | too long), lexing should resume after the end of the string. + * The end of the string is defined as either: + * the beginning of the next line if an unescaped newline occurs after these errors are encountered + * after the closing ” otherwise + */ +\"|\n { + BEGIN(INITIAL); + if (yytext[0] == '\n') curr_lineno += 1; + } +. + \" { BEGIN(STRING); string_buf_ptr = string_buf; /* reset string buf ptr*/ - string_recover_error = 0; /* reset error flag*/ } -[^\"\\\n] { - if (!string_recover_error){ - *string_buf_ptr = yytext[0]; - string_buf_ptr ++; - if (string_buf_ptr >= string_buf + MAX_STR_CONST) { - string_recover_error = 1; /* string too long */ - } +[^\"\\\n\0] { + /* it is necessary to exclude \0 here */ + *string_buf_ptr = yytext[0]; + string_buf_ptr ++; + if (string_buf_ptr > string_buf + MAX_STR_CONST) { + BEGIN(STRINGREC); + cool_yylval.error_msg = "String constant too long"; + return (ERROR); + /* + * When a string is too long, + * report the error as ‘‘String constant too long’’ + * in the error string in the ERROR token. + */ } } \\(.|\n) { - if (!string_recover_error){ - /* - * Within a string, a sequence ‘\c’ denotes the - * character ‘c’, except \b \t \n \f - */ - switch(yytext[1]) { - case 'b': *string_buf_ptr = '\b'; break; - case 't': *string_buf_ptr = '\t'; break; - case 'n': *string_buf_ptr = '\n'; break; - case 'f': *string_buf_ptr = '\f'; break; - case '\n': *string_buf_ptr = '\n'; curr_lineno += 1; break; - default: *string_buf_ptr = yytext[1]; break; - } - string_buf_ptr ++; - if (string_buf_ptr >= string_buf + MAX_STR_CONST) { - string_recover_error = 1; /* string too long */ - } + if (yytext[1] == '\n') { + curr_lineno += 1; + } + /* + * Within a string, a sequence ‘\c’ denotes the + * character ‘c’, except \b \t \n \f + */ + switch(yytext[1]) { + case 'b': *string_buf_ptr = '\b'; break; + case 't': *string_buf_ptr = '\t'; break; + case 'n': *string_buf_ptr = '\n'; break; + case 'f': *string_buf_ptr = '\f'; break; + default: *string_buf_ptr = yytext[1]; break; /* \\n is included here*/ + } + /* + * The two characters \0(0x5c30) is valid, but actually converted to '0'(0x30) + * this is handled in the default branch + */ + string_buf_ptr ++; + if (string_buf_ptr > string_buf + MAX_STR_CONST) { + BEGIN(STRINGREC); + cool_yylval.error_msg = "String constant too long"; + return (ERROR); } } /* @@ -242,52 +267,30 @@ FALSE f[Aa][Ll][Ss][Ee] */ } \0 { - string_recover_error = 2; /* null character */ + BEGIN(STRINGREC); + cool_yylval.error_msg = "String contains null character."; + return (ERROR); + /* + * If the string contains invalid characters (i.e., the null character), + * report this as ‘‘String contains null character’’. + */ } - /* - * In either case(null char | too long), lexing should resume after the end of the string. - * The end of the string is defined as either: - * the beginning of the next line if an unescaped newline occurs after these errors are encountered - * after the closing ” otherwise - */ -\"|\n { +\n { + /* the escaped case should haved been captured in the escape rule */ BEGIN(INITIAL); - if (yytext[0] == '\n') { - curr_lineno += 1; - } - if (!string_recover_error) { - if (yytext[0] == '\"') { - cool_yylval.symbol = stringtable.add_string(string_buf, string_buf_ptr - string_buf); - return (STR_CONST); - } - else if (yytext[0] == '\n') { - /* the escaped case should haved been captured in the escape rule */ - cool_yylval.error_msg = "Unterminated string constant"; - return (ERROR); - /* - * If a string contains an unescaped newline, - * report that error as ‘‘Unterminated string constant’’ - * and resume lexing at the beginning of the next line - */ - } - } - else if (string_recover_error == 1) { - cool_yylval.error_msg = "EOF in string constant"; - return (ERROR); - /* - * When a string is too long, - * report the error as ‘‘String constant too long’’ - * in the error string in the ERROR token. - */ - } - else if (string_recover_error == 2){ - cool_yylval.error_msg = "String contains null character"; - return (ERROR); - /* - * If the string contains invalid characters (i.e., the null character), - * report this as ‘‘String contains null character’’. - */ - } + curr_lineno += 1; + cool_yylval.error_msg = "Unterminated string constant"; + return (ERROR); + /* + * If a string contains an unescaped newline, + * report that error as ‘‘Unterminated string constant’’ + * and resume lexing at the beginning of the next line + */ + } +\" { + BEGIN(INITIAL); + cool_yylval.symbol = stringtable.add_string(string_buf, string_buf_ptr - string_buf); + return (STR_CONST); } /* Integer constants diff --git a/assignments/PA2/test.cl b/assignments/PA2/test.cl index 87f84da..5260cd9 100644 --- a/assignments/PA2/test.cl +++ b/assignments/PA2/test.cl @@ -95,19 +95,39 @@ class Main { } }; }; - -- fuc - >fuck > ? *) - (* (* (*shit - fuck - scheisse*)sdsd*)*) - sds - inttest: Int <- 0011234; inttest2: Int <- 0x011234; --- - trUE <= True = true < fALse FaLSE; - err_string "This is - not ok" - err_string "This is \ - also not ok" - ok_string "This is \ - ok" - escape_string "\"\n\\n\b\f\t\K\*" \ No newline at end of file +*) +class AdditionalTest { +-- Int test + valid_int: Int <- 0123123; + invalid_int: Int <- 0x1234; + invalid_int2: Int <- 000_000; +-- String test + valid_str: String <- "hello world!\n"; + escape_str: String <- "\"\n\\n\b\ff\t\K\*\00"; + err_newline_str: String <- "This is + not ok"; + err_newline_str2: String <- "This is \ + also not ok"; + err_newline_str3: String <- "This is \ + tricky lineno + not ok"; + ok_newline_string: String <- "This is \ + ok"; +-- Bool test + valid_bool_true: Bool <- true = tRUE = trUe; + valid_bool_false: Bool <- false = fALsE = fALSE; + invalid_bool_true: Bool <- True = TRUe; + invalid_bool_false: Bool <- False = FALSE; +-- Invalid character test + _hello: Bool <- 1 != 2 <> 3_3?; +-- Comment test + hello: Type <- 0; +-- Valid nested comment + (* (* feigling (* versagar + scheisse + scheisse*) verpfeif *) +*) +-- Invalid nested comment +(* *) (( lost left close, Unmatched * ) expected *) +(* (* lost right close, EOF in comment expected) *) +}; \ No newline at end of file diff --git a/assignments/PA2/test_special1.cl b/assignments/PA2/test_special1.cl new file mode 100644 index 0000000..77eeb13 --- /dev/null +++ b/assignments/PA2/test_special1.cl @@ -0,0 +1,2 @@ +-- String EOF +"haha \ No newline at end of file diff --git a/assignments/PA2/test_special2.cl b/assignments/PA2/test_special2.cl new file mode 100644 index 0000000..26d510b --- /dev/null +++ b/assignments/PA2/test_special2.cl @@ -0,0 +1 @@ +-- Single line comment EOF \ No newline at end of file diff --git a/assignments/PA2/test_special3.cl b/assignments/PA2/test_special3.cl new file mode 100644 index 0000000..ddf4885 Binary files /dev/null and b/assignments/PA2/test_special3.cl differ diff --git a/assignments/PA2/test_special4.cl b/assignments/PA2/test_special4.cl new file mode 100644 index 0000000..9503be1 --- /dev/null +++ b/assignments/PA2/test_special4.cl @@ -0,0 +1,206 @@ +(* +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +Long comment test +*) +long_string: String <- " long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string"; + +long_string_resume: String <- " long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string +long string long string long string long string long string long string long string long string long string long string"; + +long_string_1026: String <- " long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long strin "; + +long_string_1025: String <- " long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long strin "; + +long_string_1024: String <- " long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long string long string long string long string long string \ +long string long string long string long string long string long strin "; +