Skip to content

Commit

Permalink
update current code
Browse files Browse the repository at this point in the history
  • Loading branch information
babohhauru committed Oct 19, 2021
1 parent 966526d commit bf2e3bb
Showing 1 changed file with 97 additions and 3 deletions.
100 changes: 97 additions & 3 deletions cool.flex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ extern int verbose_flag;

extern YYSTYPE cool_yylval;

/*
* Add Your own definitions here
*/

std::string str = "";
%}

/*
Expand All @@ -55,24 +60,56 @@ SINGLECHAR [+-*/@$\><,.:]
DARROW =>
TRUE t(?i:rue)
FALSE f(?i:alse)

%x NCOMMENT
%x STRING

%%

/*
* One-line comment
*/

{OneLineComm} {}

/*
* Nested comments
*/

"(*" {
BEGIN (NCOMMENT);
}

"*)" {
cool_yylval.error_msg = "Unmatched *)";
return (ERROR);
}

<NCOMMENT>[^*\n]* /*eat anything that's not a '*' */

<NCOMMENT>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */

<NCOMMENT>\n {
++curr_lineno;
}

<NCOMMENT><<EOF>> {
BEGIN (INITIAL);
cool_yylval.error_msg = "EOF in comment.";
return (ERROR);
}

<NCOMMENT>"*"+")" {
BEGIN (INITIAL);
}




/*
* The multiple-character operators.
*/

{DARROW} {
return (DARROW);
}
Expand All @@ -96,14 +133,16 @@ FALSE f(?i:alse)
/*
* The type identifiers.
*/

{TypeID} {
cool_yylval.symbol = idtable.add_string(yytext);
return TYPEID;
}

/*
/*
* The object identifiers.
*/

{ObjectID} {
cool_yylval.symbol = idtable.add_string(yytext);
return OBJECTID;
Expand All @@ -119,13 +158,15 @@ FALSE f(?i:alse)
/*
* White space
*/

{WHITESPACE} {}


/*
* Keywords are case-insensitive except for the values true and false,
* which must begin with a lower-case letter.
*/

(?i:class) return CLASS;
(?i:else) return ELSE;
(?i:fi) return FI;
Expand All @@ -145,16 +186,18 @@ FALSE f(?i:alse)
(?i:not) return NOT;

/*
* true condition
* True condition
*/

{TRUE} {
cool_yylval.boolean = 1;
return BOOL_CONST;
}

/*
* false condition
* False condition
*/

{FALSE} {
cool_yylval.boolean = 0;
return BOOL_CONST;
Expand All @@ -167,6 +210,57 @@ FALSE f(?i:alse)
*
*/

\" {
string_buf_ptr = string_buf;
str = "";
BEGIN(STRING);
}

<STRING>\" {
BEGIN(INITIAL);
if (str.size() >= MAX_STR_CONST) {
cool_yylval.error_msg = "String constant too long";
return(ERROR);
}
}

<STRING>\n {
curr_lineno++;
str += '\n';
}

<STRING>\0 {
BEGIN(INITIAL);
cool_yylval.error_msg = "Null character in string.";
return(ERROR);
}

<STRING><<EOF>> {
BEGIN(INITIAL);
cool_yylval.error_msg = "EOF in string constant.";
return (ERROR);
}

<STRING>\\n {
BEGIN(INITIAL);
++curr_lineno;
cool_yylval.error_msg = "‘Unterminated string constant.";
return (ERROR);
}
<STRING>\\t {
str += '\t';
}
<STRING>\\r {
str += '\r';
}
<STRING>\\b {
str += '\b';
}
<STRING>\\f {
str += '\f';
}

<STRING>\\. {
str += yytext[1];
}
%%

0 comments on commit bf2e3bb

Please sign in to comment.