-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlex.l
More file actions
121 lines (108 loc) · 2.9 KB
/
lex.l
File metadata and controls
121 lines (108 loc) · 2.9 KB
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
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
#define stack_size 100
#define DEBUG 1
int yycolumn = 1;
int startFlag = 1;
#define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno; \
yylloc.first_column = yycolumn; yylloc.last_column = yycolumn + yyleng - 1; \
yycolumn += yyleng;
static int sp=0, stack [stack_size];
static void debug(const char *s){
#ifdef DEBUG
fflush(stdin);
if(startFlag){
printf("------------------------TOKENS--------------------------\n");
startFlag=0;
}
int res= strcmp(s, "NL");
int temp=yylineno;
if(res==0){
printf("\nline: %d ",temp);
printf("NEWLINE ");
fflush(stdout);
}
else{
printf("Token_%s ", s);
}
#endif
}
static void push (int i)
{
if (++sp<stack_size) stack[sp]= i;
else {printf ("error: stack overflow\n"); exit(1);}
}
int pop ()
{
if (sp>-1) return stack[sp--];
else {printf ("error: stack underflow\n"); exit(1);}
}
int top()
{
if(sp>-1) return stack[sp];
else return 1;
}
static int indent_depth(const char *K)
{
int len = strlen(K), i, tab_count=1;
for(i=0; i< len ; i++)
{
if(K[i]=='\t'){
tab_count++;
}
else{
printf("Nope");
break;
}
}
return tab_count;
}
int depth = 1;
int scope_depth = 1;
%}
%option yylineno
whitespace [ ]
Multiline_comment \'\'\'.+\'\'\'
%%
[\t" "]* { }
"print" {debug("Print"); return T_Print;}
"if" {debug("If"); return T_If;}
"in" {debug("In"); return T_In;}
"range" {debug("Range"); return T_Range;}
"for" {debug("For"); return T_For;}
"while" {debug("While"); return T_While;}
"break" {debug("Break"); return T_Break;}
"and" {debug("And"); return T_And;}
"or" {debug("Or"); return T_Or;}
"not" {debug("Not"); return T_Not;}
"elif" {debug("Elif"); return T_Elif;}
"else" {debug("Else"); return T_Else;}
"{" {debug("OB"); scope_depth++; return T_Cln;}
"}" {debug("CB"); scope_depth--; return T_CB;}
">" {debug("GT"); return T_GT;}
"<" {debug("LT"); return T_LT;}
">=" {debug("EGT"); return T_EGT;}
"<=" {debug("ELT"); return T_ELT;}
"==" {debug("EQ"); return T_EQ;}
"!=" {debug("NEQ"); return T_NEQ;}
"True" {debug("True"); return T_True;}
"False" {debug("False"); return T_False;}
"+" {debug("PL"); return T_PL;}
"-" {debug("MN"); return T_MN;}
"*" {debug("ML"); return T_ML;}
"/" {debug("DV"); return T_DV;}
"(" { debug("OP"); return T_OP;}
")" {debug("CP"); return T_CP;}
"[" {debug("OB"); return T_OB;}
"]" {debug("CB"); return T_CB;}
"," {debug("Comma"); return T_Comma;}
"=" {debug("EQL"); return T_EQL;}
-?(([0-9]+)|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {yylval.text = strdup(yytext); debug(yylval.text); return T_Number;}
[_a-zA-Z][_a-zA-Z0-9]* {yylval.text = strdup(yytext); debug(yylval.text); return T_ID;}
"#"([a-z]|[0-9]|[A-Z]|" ")* {}
"\n" {yycolumn=1; debug("NL"); return T_NL;}
<<EOF>> {debug("EOF"); return T_EndOfFile;}
%%