-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.c
173 lines (148 loc) · 3.88 KB
/
lex.c
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
165
166
167
168
169
170
171
172
173
#include "lex.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char* yytext = ""; /* Lexeme (not '\0'
terminated) */
int yyleng = 0; /* Lexeme length. */
int yylineno = 0; /* Input line number */
int lex(void){
static char input_buffer[1024];
char *current;
int first_char_is_digit = 0;
int invalid_num= 0;
current = yytext + yyleng; /* Skip current
lexeme */
while(1){ /* Get the next one */
while(!*current ){
/* Get new lines, skipping any leading
* white space on the line,
* until a nonblank line is found.
*/
if(!gets(input_buffer)){
*current = '\0' ;
return EOI;
}
current = input_buffer;
++yylineno;
while(isspace(*current))
++current;
}
// printf("%s\n", current);
for(; *current; ++current){
/* Get the next token */
yytext = current;
yyleng = 1;
switch( *current ){
case ':':
// printf("ASSIGN\n");
return ASSIGN;
case '=':
// printf("EQUALS\n");
return EQUALS;
case '<':
// printf("LT\n");
return LT;
return LT;
case '>':
// printf("GT\n");
return GT;
case ';':
// printf("semi colon\n");
return SEMI;
case '+':
// printf("plus\n");
return PLUS;
case '-':
// printf("minus\n");
return MINUS;
case '*':
// printf("mul\n");
return TIMES;
case '/':
// printf("div\n");
return DIV;
case '(':
// printf("lp\n");
return LP;
case ')':
// printf("rp\n");
return RP;
case '\n':
case '\t':
case ' ' :
break;
default:
if(!isalnum(*current))
fprintf(stderr, "Agvonse la8emevn eisagwgn <%c>\n", *current);
else{
if(isdigit(*current))
first_char_is_digit = 1;
current++;
while(isalnum(*current)){
if(isalpha(*current)){
if(first_char_is_digit && !invalid_num)
invalid_num = 1;
}
++current;
}
yyleng = current - yytext;
if(invalid_num)
fprintf(stderr, "Neither number nor id\n");
else
if(first_char_is_digit)
return NUM;
else
return find_keyword();
}
break;
}
}
}
}
static int Lookahead = -1; /* Lookahead token */
int match(int token){
/* Return true if "token" matches the
current lookahead symbol. */
if(Lookahead == -1)
Lookahead = lex();
return token == Lookahead;
}
void advance(void){
/* Advance the lookahead to the next
input symbol. */
Lookahead = lex();
// printf("%d\n", Lookahead);
}
int find_keyword(){
char *temp;
int i;
temp = (char *) malloc(sizeof(char) * (yyleng+1));
temp[yyleng]='\0';
for(i=0;i<yyleng;i++){
temp[i]=yytext[i];
}
/*Compare for keywords*/
if(!strcmp(temp,"if")){
return IF;
}
else if(!strcmp(temp,"then")){
return THEN;
}
else if(!strcmp(temp,"while")){
return WHILE;
}
else if(!strcmp(temp,"do")){
return DO;
}
else if(!strcmp(temp,"begin")){
return BEGIN;
}
else if(!strcmp(temp,"end")){
return END;
}
else{
return ID;
}
}