实验一 词法分析

 

 

 

//头文件
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include<string.h>
#include<stdio.h>
#include<time.h>

using namespace std;
string instr;//输入符号串
int index;//当前输入符号读入字符的位置
char character;//全局变量字符,存放最新读入的字符
string token;//字符数组,存放已读入的字符序列
map<string, int> Symbol;//标识符集
map<string, int> Digit;//常数集
map<string, int> other;//other
map<string, int>::iterator ite;
const int len = 100;
string Reserve[len];//保留字表
map<string,int> type;
//void init_Reserve()  //构造保留字表的函数
//{
//    Reserve[1] = "begin";
//    Reserve[2] = "end";
//    Reserve[3] = "if";
//    Reserve[4] = "then";
//    Reserve[5] = "while";
//    Reserve[6] = "do";
//    Reserve[7] = "const";
//    Reserve[8] = "var";
//    Reserve[9] = "call";
//    Reserve[10] = "procedure";
//
//}
void getChar()  //读入一个字符
{
    character = instr[index++];
}
void getnbc()  //读入非空白字符
{
    while (character == ' ')
    {
        getChar();
    }
}
void concat()  //连接字符串
{
    token = token + character;
}
bool letter()  //判断是否为字母
{
    if ((character >= 'A'&&character <= 'Z') || (character >= 'a'&&character <= 'z'))
        return true;
    return false;
}
bool digit()  //判断是否为数字
{
    if (character >= '0'&&character <= '9')
        return true;
    return false;
}
void retract() //回退字符的函数
{
//    character = ' ';
    index--;
}
int reserve()  //匹配保留字符
{
    for (int i = 0; i < len; i++)
        if (Reserve[i] == token)
            return i;
    return -1;
}
void symbol()
{
    ite = Symbol.find(token);
    if (ite != Symbol.end())
    {
//        return ite->first;
          return ;
    }
    else
    {
        Symbol[token] = Symbol.size();
        return ;
//        return token;
    }
}
void constant()
{
    ite = Digit.find(token);
    if (ite != Digit.end())
    {
//        return ite->first;
        return ;
    }
    else
    {
        Digit[token] = Digit.size();
         return ;
//        return token;

    }
}
void Three()
{
    ite = other.find(token);
    if (ite != other.end())
    {
        return ;
//      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的探索之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值