编译原理实验一------词法分析程序

实验目的:

设计、编制和调试一个具体的词法分析程序,加深对词法分析的理解。

要求:

(1)通过对PL/0词法分析程序(GETSYS)的分析,编制一个具有以下功能的词法分析程序:

a,输入为字符串(或待进行词法分析的源程序),输出为单词串,即由(单词,类别)所组成的二元组序列;

b.有一定的错误检查能力,例如能发现2a这类不能作为单词的字符串。

  (2)提交设计报告,报告内容包括:

实验目的、要求,算法描述,程序结构,主要变量说明,程序清单,调试情况,设计技巧,心得体会。

实验代码:

#include <string>
#include <iostream>
#include <fstream>
#include <set>
using namespace std;

const int maxLineNum = 100;
set<string> key;             //关键字
char ch;                     //当前读取字符
int col, maxcol;             // col 读取当前行位置,当前行最大长度
ifstream readfile;           //文件输入流
char line[maxLineNum];       //读取文件一行
char identifier[maxLineNum]; //保留读取标识符或者关键字
void init();                 //进行初始化工作
int getsym();                //进行词法分析
int getch();                 //读取一个字符

int main()
{
    cout << "请输入要分析的文件名(请放在该程序同一目录): ";
    string filename = "";
    cin>>filename;
    readfile.open(filename);
    if (readfile.is_open() == false)
    {
        cout << "文件不存在";
        return 0;
    }
    init();
    col = maxcol = 0;
    ch = ' ';
    while (getsym())
        ;
    return 0;
}

void init()
{
    key.insert("const");
    key.insert("do");
    key.insert("while");
    key.insert("if");
    key.insert("else");
    key.insert("for");
    key.insert("int");
    key.insert("char");
    key.insert("return");
    key.insert("void");
    key.insert("read");
    key.insert("write");
}

int getch()
{
    if (col == maxcol)
    {
        if (readfile.peek() == EOF)
        {
            return -1;
        }
        maxcol = col = 0;
        ch = ' ';
        while (ch != '\n')
        {
            if (readfile.peek() == EOF)
            {
                line[maxcol] = 0;
                break;
            }
            readfile.get(ch);
            line[maxcol++] = ch;
        }
    }
    ch = line[col++];
    return 0;
}

int getsym()
{
    int i, j, k, num;
    while (ch == ' ' || ch == 10 || ch == 13 || ch == 9)
    {
        if (getch() == -1)
            return 0;
    }
    if (isalpha(ch))
    {
        k = 0;
        while (isalpha(ch) || isdigit(ch))
        {
            identifier[k] = ch;
            k++;
            if (getch() == -1)
                return 0;
        }
        identifier[k] = 0;
        if (key.find(string(identifier)) == key.end())
        {
            cout << "标识符 (3 , '" << identifier << "')\n";
        }
        else
        {
            cout << "关键字 (1 , '" << identifier << "')\n";
        }
    }
    else
    {
        if (isdigit(ch))
        {
            k = num = 0;
            while (isdigit(ch))
            {
                num = num * 10 + ch - '0';
                k++;
                if (getch() == -1)
                    return 0;
            }
            cout << "整数 (2 , '" << num << "')\n";
        }
        else
        {
            if (ch == '<')
            {
                if (getch() == -1)
                    return 0;
                if (ch == '=')
                {
                    cout << "运算符 (4 , '<=')\n";
                    if (getch() == -1)
                        return 0;
                }
                else
                {
                    cout << "运算符 (4 , '<')\n";
                }
            }
            else if (ch == '>')
            {
                if (getch() == -1)
                    return 0;
                if (ch == '=')
                {
                    cout << "运算符 (4 , '>=')\n";
                    if (getch() == -1)
                        return 0;
                }
                else
                {
                    cout << "运算符 (4 , '>')\n";
                }
            }
            else
            {
                if (ch == '+')
                {
                    cout << "运算符 (4 , '+')\n";
                }
                else if (ch == '-')
                {
                    cout << "运算符 (4 , '-')\n";
                }
                else if (ch == '*')
                {
                    cout << "运算符 (4 , '*')\n";
                }
                else if (ch == '/')
                {
                    cout << "运算符 (4 , '/')\n";
                }
                else if (ch == '=')
                {
                    cout << "运算符 (4 , '=')\n";
                }
                else if (ch == '(')
                {
                    cout << "运算符 (4 , '(')\n";
                }
                else if (ch == ')')
                {
                    cout << "运算符 (4 , ')')\n";
                }
                else if (ch == '.')
                {
                    cout << "运算符 (4 , '.')\n";
                }
                else if (ch == ',')
                {
                    cout << "分界符 (5 , ',')\n";
                }
                else if (ch == ';')
                {
                    cout << "分界符 (5 , ';')\n";
                }
                else if (ch == '{')
                {
                    cout << "分界符 (5 , '{')\n";
                }
                else if (ch == '}')
                {
                    cout << "分界符 (5 , '}')\n";
                }
                else
                {
                    cout << ch << " error!请检查代码是否拼写正确!";
                    return 0;
                }
                if (getch() == -1)
                    return 0;
            }
        }
    }
    return 1;
}

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值