compiler.l

mail@pastecode.io avatar
unknown
plain_text
25 days ago
3.9 kB
0
Indexable
Never
/* Definition section */
%{
    #include "compiler_common.h"
    #include "y.tab.h"	/* header file generated by bison */
    #define YY_NO_UNPUT
    #define YY_NO_INPUT

    #define Return(tokenType) yycolumn += yyleng; return tokenType

    extern YYSTYPE yylval;


    int yycolumn;
%}

/* Define regular expression label */
letter [a-zA-Z_]
digit [0-9]
variable {letter}+({letter}|{digit})*
inumber {digit}+
fnumber ({digit}*\.{digit}+)

%option yylineno
%x CMT_CON
%x DSTR_CON
%x STR_CON

/* Rules section */
%%

"/*"                { BEGIN(CMT_CON); }
<CMT_CON>(.|\r?\n)  {;}
<CMT_CON>"*/"       { BEGIN(INITIAL); }
"//".*              {;}

\"                  { BEGIN(DSTR_CON); yylval.s_var = ""; }
<DSTR_CON>\"        { BEGIN(INITIAL); return STR_LIT; }
<DSTR_CON>(\\\"|[^\"])* { yylval.s_var = strdup(yytext); }

'                   { BEGIN(STR_CON); yylval.s_var = ""; }
<STR_CON>'          { BEGIN(INITIAL); return STR_LIT; }
<STR_CON>(\\'|[^'\n])* { yylval.s_var = strdup(yytext); }

">>"        { Return(SHR); }
"<<"        { Return(SHL); }
"&"         { Return(BAN); }
"|"         { Return(BOR); }
"~"         { Return(BNT); }
"^"         { Return(BXO); }

"+"         { Return(ADD); }
"-"         { Return(SUB); }
"*"         { Return(MUL); }
"/"         { Return(DIV); }
"%"         { Return(REM); }
"!"         { Return(NOT); }

">"         { Return(GTR); }
"<"         { Return(LES); }
">="        { Return(GEQ); }
"<="        { Return(LEQ); }
"=="        { Return(EQL); }
"!="        { Return(NEQ); }
"&&"        { Return(LAN); }
"||"        { Return(LOR); }

"="         { Return(VAL_ASSIGN); }
"+="        { Return(ADD_ASSIGN); }
"-="        { Return(SUB_ASSIGN); }
"*="        { Return(MUL_ASSIGN); }
"/="        { Return(DIV_ASSIGN); }
"%="        { Return(REM_ASSIGN); }
">>="       { Return(SHR_ASSIGN); }
"<<="       { Return(SHL_ASSIGN); }
"&="        { Return(BAN_ASSIGN); }
"|="        { Return(BOR_ASSIGN); }
"^="        { Return(BXO_ASSIGN); }
"++"        { Return(INC_ASSIGN); }
"--"        { Return(DEC_ASSIGN); }

"("         { Return ('('); }
")"         { Return (')'); }
"["         { Return ('['); }
"]"         { Return (']'); }
"{"         { Return ('{'); }
"}"         { Return ('}'); }

":"         { Return (':'); }
";"         { Return (';'); }
","         { Return (','); }

"cout"      { Return(COUT); }

"auto"      { yylval.var_type = OBJECT_TYPE_AUTO; Return(VARIABLE_T); }
"void"      { yylval.var_type = OBJECT_TYPE_VOID; Return(VARIABLE_T); }
"char"      { yylval.var_type = OBJECT_TYPE_CHAR; Return(VARIABLE_T); }
"int"       { yylval.var_type = OBJECT_TYPE_INT; Return(VARIABLE_T); }
"long"      { yylval.var_type = OBJECT_TYPE_LONG; Return(VARIABLE_T); }
"float"     { yylval.var_type = OBJECT_TYPE_FLOAT; Return(VARIABLE_T); }
"double"    { yylval.var_type = OBJECT_TYPE_DOUBLE; Return(VARIABLE_T); }
"bool"      { yylval.var_type = OBJECT_TYPE_BOOL; Return(VARIABLE_T); }
"string"    { yylval.var_type = OBJECT_TYPE_STR; Return(VARIABLE_T); }

"if"        { Return(IF); }
"else"      { Return(ELSE); }
"for"       { Return(FOR); }
"while"     { Return(WHILE); }
"return"    { Return(RETURN); }
"break"     { Return(BREAK); }
"continue"  { Return(CONTINUE); }

"true"      { yylval.b_var = true; Return(BOOL_LIT); }
"false"     { yylval.b_var = false; Return(BOOL_LIT); }
{inumber}   { yylval.i_var = atoi(yytext); Return(INT_LIT); }
{fnumber}   { yylval.f_var = atof(yytext); Return(FLOAT_LIT); }
{variable}  { yylval.s_var = strdup(yytext); Return(IDENT); }

[ \t]+      { yycolumn += yyleng; }
\r?\n       { yycolumn = 0; }

<<EOF>>     {
                static int once = 0;
                if (once++) {
                    yyterminate();
                }
            }

%%
/*  C Code section */
int yywrap(void) {
    return 1;
}
Leave a Comment