该程序的基本思路是逐个读入字符,根据字符的不同进行分类处理,最终输出正确的词法分析结果。具体实现过程如下:

  1. 定义一个函数,用于判断一个字符是否为字母。
bool isLetter(char c)
{
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
  1. 定义一个函数,用于判断一个字符是否为数字。
bool isDigit(char c)
{
    return c >= '0' && c <= '9';
}
  1. 定义一个函数,用于判断一个字符串是否为关键词。
bool isKeyword(string s)
{
    for (int i = 0; i < keywords.number; i++)
    {
        if (keywords.str[i] == s)
            return true;
    }
    return false;
}
  1. 定义一个函数,用于判断一个字符串是否为运算符。
bool isOperator(string s)
{
    for (int i = 0; i < operators.number; i++)
    {
        if (operators.str[i] == s)
            return true;
    }
    return false;
}
  1. 定义一个函数,用于判断一个字符串是否为分界符。
bool isBoundary(string s)
{
    for (int i = 0; i < boundaries.number; i++)
    {
        if (boundaries.str[i] == s)
            return true;
    }
    return false;
}
  1. 定义一个函数,用于输出词法分析结果。
void printToken(int classNum, int serialNum)
{
    cout << "<" << classNum << ", " << serialNum << ">" << endl;
}
  1. 主函数实现如下:
int main()
{
    char c;
    string s = "";
    while (cin.get(c))
    {
        if (isLetter(c))
        {
            s += c;
            while (cin.get(c))
            {
                if (isLetter(c) || isDigit(c))
                {
                    s += c;
                }
                else
                {
                    cin.unget();
                    break;
                }
            }
            if (isKeyword(s))
            {
                printToken(1, distance(keywords.str, find(keywords.str, keywords.str + keywords.number, s)));
            }
            else
            {
                printToken(4, distance(identifieres.str, find(identifieres.str, identifieres.str + identifieres.number, s)));
                identifieres.str[identifieres.number++] = s;
            }
            s = "";
        }
        else if (isDigit(c))
        {
            s += c;
            while (cin.get(c))
            {
                if (isDigit(c))
                {
                    s += c;
                }
                else
                {
                    cin.unget();
                    break;
                }
            }
            printToken(5, distance(Unsigned_integer.str, find(Unsigned_integer.str, Unsigned_integer.str + Unsigned_integer.number, s)));
            Unsigned_integer.str[Unsigned_integer.number++] = s;
            s = "";
        }
        else if (isOperator(string(1, c)))
        {
            string op(1, c);
            if (c == '+' || c == '*')
            {
                if (cin.get(c))
                {
                    if (c == '=')
                    {
                        op += c;
                    }
                    else
                    {
                        cin.unget();
                    }
                }
            }
            else if (c == '=')
            {
                if (cin.get(c))
                {
                    if (c == '+')
                    {
                        op += c;
                    }
                    else if (c == '=')
                    {
                        op += c;
                    }
                    else
                    {
                        cin.unget();
                    }
                }
            }
            printToken(2, distance(operators.str, find(operators.str, operators.str + operators.number, op)));
        }
        else if (isBoundary(string(1, c)))
        {
            printToken(3, distance(boundaries.str, find(boundaries.str, boundaries.str + boundaries.number, string(1, c))));
        }
    }
    return 0;
}

在主函数中,使用一个循环读入字符,然后根据字符的类型进行分类处理。如果当前字符为字母,则继续读入字符并将其加入到字符串s中,直到读到一个非字母或非数字字符,然后判断s是否为关键词,如果是,则输出词法分析结果;否则,将s保存到标识符数组中,并输出词法分析结果。如果当前字符为数字,则类似地读入数字字符,并输出词法分析结果。如果当前字符为运算符,则需要判断后面是否还有一个字符,如果是,则需要继续读入一个字符,并判断是否组成了一个复合运算符,然后输出词法分析结果。如果当前字符为分界符,则直接输出词法分析结果。

最终的输出结果为一系列的类号和序号组成的词法分析结果

设一语言的关键词、运算符、分界符的个数与单词如下: struct int number; string str10; keywords=3intmainreturn ; 关键词struct int number; string str10; operators =5+=+==; 运算符struct int number; string str10; boundaries =6; ; 分

原文地址: http://www.cveoy.top/t/topic/g1uU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录