-
Notifications
You must be signed in to change notification settings - Fork 2
/
lex.l
114 lines (96 loc) Β· 5.89 KB
/
lex.l
1
2
3
4
5
6
7
8
9
10
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
%{
#include <bits/stdc++.h>
using namespace std;
#include <stdio.h>
#include<string.h>
#include<errno.h>
//#include "src/SymbolTable.h"
#include "y.tab.h" /* token codes from the parser */
int yylex();
void yyerror (const char* s);
extern int errno;
int yycolno = 0;
int prevleng = 0;
#define YY_USER_ACTION {yylloc.first_line = yylloc.last_line = yylineno; yylloc.last_column = yycolno;}
%}
%option nounput yylineno
UNICODE_LETTER [a-zA-Z]
LETTER [a-zA-Z_]
DIGIT [0-9]
UNICODE_CHAR [^\n]
NEW_LINE [\n]
VAR_TYPE "bool"|"error"|"float32"|"float64"|"int"|"string"
BOOL_CONST "true"|"false"
NIL_VAL "nil"
WHITESPACE [\t\r\f\v]+
%%
"fmt.Println" {yylval.sval= strdup(yytext); yycolno+=prevleng; prevleng=yyleng; return T_PRINT; }
"=" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_ASSIGN;}
"+" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_ADD;}
"-" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_MINUS;}
"*" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_MULTIPLY;}
"/" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_DIVIDE;}
"%" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_MOD;}
"&&" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_LAND;}
"||" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_LOR;}
"++" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_INCREMENT;}
"--" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_DECREMENT;}
"==" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_EQL;}
">" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_GTR;}
"<" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_LSR;}
"!" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_NOT;}
"!=" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_NEQ;}
"<=" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_LEQ;}
">=" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_GEQ;}
"(" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_LEFTPARANTHESES;}
")" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_RIGHTPARANTHESES;}
"{" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_LEFTBRACE;}
"}" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_RIGHTBRACE;}
"[" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_LEFTBRACKET;}
"]" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_RIGHTBRACKET;}
"," {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_COMMA;}
";" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_SEMICOLON;}
"." {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_PERIOD;}
"package" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_PACKAGE;}
"import" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_IMPORT;}
"func" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_FUNC;}
"break" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_BREAK;}
"const" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_CONST;}
"continue" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_CONTINUE;}
"else" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_ELSE;}
"for" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_FOR;}
"if" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_IF;}
"return" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_RETURN;}
"var" {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_VAR;}
{VAR_TYPE} {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_VAR_TYPE;}
{BOOL_CONST} {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_BOOL_CONST;}
{NIL_VAL} {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_NIL_VAL;}
{LETTER}({LETTER}|{DIGIT})* {
if(strlen(yytext)<=32)
{yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_IDENTIFIER;}
else
{
printf ("\033[0;31m Line:%d | Identifier too long,must be between 1 to 32 characters \n\033[0m\n",yylineno);
char newid[32];
strncpy(newid,yytext,32);
printf ("\033[0;36m Error handled , Identifier name %s replaced by %s \n\033[0m\n",yytext,newid);
yylval.sval= strdup(newid);yycolno+=prevleng; prevleng=strlen(newid);return T_IDENTIFIER;
}
}
{DIGIT}+({LETTER}|_)+ {yycolno+=prevleng; prevleng=yyleng; yyerror("Lexical Error : Illegal identifier format\n");}
{DIGIT}+ {yylval.sval= strdup(yytext);yycolno+=prevleng; prevleng=yyleng;return T_INTEGER;}
{DIGIT}+"."{DIGIT}+ { yylval.sval= strdup(yytext); yycolno+=prevleng; prevleng=yyleng;return T_FLOAT;}
\"([^\"\n\"]|(\\.))*$ { yycolno+=prevleng; prevleng=yyleng; yyerror("Lexical Error : Badly Terminated String\n");}
\"([^\\\n\"]|(\\.))*\" { yycolno+=prevleng; prevleng=yyleng; yylval.sval= strdup(yytext);return T_STRING;}
\/\/{UNICODE_CHAR}*\n {yycolno=0; prevleng=0;}
"\n" {yycolno=0;prevleng=0;}
"\t" {yycolno+=prevleng; prevleng=4;};
" " {yycolno+=prevleng; prevleng=1;}
{WHITESPACE} ;
. {yyerror("Lexical error : Unidentified token");}
%%
int yywrap()
{
printf("Input Exhausted!\n");
return 1;
}