almost bug free
This commit is contained in:
parent
88610ace11
commit
20d083bc58
@ -44,7 +44,6 @@ extern YYSTYPE cool_yylval;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
int comment_nest_level;
|
int comment_nest_level;
|
||||||
int string_recover_error;
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -83,7 +82,7 @@ TRUE t[Rr][Uu][Ee]
|
|||||||
FALSE f[Aa][Ll][Ss][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 \t \b \f, the result is c.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
<STRINGREC>\\\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
|
||||||
|
*/
|
||||||
|
<STRINGREC>\"|\n {
|
||||||
|
BEGIN(INITIAL);
|
||||||
|
if (yytext[0] == '\n') curr_lineno += 1;
|
||||||
|
}
|
||||||
|
<STRINGREC>.
|
||||||
|
|
||||||
\" {
|
\" {
|
||||||
BEGIN(STRING);
|
BEGIN(STRING);
|
||||||
string_buf_ptr = string_buf; /* reset string buf ptr*/
|
string_buf_ptr = string_buf; /* reset string buf ptr*/
|
||||||
string_recover_error = 0; /* reset error flag*/
|
|
||||||
}
|
}
|
||||||
<STRING>[^\"\\\n] {
|
<STRING>[^\"\\\n\0] {
|
||||||
if (!string_recover_error){
|
/* it is necessary to exclude \0 here */
|
||||||
*string_buf_ptr = yytext[0];
|
*string_buf_ptr = yytext[0];
|
||||||
string_buf_ptr ++;
|
string_buf_ptr ++;
|
||||||
if (string_buf_ptr >= string_buf + MAX_STR_CONST) {
|
if (string_buf_ptr > string_buf + MAX_STR_CONST) {
|
||||||
string_recover_error = 1; /* string too long */
|
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.
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
<STRING>\\(.|\n) {
|
<STRING>\\(.|\n) {
|
||||||
if (!string_recover_error){
|
if (yytext[1] == '\n') {
|
||||||
/*
|
curr_lineno += 1;
|
||||||
* Within a string, a sequence ‘\c’ denotes the
|
}
|
||||||
* character ‘c’, except \b \t \n \f
|
/*
|
||||||
*/
|
* Within a string, a sequence ‘\c’ denotes the
|
||||||
switch(yytext[1]) {
|
* character ‘c’, except \b \t \n \f
|
||||||
case 'b': *string_buf_ptr = '\b'; break;
|
*/
|
||||||
case 't': *string_buf_ptr = '\t'; break;
|
switch(yytext[1]) {
|
||||||
case 'n': *string_buf_ptr = '\n'; break;
|
case 'b': *string_buf_ptr = '\b'; break;
|
||||||
case 'f': *string_buf_ptr = '\f'; break;
|
case 't': *string_buf_ptr = '\t'; break;
|
||||||
case '\n': *string_buf_ptr = '\n'; curr_lineno += 1; break;
|
case 'n': *string_buf_ptr = '\n'; break;
|
||||||
default: *string_buf_ptr = yytext[1]; break;
|
case 'f': *string_buf_ptr = '\f'; break;
|
||||||
}
|
default: *string_buf_ptr = yytext[1]; break; /* \\n is included here*/
|
||||||
string_buf_ptr ++;
|
}
|
||||||
if (string_buf_ptr >= string_buf + MAX_STR_CONST) {
|
/*
|
||||||
string_recover_error = 1; /* string too long */
|
* 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]
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
<STRING>\0 {
|
<STRING>\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’’.
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
/*
|
<STRING>\n {
|
||||||
* In either case(null char | too long), lexing should resume after the end of the string.
|
/* the escaped case should haved been captured in the escape rule */
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
<STRING>\"|\n {
|
|
||||||
BEGIN(INITIAL);
|
BEGIN(INITIAL);
|
||||||
if (yytext[0] == '\n') {
|
curr_lineno += 1;
|
||||||
curr_lineno += 1;
|
cool_yylval.error_msg = "Unterminated string constant";
|
||||||
}
|
return (ERROR);
|
||||||
if (!string_recover_error) {
|
/*
|
||||||
if (yytext[0] == '\"') {
|
* If a string contains an unescaped newline,
|
||||||
cool_yylval.symbol = stringtable.add_string(string_buf, string_buf_ptr - string_buf);
|
* report that error as ‘‘Unterminated string constant’’
|
||||||
return (STR_CONST);
|
* and resume lexing at the beginning of the next line
|
||||||
}
|
*/
|
||||||
else if (yytext[0] == '\n') {
|
}
|
||||||
/* the escaped case should haved been captured in the escape rule */
|
<STRING>\" {
|
||||||
cool_yylval.error_msg = "Unterminated string constant";
|
BEGIN(INITIAL);
|
||||||
return (ERROR);
|
cool_yylval.symbol = stringtable.add_string(string_buf, string_buf_ptr - string_buf);
|
||||||
/*
|
return (STR_CONST);
|
||||||
* 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’’.
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Integer constants
|
/* Integer constants
|
||||||
|
|||||||
@ -95,19 +95,39 @@ class Main {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
-- fuc
|
*)
|
||||||
>fuck > ? *)
|
class AdditionalTest {
|
||||||
(* (* (*shit
|
-- Int test
|
||||||
fuck
|
valid_int: Int <- 0123123;
|
||||||
scheisse*)sdsd*)*)
|
invalid_int: Int <- 0x1234;
|
||||||
sds
|
invalid_int2: Int <- 000_000;
|
||||||
inttest: Int <- 0011234; inttest2: Int <- 0x011234;
|
-- String test
|
||||||
--
|
valid_str: String <- "hello world!\n";
|
||||||
trUE <= True = true < fALse FaLSE;
|
escape_str: String <- "\"\n\\n\b\ff\t\K\*\00";
|
||||||
err_string "This is
|
err_newline_str: String <- "This is
|
||||||
not ok"
|
not ok";
|
||||||
err_string "This is \
|
err_newline_str2: String <- "This is \
|
||||||
also not ok"
|
also not ok";
|
||||||
ok_string "This is \
|
err_newline_str3: String <- "This is \
|
||||||
ok"
|
tricky lineno
|
||||||
escape_string "\"\n\\n\b\f\t\K\*"
|
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) *)
|
||||||
|
};
|
||||||
2
assignments/PA2/test_special1.cl
Normal file
2
assignments/PA2/test_special1.cl
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
-- String EOF
|
||||||
|
"haha
|
||||||
1
assignments/PA2/test_special2.cl
Normal file
1
assignments/PA2/test_special2.cl
Normal file
@ -0,0 +1 @@
|
|||||||
|
-- Single line comment EOF
|
||||||
BIN
assignments/PA2/test_special3.cl
Normal file
BIN
assignments/PA2/test_special3.cl
Normal file
Binary file not shown.
206
assignments/PA2/test_special4.cl
Normal file
206
assignments/PA2/test_special4.cl
Normal file
@ -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 ";
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user