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>
#include <string.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;

extern YYSTYPE cool_yylval;

int dubina_komentara=0;
int ukupno_ucitano_znakova=0;
int provjeri_duljinu_unesenog_stringa(char zadnje_ucitano[MAX_STR_CONST]);
%}

%x KOMENTAR
%x JEDNOLINIJSKI_KOMENTAR
%x STRING
%x IZADI_IZ_STRINGA_S_GRESKOM

A a|A
B b|B
C c|C
D d|D
E e|E
F f|F
G g|G
H h|H
I i|I
J j|J
K k|K
L l|L
M m|M
N n|N
O o|O
P p|P
Q q|Q
R r|R
S s|S
T t|T
U u|U
V v|V
W w|W
X x|X
Y y|Y
Z z|Z

CIJELI_BROJ [0-9]+
IDENTIFIKATOR [a-zA-Z0-9_]+
IDENTIFIKATOR_OBJEKTA [a-z][a-zA-Z0-9_]*
IDENTIFIKATOR_TIPA [A-Z][a-zA-Z0-9_]*

%%

{C}{L}{A}{S}{S} {return (CLASS) ;}


{E}{L}{S}{E} {return (ELSE) ;}
f{A}{L}{S}{E} {cool_yylval.boolean = 0; return (BOOL_CONST) ;}
t{R}{U}{E} {cool_yylval.boolean = 1; return (BOOL_CONST) ;}
{F}{I} {return (FI) ;}
{I}{F} {return (IF) ;}
{I}{N} {return (IN) ;}
{I}{N}{H}{E}{R}{I}{T}{S} {return (INHERITS) ;}
{I}{S}{V}{O}{I}{D} {return (ISVOID) ;}
{L}{E}{T} {return (LET) ;}
{L}{O}{O}{P} {return (LOOP) ;}
{P}{O}{O}{L} {return (POOL) ;}
{T}{H}{E}{N} {return (THEN) ;}
{W}{H}{I}{L}{E} {return (WHILE) ;}
{C}{A}{S}{E} {return (CASE) ;}
{E}{S}{A}{C} {return (ESAC) ;}
{N}{E}{W} {return (NEW) ;}
{O}{F} {return (OF) ;}
{N}{O}{T} {return (NOT) ;}

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


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

{CIJELI_BROJ} {cool_yylval.symbol = inttable.add_string(yytext); return


(INT_CONST);}

"+"|"-"|"~"|"@"|"<"|"="|"/"|"*"|"{"|"}"|"("|")"|";"|":"|"."|"," {return yytext[0];}


"=>" { return (DARROW) ; }
"<=" {return (LE);}
"<-" {return (ASSIGN);}

"!"|"#"|"$"|"%"|"^"|"&"|"_"|">"|"?"|"`"|"["|"]"|"\\"|\||#|#|#|#
{cool_yylval.error_msg=yytext; return ERROR;}

\r\n|\n curr_lineno++;
[ \t\f\v\r]+ {}

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

"--" BEGIN JEDNOLINIJSKI_KOMENTAR;


<JEDNOLINIJSKI_KOMENTAR>{
\n { BEGIN INITIAL; curr_lineno++;}
<<EOF>> {yyterminate(); BEGIN INITIAL;}
. { }
}

"(*" {BEGIN KOMENTAR; ++dubina_komentara;}


<KOMENTAR>{
"*)" {--dubina_komentara; if(!dubina_komentara) BEGIN INITIAL;}
"(*" {++dubina_komentara;}
<<EOF>> { cool_yylval.error_msg="EOF in comment"; yyterminate(); return ERROR;
BEGIN INITIAL;}
\n { curr_lineno++; }
. {}
}

\" {string_buf_ptr=string_buf; BEGIN STRING; string_buf[0]='\0';}


<STRING>{
\" { BEGIN INITIAL; cool_yylval.symbol = stringtable.add_string(string_buf);
return STR_CONST;}

<<EOF>> { cool_yylval.error_msg="EOF in string constant"; yyterminate(); return


ERROR; BEGIN INITIAL;}
\n {
cool_yylval.error_msg="Unterminated string constant";
string_buf[0]='\0';
BEGIN INITIAL;
return ERROR;
}
\0 {
cool_yylval.error_msg="Null character in string constant";
string_buf[0]='\0';
BEGIN IZADI_IZ_STRINGA_S_GRESKOM;
return ERROR;
}
\0\0 return provjeri_duljinu_unesenog_stringa("\0");
\\\n {
curr_lineno++;
return provjeri_duljinu_unesenog_stringa("\n");
}
\\n {
curr_lineno++;
return provjeri_duljinu_unesenog_stringa("\n");
}
\\t return provjeri_duljinu_unesenog_stringa("\t");
\\f return provjeri_duljinu_unesenog_stringa("\f");
\\b return provjeri_duljinu_unesenog_stringa("\b");
\\. return provjeri_duljinu_unesenog_stringa(yytext+1);
. return provjeri_duljinu_unesenog_stringa(yytext);
}

<IZADI_IZ_STRINGA_S_GRESKOM>{
\" {BEGIN INITIAL;}
\n { curr_lineno++; BEGIN INITIAL; }
. { }
}
%%

int provjeri_duljinu_unesenog_stringa(char zadnje_ucitano[MAX_STR_CONST]) {


if(!zadnje_ucitano[0]) ukupno_ucitano_znakova++;
if(ukupno_ucitano_znakova+strlen(zadnje_ucitano)>MAX_STR_CONST-1) {
ukupno_ucitano_znakova=0;
string_buf_ptr=string_buf;
string_buf[0]='\0';
BEGIN IZADI_IZ_STRINGA_S_GRESKOM;
return ERROR;
}
else {
if(!zadnje_ucitano[0]) string_buf[strlen(string_buf)]=zadnje_ucitano[0];
strcat(string_buf, zadnje_ucitano);
string_buf_ptr++;
ukupno_ucitano_znakova+=strlen(zadnje_ucitano);
return 0;
}
}

You might also like