ad hoc scanner issues, unable to determine the problem
Oct 5, 2012 at 2:26am UTC
So far i have this code that is suppose to be an ad hoc scanner, im not sure
where exactly the problem is because i get a very odd error in the output.txt
file. it all starts when the program hits the left parenthesis, is there
somthing wrong with my checkforoperators function? i have looked but have found
little and have even asked others, but no reall luck as to what the problem is.
the input file contains this
1 2 3 4 5
read A1
read A2
sum:=(A1-33)+A2*5.5
write sum
write (A1+A2)/2
the output file should look like this
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
read
A1
read
A2
Sum
:=
(
A1
-
33
)
+
A2
*
5.5
write
sum
write
(
A1
+
A2
)
/
2
actual output.txt
1 2 3 4 5 6 7
read
A1
read
A2
sum
:=
rCommandA1and33CommandmmandA2ommand5....................................................................................................................................................................................................................................................................................................................
main file
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// This program is an ad-hoc scanner.
// It will recognize tokens and seperate them.
// this language will NOT DISTINGUISH between uppercase or lowercase.
void stripSpacing(string &);
void checkForCommands();
void checkForLetters();
void checkForOperators();
void checkForAssignment();
void checkForDecimal();
void checkForDigit();
int x1 = 0;
int x2 = 0;
int vars[10];
// Only for demonstration purposes, we could use a dynamic array for larger purposes.
string holder;
// used to hold the current string before processing.
fstream handling;
// file handling from other libraries.
fstream output;
char temp1[1];
char temp2[1];
int x = 0;
int xtemp = 0;
int main()
{
output.open("output.txt" );
handling.open("input.txt" );
if (handling.is_open())
{
while (!handling.eof())
{
dcom:
getline (handling,holder);
if (holder[x] == '/' && holder[x+1] == '/' )
{
cout << "This line has only comments.... REMOVED :)" << endl;
//do nothing :)
goto dcom;
}
cout << endl;
stripSpacing(holder);
cout << "The line is this long after removing spaces: " << holder.length() << endl;
cout << "The line contains: " << holder << endl;
cout << endl;
while (holder.length() != x)
{
// let's check for commands, such as read write or sum.
checkForOperators();
checkForAssignment();
checkForDecimal();
checkForDigit();
checkForCommands();
checkForLetters();
}
output << "\n" ;
x=0;
}
handling.close();
output.close();
return 0;
}
}
void stripSpacing(string &str)
{
for (int i=0; i<str.length(); i++)
if (str[i]==' ' )
{
str.erase(i,1);
i--;
}
}
void checkForOperators()
{
if (holder[x] == '(' || holder[x] == ')' || holder[x] == '+' || holder[x] == '-' || holder[x] == '*' ||holder[x] == '/' )
{
output << holder[x] + "\n" ;
x++;
}
cout << "checkForOpertors" << endl;
}
void checkForCommands()
{
xtemp = x;
if (holder[x] == 'w' )
{
x++;
if (holder[x] == 'r' )
{
x++;
if (holder[x] == 'i' )
{
x++;
if (holder[x] == 't' )
{
x++;
if (holder[x] == 'e' )
{
x++;
output << "write\n" ; goto stop;
}else {x=xtemp; goto stop;}
}else {x=xtemp; goto stop;}
}else {x=xtemp; goto stop;}
}else {x=xtemp; goto stop;}
}
if (holder[x] == 'r' )
{
x++;
if (holder[x] == 'e' )
{
x++;
if (holder[x] == 'a' )
{
x++;
if (holder[x] == 'd' )
{
x++;
output << "read\n" ; goto stop;
}else {x=xtemp; goto stop;}
}else {x=xtemp; goto stop;}
}else {x=xtemp; goto stop;}
}
if (holder[x] == 's' )
{
x++;
if (holder[x] == 'u' )
{
x++;
if (holder[x] == 'm' )
{
x++;
output << "sum\n" ; goto stop;
}else {x=xtemp; goto stop;}
}else {x=xtemp; goto stop;}
}
stop:
cout << "checkForCommand" << endl;
}
void checkForLetters()
{
if (isalpha(holder[x]))
{
output << holder[x];
x++;
}
cout << "checkForLetters" << endl;
}
void checkForAssignment()
{
if (holder[x] == ':' )
{
x++;
if (holder[x] == '=' )
{
output << ":=\n" ;
x++;
}
else
{
cout << "ERROR!!! NO : BEFORE =" << endl;
}
}
cout << "checkForAssign" << endl;
}
void checkForDecimal()
{
if (holder[x] == '.' )
{
x++;
if (isdigit(holder[x]))
{
output << '.' ;
x--;
}
}
cout << "checkForDeci" << endl;
}
void checkForDigit()
{
if (isdigit(holder[x]))
{
output << holder[x];
x++;
}
cout << "checkForDig" << endl;
}
Last edited on Oct 5, 2012 at 2:27am UTC
Oct 5, 2012 at 4:01am UTC
In checkForDecimal: I don't think you want to do the x--.
In checkForOperators: Change the output line to:
output << holder[x] << "\n" ;
I get:
$ cat output.txt
read
A1
read
A2
sum
:=
(
A1-
33)
+
A2*
5.5
write
sum
write
(
A1+
A2)
/
2
You need to fix the A1-, 33), A2*, A1+ and A2)
Topic archived. No new replies allowed.