TXT
TXT
TXT
/*
* 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>
/*
* 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 "*)"
%%
{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);
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);
}
%%