codeslexer.l 3.91 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7 8
%{
#include <stdlib.h>
9 10
#include "src/iokernellang/CodesIOKernelTypes.h"
#include "src/iokernellang/CodesIOKernelParser.h"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

#define YY_NO_INPUT

%}

%option reentrant
%option prefix="CodesIOKernel_"
%option bison-bridge
%option bison-locations
%option noyywrap
%option yylineno
%option nounput

%{
	YYLTYPE *CodesIOKernel_get_lloc  (yyscan_t yyscanner);
	int CodesIOKernel_lex_init (yyscan_t* scanner);
	int CodesIOKernel_lex(YYSTYPE * lvalp, YYLTYPE * llocp, void * scanner);
	YY_BUFFER_STATE CodesIOKernel__scan_string (yyconst char *yy_str ,yyscan_t yyscanner);

	#define YY_EXTRA_TYPE CodesIOKernelContext*
#if 0
	#define YY_USER_ACTION \
        do { \
                yylloc->first_line = yylloc->last_line = 1; \
                yylloc->first_column = yylloc->last_column = 0; \
        } while(0) ;
#else
        #define YY_USER_ACTION /* no user action */;
#endif

%}

%%

[a-z]       {
		if(yyextra->text != NULL)
                {
			free(yyextra->text);
                }
		yyextra->text = strdup(yytext);
                yylval->sIndex = *(yyextra->text) - 'a';
                return VARIABLE;
            }

0           {
		if(yyextra->text != NULL)
                {
			free(yyextra->text);
                }
		yyextra->text = strdup(yytext);
                yylval->iValue = atoi(yyextra->text);
                return INTEGER;
            }

[1-9][0-9]* {
		if(yyextra->text != NULL)
                {
			free(yyextra->text);
                }
		yyextra->text = strdup(yytext);
                yylval->iValue = atoll(yyextra->text);
                return INTEGER;
            }

[-()<>=+*/%,;{}.] {
		if(yyextra->text != NULL)
                {
			free(yyextra->text);
                }
		yyextra->text = strdup(yytext);
                return *(yyextra->text);
             }

">="            return GE;
"<="            return LE;
"=="            return EQ;
"!="            return NE;
"while"         return WHILE;
"if"            return IF;
"else"          return ELSE;
"print"         return PRINT;
"write"         return WRITE;
"writeat"       return WRITEAT;
"write_all"     return WRITE_ALL;
"writeat_all"   return WRITEAT_ALL;
"read"          return READ;
"readat"        return READAT;
"read_all"      return READ_ALL;
"readat_all"    return READAT_ALL;
"sync"          return SYNC;
"delete"        return DELETE;
"sleep"         return SLEEP;
"open"          return OPEN;
"close"         return CLOSE;
"flush"         return FLUSH;
"seek"          return SEEK;
"getgroupid"    return GETGROUPID;
"getnumgroups"  return GETNUMGROUPS;
"getgrouprank"  return GETGROUPRANK;
"getgroupsize"  return GETGROUPSIZE;
"getcurtime"    return GETCURTIME;
"exit"    	return EXIT;

[ \t\n]+        ;       /* ignore whitespace */

.               fprintf(stderr, "Unknown character\n");
%%

void CodesIOKernelScannerInit(CodesIOKernelContext * context)
{
	yylex_init(&context->scanner_);
	yyset_extra(context, context->scanner_);
	context->text = NULL;
	context->lval = malloc(sizeof(YYSTYPE));
	context->locval = CodesIOKernel_get_lloc(*(yyscan_t *)(context->scanner_));

        //((YYLTYPE *)context->locval)->first_line = 1;

	int i = 0;
	for(i = 0 ; i < 26 ; i++)
	{
	    context->sym[i] = 0;
	}

        for(i = 0 ; i < 10 ; i++)
        {
            context->next_var[i] = 0;
        }

        for(i = 0 ; i < 10 ; i++)
        {
            context->var[i] = 0;
        }

        context->inst_ready = 0;
}

void CodesIOKernelScannerDestroy(CodesIOKernelContext * context)
{
	yylex_destroy(context->scanner_);
	free(context->text);
	context->text = NULL;
	context->locval = NULL;
	context->lval = NULL;
}

void CodesIOKernelScannerSetSymTable(CodesIOKernelContext * context)
{
	sym = &(context->sym[0]);
        var = &(context->var[0]);
        inst_ready = &(context->inst_ready);
        group_rank = &(context->GroupRank);
        group_size = &(context->GroupSize);
}