-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlexer.l
164 lines (143 loc) · 3.2 KB
/
lexer.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
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
%{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "header.h"
#include "y.tab.h"
int id_install(char *type, char *name, int yylineno, int scope);
void key_install(char *name, int yylineno);
void multicomment();
int temp;
%}
%option yylineno
%%
"<?php" {
fprintf(stdout, "%s", yytext);
return START;
}
"?>" {
fprintf(stdout, "%s", yytext);
return END;
}
"$"[a-zA-Z_]+[0-9a-zA-Z_]* { // regex to match identifier
fprintf(stdout, "%s", yytext);
yylval.sIndex = id_install("int\0", yytext, yylineno, scope); // create symbol table entry
return VARIABLE; // return token
}
0 {
fprintf(stdout, "%s", yytext);
yylval.iValue = atoi(yytext);
return INTEGER; // int const token return hua
}
[1-9][0-9]* { // regex to match int const
fprintf(stdout, "%s", yytext);
yylval.iValue = atoi(yytext);
return INTEGER; // int const token return hua
}
[-<>=+*/;.] {
fprintf(stdout, "%s", yytext);
return *yytext;
}
"(" {
++brack_count;
fprintf(stdout, "%s", yytext);
return *yytext;
}
")" {
--brack_count;
fprintf(stdout, "%s", yytext);
return *yytext;
}
"{" {
++scope;
++paren_count;
fprintf(stdout, "%s", yytext);
return *yytext;
}
"}" {
--scope;
--paren_count;
fprintf(stdout, "%s", yytext);
return *yytext;
}
[\t]*"//".* {;}
"/*" {
multicomment(); // function to detect multiline comment
}
">=" { /* tokens for keywors and operators */
fprintf(stdout, "%s", yytext);
return GE;
}
"<=" {
fprintf(stdout, "%s", yytext);
return LE;
}
"==" {
fprintf(stdout, "%s", yytext);
return EQ;
}
"!=" {
fprintf(stdout, "%s", yytext);
return NE;
}
"while" {
fprintf(stdout, "%s", yytext);
key_install(yytext, yylineno);
return WHILE;
}
"if" {
fprintf(stdout, "%s", yytext);
key_install(yytext, yylineno);
return IF;
}
"else" {
fprintf(stdout, "%s", yytext);
key_install(yytext, yylineno);
return ELSE;
}
"print" {
fprintf(stdout, "%s", yytext);
key_install(yytext, yylineno);
return PRINT;
}
[ \t\n]+ {
fprintf(stdout, "%s", yytext);
}; /* ignore whitespace */
. yyerror("unknown character");
%%
int yywrap(void) {
return 1;
}
/* Function to insert entries for identifiers in the symbol table. */
int id_install(char *type, char *name, int yylineno, int scope){
for(int i = 0; i < sym_size; ++i){
if(strcmp(sym_table[i].name, name) == 0)
return i;
}
strcpy(sym_table[sym_size].type, type);
strcpy(sym_table[sym_size].name, name);
sym_table[sym_size].value = 0;
sym_table[sym_size].line_no = yylineno;
sym_table[sym_size].scope = scope;
sym_table[sym_size].storage_req = sizeof(int);
++sym_size;
return sym_size-1;
}
void multicomment()
{
char c, c1, c2;
while ((c = input()) != '*' && c != 0);
c1=input();
if(c=='*' && c1=='/')
{
c=0;
}
if (c != 0)
putchar(c1);
}
/* Function to insert entries of keyword in the symbol table. */
void key_install(char *name, int yylineno){
strcpy(key_table[key_size].name, name);
key_table[key_size].line_no = yylineno;
++key_size;
}