Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

TXT

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

/*

* The scanner definition for COOL.


*/

/*
* Stuff enclosed in %{ %} in the first section is copied verbatim to the
* output, so headers and global definitions are placed here to be visible
* to the code in the file. Don't remove anything that was here initially
*/
%option noyywrap
%{
#include <cool-parse.h>
#include <stringtab.h>
#include <utilities.h>

/* The compiler assumes these identifiers. */


#define yylval cool_yylval
#define yylex cool_yylex

/* Max size of string constants */


#define MAX_STR_CONST 1025
#define YY_NO_UNPUT /* keep g++ happy */

extern FILE *fin; /* we read from this file */

/* define YY_INPUT so we read from the FILE fin:


* This change makes it possible to use this scanner in
* the Cool compiler.
*/
#undef YY_INPUT
#define YY_INPUT(buf,result,max_size) \
if ( (result = fread( (char*)buf, sizeof(char), max_size, fin)) < 0) \
YY_FATAL_ERROR( "read() in flex scanner failed");

char string_buf[MAX_STR_CONST]; /* to assemble string constants */


char *string_buf_ptr;

extern int curr_lineno;


extern int verbose_flag;
int commentLvl = 0;

extern YYSTYPE cool_yylval;

/*
* Add Your own definitions here
*/

bool isStringLengthValid(){
/*oduzimanjem trenutne pozicije buffera od startne dobivamo duzinu stringa*/
int lenght = string_buf_ptr - string_buf;
return (lenght < MAX_STR_CONST);
}

%}
%x STRING
%x COMMENT

/*
* Define names for regular expressions here.
*/
DARROW =>
ASSIGN <-
LE <=
CLASS (c|C)(l|L)(a|A)(s|S)(s|S)
ELSE (e|E)(l|L)(s|S)(e|E)
FALSE f(a|A)(l|L)(s|S)(e|E)
FI (f|F)(i|I)
IF (i|I)(f|F)
IN (i|I)(n|N)
INHERITS (i|I)(n|N)(h|H)(e|E)(r|R)(i|I)(t|T)(s|S)
ISVOID (i|I)(s|S)(v|V)(o|O)(i|I)(d|D)
LET (l|L)(e|E)(t|T)
LOOP (l|L)(o|O)(o|O)(p|P)
POOL (p|P)(o|O)(o|O)(l|L)
THEN (t|T)(h|H)(e|E)(n|N)
WHILE (w|W)(h|H)(i|I)(l|L)(e|E)
CASE (c|C)(a|A)(s|S)(e|E)
ESAC (e|E)(s|S)(a|A)(c|C)
NEW (n|N)(e|E)(w|W)
OF (o|O)(f|F)
NOT (n|N)(o|O)(t|T)
TRUE t(r|R)(u|U)(e|E)
TYPE [A-Z][A-Z|a-z|0-9|_]*
OBJECT [a-z][A-Z|a-z|0-9|_]*
DIGIT [0-9]
INVALID "!"|"#"|"$"|"%"|"^"|"&"|">"|"?"|"`"
SPECIAL "~"|"@"|"*"|"("|")"|"-"|"+"|"/"|"="|"<"|"."|","|":"|";"|"{"|"}"
WHITESPACE " "|"\n"|"\f"|"\r"|"\t"|"\v"
NEWLINE "\n"
NULL_CHR "\0"
STRING_BEGIN \"
DASHED_COMMENT --(.)*
COMMENT_BEGIN "(*"
COMMENT_END "*)"

%%

{DARROW} { return (DARROW); }


{ASSIGN} { return (ASSIGN); }
{CLASS} { return (CLASS); }
{LOOP} { return (LOOP); }
{POOL} { return (POOL); }
{CASE} { return (CASE); }
{ISVOID} { return (ISVOID); }
{THEN} { return (THEN); }
{ESAC} { return (ESAC); }
{ELSE} { return (ELSE); }
{WHILE} { return (WHILE); }
{LE} { return (LE); }
{FI} { return (FI); }
{IF} { return (IF); }
{IN} { return (IN); }
{LET} { return (LET); }
{OF} { return (OF); }
{NEW} { return (NEW); }
{NOT} { return (NOT); }
{INHERITS} { return (INHERITS); }
{SPECIAL} { return (yytext[0]); }
{WHITESPACE} { }
{DASHED_COMMENT} { }
{NEWLINE} { curr_lineno++; }

{FALSE} {
cool_yylval.boolean = 0;
return (BOOL_CONST);
}

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

{TYPE} {
cool_yylval.symbol = stringtable.add_string(yytext);
return (TYPEID);
}

{OBJECT} {
cool_yylval.symbol = stringtable.add_string(yytext);
return (OBJECTID);
}

{DIGIT}+ {
cool_yylval.symbol = inttable.add_string(yytext);
return (INT_CONST);
}

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

{STRING_BEGIN} {
BEGIN(STRING);
string_buf_ptr = string_buf;
}

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

<STRING>{STRING_BEGIN} {
BEGIN(INITIAL);

/* resetiranje string buffer-a */


*string_buf_ptr = '\0';

if(!isStringLengthValid()){
cool_yylval.error_msg = "String constant too long";
return (ERROR);
}
cool_yylval.symbol = stringtable.add_string(string_buf);
return (STR_CONST);
}

<STRING>{NULL_CHR} {
BEGIN(INITIAL);
*string_buf_ptr = '\0';
cool_yylval.error_msg = "String contains null character";
return (ERROR);
}

<STRING>. {
*string_buf_ptr = yytext[0];
string_buf_ptr++;
}

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

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

{COMMENT_BEGIN} {
BEGIN(COMMENT);
commentLvl++;
}

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

<COMMENT>{COMMENT_BEGIN} { commentLvl++; }
<COMMENT>{COMMENT_END} {
commentLvl--;

if(commentLvl == 0){
BEGIN(INITIAL);
}
}

<COMMENT>. { }
<COMMENT>\n { curr_lineno++; }
<COMMENT><<EOF>> {
BEGIN(INITIAL);
cool_yylval.error_msg = "EOF in comment";
return (ERROR);
}
%%

You might also like