TXT
TXT
TXT
%{
#include <cool-parse.h>
#include <stringtab.h>
#include <utilities.h>
#include <iostream>
//-------------------DEFINITIONS----------------------
int counter=0;
// Provjerava tip otvorenog komentara true=(**), false="--"
bool comm=false;
bool max_strlen_check();
void open_comment_check(int);
int eof_in_comment_check(bool);
int max_strlen_error();
%}
/* ----------------REGULAR EXPRESSIONS----------------------------*/
DARROW =>
NEWLINE \n
WHITESPACE [ \f\r\t\v]+
DASHCOMMENT "--"
TRUE [t][Rr][Uu][Ee]
FALSE [f][Aa][Ll][Ss][Ee]
TRUETYPEID [Tt][Rr][Uu][Ee]
LETTER [a-zA-Z_]
DIGIT [0-9]
INVISIBLE_CHARACTERS [\001-\037]
CLASS (?i:class)
ELSE (?i:else)
FI (?i:fi)
IF (?i:if)
IN (?i:in)
INHERITS (?i:inherits)
LET (?i:let)
LOOP (?i:loop)
POOL (?i:pool)
THEN (?i:then)
WHILE (?i:while)
CASE (?i:case)
ESAC (?i:esac)
NEW (?i:new)
ISVOID (?i:isvoid)
OF (?i:of)
NOT (?i:not)
ASSIGN (?i:assign)
TYPEID [A-Z]({DIGIT}|{LETTER})*
OBJECTID [a-z]({DIGIT}|{LETTER})*
INT_CONST {DIGIT}+
%x COMMENT
%x STRING
%%
<INITIAL>{
{DARROW} { return (DARROW); }
"(*" {
counter++;
comm=true;
BEGIN COMMENT;
}
{DASHCOMMENT} {
comm=false;
BEGIN COMMENT;
}
"*)" {
cool_yylval.error_msg = "Unmatched *)";
return ERROR;
}
\" {
BEGIN STRING;
string_buf_ptr = string_buf;
}
<COMMENT>{
"*)" {
counter--;
open_comment_check(counter);
}
"(*" {
if(comm){
counter++;
comm=true;
BEGIN COMMENT;
}
}
{NEWLINE} {
curr_lineno++;
if(!comm) BEGIN INITIAL;
}
<<EOF>> {
eof_in_comment_check(comm);
}
. {}
}
<STRING>{
\" {
BEGIN INITIAL;
if(max_strlen_check()) return max_strlen_error();
*string_buf_ptr = '\0';
cool_yylval.symbol = stringtable.add_string(string_buf);
return(STR_CONST);
}
<<EOF>> {
cool_yylval.error_msg = "EOF in string constant";
BEGIN INITIAL;
return ERROR;
}
\\\n {
*string_buf_ptr++ = '\n';
}
\n {
curr_lineno++;
cool_yylval.error_msg = "Unterminated string constant";
BEGIN INITIAL;
return ERROR;
}
\0 {
cool_yylval.error_msg = "String contains null character";
return ERROR;
}
\\[^ntbf] {
if (max_strlen_check()) return max_strlen_check();
*string_buf_ptr++ = yytext[1];
}
\\[n] {
if (max_strlen_check()) return max_strlen_check();
*string_buf_ptr++ = '\n';
}
\\[t] {
if (max_strlen_check()) return max_strlen_check();
*string_buf_ptr++ = '\t';
}
\\[b] {
if (max_strlen_check()) return max_strlen_check();
*string_buf_ptr++ = '\b';
}
\\[f] {
if (max_strlen_check()) return max_strlen_check();
*string_buf_ptr++ = '\f';
}
. {
if (max_strlen_check()) return max_strlen_error();
*string_buf_ptr++ = *yytext;
}
{NEWLINE} {curr_lineno++;}
{WHITESPACE} {}
{FALSE} {
cool_yylval.boolean = 0;
return(BOOL_CONST);
}
{INT_CONST} {
cool_yylval.symbol = inttable.add_string(yytext);
return(INT_CONST);
}
{ELSE} { return(ELSE);}
{TRUE} {
cool_yylval.boolean = 1;
return(BOOL_CONST);
}
{TRUETYPEID} {
cool_yylval.symbol = stringtable.add_string(yytext);
return(TYPEID);
}
"^" {
cool_yylval.error_msg = "^";
return ERROR;
}
"_" {
cool_yylval.error_msg = "_";
return ERROR; }
"!" {
cool_yylval.error_msg = "!";
return ERROR;
}
"#" {
cool_yylval.error_msg = "#";
return ERROR;
}
"$" {
cool_yylval.error_msg = "$";
return ERROR;
}
"%" {
cool_yylval.error_msg = "%";
return ERROR;
}
"&" {
cool_yylval.error_msg = "&";
return ERROR;
}
"[" {
cool_yylval.error_msg = "[";
return ERROR;
}
"]" {
cool_yylval.error_msg = "]";
return ERROR;
}
">" {
cool_yylval.error_msg = ">";
return ERROR;
}
"?" {
cool_yylval.error_msg = "?";
return ERROR;
}
"`" {
cool_yylval.error_msg = "`";
return ERROR;
}
"\\" {
cool_yylval.error_msg = "\\";
return ERROR;
}
"|" {
cool_yylval.error_msg = "|";
return ERROR;
}
{FI} {return(FI);}
{IF} {return(IF);}
{IN} {return(IN);}
{INHERITS} {return(INHERITS);}
{LET} {return(LET);}
{LOOP} {return(LOOP);}
{POOL} {return(POOL);}
{THEN} {return(THEN);}
{WHILE} {return(WHILE);}
{CASE} {return(CASE);}
{ESAC} {return(ESAC);}
{OF} {return(OF);}
{NEW} {return(NEW);}
{ISVOID} {return(ISVOID);}
{ASSIGN} {return(ASSIGN);}
{NOT} {return(NOT);}
{CLASS} {return(CLASS);}
bool_const {return(BOOL_CONST);}
error {return(ERROR);}
{OBJECTID} {
cool_yylval.symbol = stringtable.add_string(yytext);
return(OBJECTID);
}
{TYPEID} {
cool_yylval.symbol = stringtable.add_string(yytext);
return(TYPEID);
}
{INVISIBLE_CHARACTERS} {
cool_yylval.error_msg = yytext;
return ERROR;
}
%%
bool max_strlen_check(){
int max_strlen_error(){
BEGIN INITIAL;
cool_yylval.error_msg = "String constant too long";
return (ERROR);
if(comm){
cool_yylval.error_msg = "EOF in comment";
BEGIN INITIAL;
return ERROR;
}
else{
BEGIN INITIAL;
return 0;
}
}