-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.cpp
More file actions
166 lines (152 loc) · 4.24 KB
/
lex.cpp
File metadata and controls
166 lines (152 loc) · 4.24 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
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
#include<iostream>
#include"general.h"
#include"lex.h"
using namespace std;
/*****************************************************/
/* lookup - a function to lookup operators and parentheses
and return the token */
int lookup(char ch)
{
switch (ch) {
case '(':
addChar();
nextToken = LEFT_PAREN;
break;
case ')':
addChar();
nextToken = RIGHT_PAREN;
break;
case '+':
addChar();
nextToken = ADD_OP;
break;
case '-':
addChar();
nextToken = SUB_OP;
break;
case '*':
addChar();
nextToken = MULT_OP;
break;
case '/':
addChar();
nextToken = DIV_OP;
break;
case '%':
addChar();
nextToken = MOD_OP;
break;
case '^':
addChar();
nextToken = POW_OP;
break;
case '=':
addChar();
nextToken = ASSIGN_OP;
break;
default:
addChar();
nextToken = EOF;
break;
}
return nextToken;
}
/*****************************************************/
/* addChar - a function to add nextChar to lexeme */
void addChar()
{
if (lexLen <= 98) {
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = '\0';
} else
printf("Error - lexeme is too long \n");
}
/*****************************************************/
/* getChar - a function to get the next character of
input and determine its character class
Two globals are set
nextChar - the next character scanned from the input.
charClass - the category of the character - LETTER, DiGIT, OPERATOR
*/
void getChar()
{
cin.get(nextChar);
if (nextChar != EOF) {
if (isalpha(nextChar))
charClass = LETTER;
else if (isdigit(nextChar))
charClass = DIGIT;
else if (nextChar == '\n')
charClass = NEWLINE_CLASS;
else
charClass = OPERATOR;
} else
charClass = EOF;
}
/*****************************************************/
/* getNonBlank - remove white space characters.
call getChar until it returns a non-whitespace
character.
nextChar will be set to the next non-whitespace char.
*/
void getNonBlank()
{
while (isspace(nextChar) && nextChar != '\n') {
getChar();
}
}
/* lex - a simple lexical analyzer for arithmetic
expressions */
int lex()
{
lexLen = 0;
getNonBlank();
switch (charClass) {
/* Parse identifiers - once you find the first
letter, read and add char by char to lexeme. */
case LETTER:
addChar();
getChar();
/* After first char, you may use either char or digits */
while (charClass == LETTER || charClass == DIGIT) {
addChar();
getChar();
}
/*if(strcmp(lexeme,"quit") == 0) {
exit(11);
}*/
nextToken = IDENT;
break;
/* Parse integer literals - once you find the first
digit, read and add digits to lexeme. */
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT) {
addChar();
getChar();
}
nextToken = INT_LIT;
break;
/* Parentheses and operators */
case OPERATOR:
/* Call lookup to identify the type of operator */
lookup(nextChar);
getChar();
break;
/* Newline characters */
case NEWLINE_CLASS:
addChar();
nextToken = NEWLINE;
break;
/* EOF */
case EOF:
nextToken = EOF;
lexeme[0] = 'E';
lexeme[1] = 'O';
lexeme[2] = 'F';
lexeme[3] = '\0';
break;
} /* End of switch */
return nextToken;
}