特设扫描仪不会工作

时间:2012-10-04 23:10:26

标签: operators adhoc

到目前为止,我有这个代码,假设是一个临时扫描程序,我不确定问题究竟在哪里,因为我在output.txt文件中得到一个非常奇怪的错误。这一切都是在程序命中左括号时开始的,我的checkforoperators函数有什么问题吗?我已经看了,但发现很少,甚至已经问过其他人,但没有回答运气问题。

输入文件包含此

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

并且输出文件是SUPPOSE看起来像这样

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 << "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;
}

0 个答案:

没有答案