-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.l
124 lines (105 loc) · 6.36 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
%{
// include the y.tab.h file. This file is automatically generated by the compiler from the grammar rules in the .y file.
#include "y.tab.h"
// a C function that is called by the parser whenever it encounters a syntax error.
void yyerror (char *s);
// called by the parser whenever it needs a token. This is where the lexical analyzer is called.
int yylex();
extern int line=1;
#include <stdio.h>
#include <stdarg.h>
#define SHOW_LOGS 1
void Log(const char* type, const char* value)
{
if(SHOW_LOGS)
{
if (type == "int value")
{
printf("Lex(%d) %s: %d\n", line, type, atoi(value));
// printf("\033[1;30mLex(%d) %s: %d\033[0m\n", line, type, atoi(value));
}
else if (type == "float value")
{
printf("Lex(%d) %s: %f\n", line, type, atof(value));
// printf("\033[1;30mLex(%d) %s: %f\033[0m\n", line, type, atof(value));
}
else{
printf("Lex(%d) %s: %s\n", line, type, value);
// printf("\033[1;30mLex(%d) %s: %s\033[0m\n", line, type, value);
}
}
}
%}
/* RegEx */
/*====================*/
DIGIT [0-9]
ALPHABET [a-zA-Z]
ALPHANUM [0-9a-zA-Z]
SPACE [ \r\t]
NEW_LINE \n
INLINE_COMMENT \/\/.*
/* TODO: MULTILINE_COMMENT is not working */
MULTILINE_COMMENT \/\*.*\*\/
arithmeticOps [/+*%-]
bitwiseOps [&^~|]
endOfStatement [;]
punctuators [()={}:,]
TRUE [tT]rue | 1 | [yY]es
FALSE [fF]alse | 0 | [nN]o
/* a directive that tells the compiler that the code below is the lexical analyzer. */
/* The lexical analyzer is built by specifying regular expressions for each token. */
/* first store the value of the token, and the second return its type */
/* e.g.: If you saw the string "print", return the token "print" */
%%
"print" {Log("command", yytext); return PRINT;} /*print Command*/
"assert" {Log("command", yytext); return ASSERT;} /*assert Command*/
"exit" {Log("command", yytext); return EXIT;} /*exit Command*/
"const" {Log("data modifier", yytext); return CONST;} /*const Command*/
"int" {Log("data type", yytext);return INT_DATA_TYPE;} /*int Command*/
"float" {Log("data type", yytext);return FLOAT_DATA_TYPE;} /*float Command*/
"string" {Log("data type", yytext);return STRING_DATA_TYPE;} /*string Command*/
"bool" {Log("data type", yytext);return BOOL_DATA_TYPE;} /*bool Command*/
"void" {Log("data type", yytext);return VOID_DATA_TYPE;} /*void Command*/
"if" {Log("control flow", yytext);return IF;} /*if Command*/
"else" {Log("control flow", yytext);return ELSE;} /*else Command*/
"switch" {Log("control flow", yytext);return SWITCH;} /*switch Command*/
"case" {Log("control flow", yytext);return CASE;} /*case Command*/
"default" {Log("control flow", yytext);return DEFAULT;} /*default Command*/
"while" {Log("control flow", yytext);return WHILE;} /*while Command*/
"for" {Log("control flow", yytext);return FOR;} /*for Command*/
"repeat" {Log("control flow", yytext);return REPEAT;} /*repeat Command*/
"until" {Log("control flow", yytext);return UNTIL;} /*until Command*/
"break" {Log("control flow", yytext);return BREAK;} /*break Command*/
"continue" {Log("control flow", yytext);return CONTINUE;} /*continue Command*/
"return" {Log("control flow", yytext);return RETURN;} /*return Command*/
"enum" {Log("enum", yytext);return ENUM;} /*enum Command*/
"<<" {Log("bitwise operator", yytext);return SHL;} /*SHL Command*/
">>" {Log("bitwise operator", yytext);return SHR;} /*SHR Command*/
{bitwiseOps} {Log("bitwise operator", yytext);return yytext[0];} /*Return the character*/
"==" {Log("comparetor operator", yytext);return EQ;} /*EQ Command*/
"!=" {Log("comparetor operator", yytext);return NEQ;} /*NEQ Command*/
"<=" {Log("comparetor operator", yytext);return LEQ;} /*LEQ Command*/
">=" {Log("comparetor operator", yytext);return GEQ;} /*GEQ Command*/
"<" {Log("comparetor operator", yytext);return LT;} /*LT Command*/
">" {Log("comparetor operator", yytext);return GT;} /*GT Command*/
"&&" {Log("logical operator", yytext);return AND;} /*and Command*/
"||" {Log("logical operator", yytext);return OR;} /*or Command*/
"!" {Log("logical operator", yytext);return NOT;} /*not Command*/
{arithmeticOps} {Log("arithmtic operator", yytext);return yytext[0];} /*Return the character*/
{punctuators} {Log("punctuators", yytext);return yytext[0];} /*Return the character*/
{endOfStatement} {Log("end Of Statement", "; \n");return yytext[0];} /*Return the character*/
"true" {Log("value", "true"); yylval.TYPE_BOOL=1; return TRUE_VAL;} /*true Command*/
"false" {Log("value", "false"); yylval.TYPE_BOOL=0; return FALSE_VAL;} /*false Command*/
[a-zA-Z_][a-zA-Z0-9_]* {Log("identifier", yytext); yylval.TYPE_INT = yytext[0]; return IDENTIFIER;}
{DIGIT}+ {Log("int value", yytext);yylval.TYPE_INT = atoi(yytext); return NUMBER;}
{DIGIT}*"."{DIGIT}+ {Log("float value", yytext);yylval.TYPE_FLOAT = atof(yytext); return FLOAT_NUMBER;}
\"[^\"]*\" {Log("string value", yytext);yylval.TYPE_STR = strdup(yytext); return STRING; }
{NEW_LINE} line++; /*Ignore New Lines*/
{SPACE} ; /*Ignore White SPACE*/
{INLINE_COMMENT} {Log("inline comment", yytext);} /*Ignore Comments*/
{MULTILINE_COMMENT} {Log("multiline comment", yytext);} /*Ignore Comments*/
. {Log("unexpected token", yytext); ECHO; yyerror ("unexpected token");} /*Else, Error*/
%%
// C code
// The wrapper function yywrap()
int yywrap (void) {return 1;}