-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.cpp
More file actions
121 lines (112 loc) · 3.22 KB
/
scan.cpp
File metadata and controls
121 lines (112 loc) · 3.22 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
/* Simple ad-hoc scanner for the calculator language.
Michael L. Scott, 2008-2017.
*/
#include<iostream>
#include<ios>
#include <cstdlib> //not include this
#include <string>
#include <cctype>
#include "scan.h"
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::noskipws;
char token_image[100];
string image;
std::string getImage(){
return " "+image;
}
token scan() {
static char c = ' ';
/* next available char; extra (int) width accommodates EOF */
int i = 0; /* index into token_image */
/* skip white space */
if (!cin.get(c)) return t_eof;
while (isspace(c)) {
if (!cin.get(c)) return t_eof;
}
cout << c << "\n";
if (isalpha(c)) { //TODO
do {
token_image[i++] = c;
if(!cin.get(c)) break;
//cout << c << endl;
} while (isalpha(c) || isdigit(c) || c == '_');
// cout << "done" << endl;
token_image[i] = '\0';
cout << token_image << endl;
if(token_image==string("read")) return t_read;
else if (token_image==string("write")) return t_write;
else if (token_image==string("do")) return t_do;
else if (token_image==string("od")) return t_od;
else if (token_image==string("if")) return t_if;
else if (token_image==string("fi")) return t_fi;
else if (token_image==string("check")) return t_check;
//else if (token_image!=string(":=")) return t_gets; //not colonequal
else{
image = token_image;
return t_id;
}
// literal = token_image;
}
else if (isdigit(c)) { //TODO
do {
token_image[i++] = c;
cin.get(c);
} while (isdigit(c));
token_image[i] = '\0';
image = token_image;
return t_literal;
}
else switch (c) {
case '+': cin.get(c); return t_add;
case '-': cin.get(c); return t_sub;
case '*': cin.get(c); return t_mul;
case '/': cin.get(c); return t_div;
case '(': cin.get(c); return t_lparen;
case ')': cin.get(c); return t_rparen;
//Relation operators added
case ':':
//cin >> c;
cin.get(c);
// cout << c << endl;
if (c == '='){
//cin >> c;
cin.get(c);
return t_gets;
}
cout << "error\n";
case '<':
//cin >> c;
cin.get(c);
if (c == '>'){
//cin >> c;
cin.get(c);
return t_notequal;
}else if(c == '='){
//cin >> c;
cin.get(c);
return t_smallerequal;
}else return t_smaller;
case '>':
//cin >> c;
cin.get(c);
if(c == '='){
cin.get(c);
return t_greaterequal;
}else return t_greater;
case '=':
//cin >> c;
cin.get(c);
if(c == '='){
cin.get(c);
return t_equal;
}//remove double equal
cout << "error\n";
default:
//printf("error\n");
cout << "error\n";
exit(1);
}
}