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 7

%option noyywrap

%{
#include <cool-parse.h>
#include <stringtab.h>
#include <utilities.h>
#include <iostream>

/* 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;

//-------------------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;
}

\0\0 { *string_buf_ptr++ = '0';}

\\[^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);
}

"<=" { return LE; }


"<-" { return ASSIGN; }
"<" { return ('<'); }
"@" { return ('@'); }
"~" { return '~'; }
"=" { return '='; }
"." { return '.'; }
"-" { return '-'; }
"," { return ','; }
"+" { return '+'; }
"*" { return '*'; }
"/" { return '/'; }
"}" { return '}'; }
"{" { return '{'; }
"(" { return '('; }
")" { return ')'; }
":" { return ':'; }
";" { return ';'; }

"^" {
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(){

return((string_buf_ptr-string_buf)+1 > MAX_STR_CONST);


}

int max_strlen_error(){

BEGIN INITIAL;
cool_yylval.error_msg = "String constant too long";
return (ERROR);

void open_comment_check(int counter){

if(counter!=0) BEGIN COMMENT;


else BEGIN INITIAL;
}

int eof_in_comment_check(bool comm){

if(comm){
cool_yylval.error_msg = "EOF in comment";
BEGIN INITIAL;
return ERROR;
}
else{
BEGIN INITIAL;
return 0;
}
}

You might also like