Ad Hoc Scanner.

Page 1 of 1

4 Replies - 1652 Views - Last Post: 04 October 2012 - 08:44 PM Rate Topic: -----

#1 eplymale3043  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 01-October 09

Ad Hoc Scanner.

Posted 08 October 2010 - 07:14 PM

I'm writing an ad hoc scanner for one of my classes, and I seem to getting a really weird error.

This program seems to work fine for the example, but when it hits the left parenthesis it freaks out.

//Extra Credit remove this comment.
read A1
read A2
sum:=(A1-33)+A2*5.5
write sum
write (A1+A2)/2



#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 << "checkForOper" << 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.

#2 eplymale3043  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 01-October 09

Re: Ad Hoc Scanner.

Posted 08 October 2010 - 08:41 PM

figured it out, thanks though :)
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

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

Re: Ad Hoc Scanner.

Posted 09 October 2010 - 04:50 AM

In the future, please be aware that "freaks out" is not a good description of a problem when seeking help. We need exact behavior and errors to be helpful.
Was This Post Helpful? 0
  • +
  • -

#4 yotic  Icon User is offline

  • D.I.C Head

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

Re: Ad Hoc Scanner.

Posted 04 October 2012 - 08:42 PM

View Posteplymale3043, on 08 October 2010 - 08:41 PM, said:

figured it out, thanks though :)


what did you do to fix the error?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: Ad Hoc Scanner.

Posted 04 October 2012 - 08:44 PM

After two years, the OP probably isn't coming back. Please avoid necroposting. Topic closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1