Skip to content

Commit

Permalink
Finished cool file
Browse files Browse the repository at this point in the history
  • Loading branch information
hhaurubabo committed Oct 21, 2021
1 parent e12343d commit e796d19
Showing 1 changed file with 56 additions and 27 deletions.
83 changes: 56 additions & 27 deletions PA2/cool.flex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ extern YYSTYPE cool_yylval;
* Add Your own definitions here
*/

static std::string currentStr; /* For determining if a string is too long */

int commentLayer; /* For counting layers of nested comment */

%}

/*
Expand All @@ -53,11 +57,12 @@ extern YYSTYPE cool_yylval;
INTEGER [0-9]+
NEWLINE "\n"
OneLineComm --[^\n]*
TypeID [a-z][a-z0-9]*
ObjectID [a-z][A-Z0-9]*
TYPEID [a-z][a-z0-9]*
OBJECTID [a-z][A-Z0-9]*
WHITESPACE [\v \f \r \t]
SINGLECHAR [+*/@$\><,.:-]
DARROW =>
ASSIGN <-
TRUE t(?i:rue)
FALSE f(?i:alse)

Expand All @@ -70,7 +75,7 @@ FALSE f(?i:alse)
* One-line comment
*/

{OneLineComm}.* {}
{OneLineComm}.* ;

/*
* Nested comments
Expand All @@ -85,9 +90,16 @@ FALSE f(?i:alse)
return (ERROR);
}

<NCOMMENT>[^*\n]* /*eat anything that's not a '*' */
<NCOMMENT>"(*" {
commentLayer++;
}

<NCOMMENT>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */
<NCOMMENT>"*)" {
if(commentLayer == 1) {
BEGIN(INITIAL);
}
commentLayer--;
}

<NCOMMENT>\n {
++curr_lineno;
Expand All @@ -99,12 +111,7 @@ FALSE f(?i:alse)
return (ERROR);
}

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



<NCOMMENT>. ;

/*
* The multiple-character operators.
Expand All @@ -114,9 +121,14 @@ FALSE f(?i:alse)
return (DARROW);
}

{ASSIGN} {
return (ASSIGN);
}

/*
* The single-character operators.
*/

{SINGLECHAR} {
return (yytext[0]);
}
Expand All @@ -134,7 +146,7 @@ FALSE f(?i:alse)
* The type identifiers.
*/

{TypeID} {
{TYPEID} {
cool_yylval.symbol = idtable.add_string(yytext);
return TYPEID;
}
Expand All @@ -143,14 +155,15 @@ FALSE f(?i:alse)
* The object identifiers.
*/

{ObjectID} {
{OBJECTID} {
cool_yylval.symbol = idtable.add_string(yytext);
return OBJECTID;
}

/*
* New line
*/

{NEWLINE} {
curr_lineno++;
}
Expand All @@ -159,7 +172,7 @@ FALSE f(?i:alse)
* White space
*/

{WHITESPACE} {}
{WHITESPACE} ;


/*
Expand Down Expand Up @@ -211,17 +224,22 @@ FALSE f(?i:alse)
*/

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

<STRING>\" {
BEGIN(INITIAL);

if(currentStr.size() >= MAX_STR_CONST) {
cool_yylval.error.msg = "String constant too long";
return (ERROR);
}
}

<STRING>\n {
curr_lineno++;
++curr_lineno;
cool_yylval.error_msg = "Unterminated string constant.";
return (ERROR);
}

<STRING>\0 {
Expand All @@ -237,25 +255,36 @@ FALSE f(?i:alse)
}

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

<STRING>\\t {
*string_buf_ptr++ = '\t';
currentStr++ = '\t';
}

<STRING>\\r {
*string_buf_ptr++ = '\r';
currentStr++ = '\r';
}

<STRING>\\b {
*string_buf_ptr++ = '\b';
currentStr++ = '\b';
}

<STRING>\\f {
*string_buf_ptr++ = '\f';
currentStr++ = '\f';
}

<STRING>\\(.|\n) {
currentStr++ = yytext[1];
}

<STRING>\\. {
<STRING>. {
currentStr++ = yytext;
}

. {
cool_yylval.error_msg = yytext;
return (ERROR);
}

%%

0 comments on commit e796d19

Please sign in to comment.