6 Replies - 2459 Views - Last Post: 05 October 2012 - 06:55 AM Rate Topic: -----

#1 yotic  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 50
  • Joined: 11-October 11

Ad hoc scanner problem

Posted 27 September 2012 - 10:23 AM

I need help on this scanner for a class, i built most of it using code i found online, i will not take credit for something i did not make, BUT i want to understand it, and i would like to know how to fix it because it doesnt completely work. I have been over the code many times following the operation of each function and i cant really seem to find whats wrong with the code, please dont tell me out right, try to help me understand where the problem would be so i could focus more on that point.

input.txt
read A1
read A2
sum:=(A1-33)+A2*5.5
write sum
write (A1+A2)/2



expected output.txt
read
A1
read
A2
Sum
:=
(
A1
-
33
)
+
A2
*
5.5
write
sum
write
(
A1
+
A2
)
/
2



actual output.txt
read
A1
read
A2
sum
:=
rCommandA1and33CommandmmandA2ommand5....................................................................................................................................................................................................................................................................................................................



Main.cpp
#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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Ad hoc scanner problem

#2 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 86
  • View blog
  • Posts: 189
  • Joined: 23-September 12

Re: Ad hoc scanner problem

Posted 27 September 2012 - 11:06 AM

Hello: I'm still looking over this, but one thing I noticed is that stripSpacing removes all spaces--not just those at the beginning and end of the line, which means you'll be trying to scan things like readA1 and writesum. It's probably better to have some function that eats arbitrary runs of spaces within a line which can be called after every token.

Also, if the output doesn't require that you distinguish between a keyword and a variable (that is, you are simply printing them out), you could probably just have a function that gets things of the form [a-zA-Z][a-zA-Z0-9]*. Even if you do have to eventually distinguish between ID tokens and keyword tokens, you can still use a function like this, and then compare the string afterwards to see if it's in the keyword list.

One other thing: since you don't need to backtrack, you could replace the string/indexing (holder/x) scheme with an std::stringstream that you pass by reference into functions. In my opinion, it's easier than keeping track of two global variables.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 yotic  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 50
  • Joined: 11-October 11

Re: Ad hoc scanner problem

Posted 27 September 2012 - 12:34 PM

View PostNathanMullenax, on 27 September 2012 - 11:06 AM, said:

Hello: I'm still looking over this, but one thing I noticed is that stripSpacing removes all spaces--not just those at the beginning and end of the line, which means you'll be trying to scan things like readA1 and writesum. It's probably better to have some function that eats arbitrary runs of spaces within a line which can be called after every token.

Also, if the output doesn't require that you distinguish between a keyword and a variable (that is, you are simply printing them out), you could probably just have a function that gets things of the form [a-zA-Z][a-zA-Z0-9]*. Even if you do have to eventually distinguish between ID tokens and keyword tokens, you can still use a function like this, and then compare the string afterwards to see if it's in the keyword list.

One other thing: since you don't need to backtrack, you could replace the string/indexing (holder/x) scheme with an std::stringstream that you pass by reference into functions. In my opinion, it's easier than keeping track of two global variables.

Hope this helps.




this is great, it makes things alot clearer, but i do have another question. how do i set up a function that scans for just the empty lines/spaces before and after the inputs?
Was This Post Helpful? 0
  • +
  • -

#4 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 86
  • View blog
  • Posts: 189
  • Joined: 23-September 12

Re: Ad hoc scanner problem

Posted 27 September 2012 - 12:44 PM

View Postyotic, on 27 September 2012 - 12:34 PM, said:

this is great, it makes things alot clearer, but i do have another question. how do i set up a function that scans for just the empty lines/spaces before and after the inputs?


Something like this would work at the top of your while loop. I suppose that would make it 'before each token'.

while( holder[x]==' '
    || holder[x]=='\n'
    || holder[x]=='\r'
    || holder[x]=='\t')
{
    ++x;
}


Was This Post Helpful? 0
  • +
  • -

#5 yotic  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 50
  • Joined: 11-October 11

Re: Ad hoc scanner problem

Posted 04 October 2012 - 12:44 PM

okay i included that into the main file, the while loop i mean, and it did away with the simple null space scanner, which is much slower. However im still getting the same error i got before. i thought i had figured it out but i still seem to be getting the same error as before, "string out of range", i dont understand what im doing wrong?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Ad hoc scanner problem

Posted 05 October 2012 - 02:51 AM

Learn how to use your debugger and step through the code.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Ad hoc scanner problem

Posted 05 October 2012 - 06:55 AM

Post your current code. Along with any error/warning messages you received.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1