编译原理基础实验——c语言实现简单词法分析器(if-else实现)

本文介绍了使用C语言基于if-else结构实现的简单词法分析器,该分析器能处理合法的C语言代码片段,生成token序列。然而,它存在一些限制,如仅支持.txt文件,且文件末尾需为' ',仅能处理绝对路径等。
摘要由CSDN通过智能技术生成

C语言实现简单词法分析器(if-else)

为进一步熟悉编译原理中词法分析的实现过程,采用c语言实现一个简单的针对c语言的词法分析器。此程序只能分析合法c语言代码段并生成token序列,无法进行预处理或错误识别。

已知的问题

  1. 只能识别存储于.txt文件中的代码片段,且文件的最后一个字符必须为‘\n’
  2. 只实现了对文件的绝对路径寻址,以相对路径方式寻址没有找到生成文件

详细代码

//c语言实现简易词法分析程序
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

/*共有如下六类字符表
第一类:标识符(iT) (_ | a~z | A~Z)(_ | a~z | A~Z | 0~9)*
第二类:常数(CT) (1~9)(0~9)*| 0(0~7)* | 0x(0~9 | a~f| A~F)+
第三类:关键字(kT)(32) 独立定义
第四类:界符与运算符(pT) 独立定义
第五类:字符(cT)  '(o_letter | \(s_letter | x(0)*(digit | 空)(digit | 空)(digit | 空) | (0~7 | 空)(0~7 | 空)(0~7 | 空)))'
第六类: 字符串(sT)    "(字符 | digit)*"
*/

/*——————————————————程序开始——————————————————*/
//全局文件流、token缓存、字符缓存
fstream in, out;
string token;
char tmp;
//关键字表
static char kT[32][20] = {
   
    "auto", "double", "int", "struct", "break", "else",
    "long", "switch", "case", "enum", "register", "typedef",
    "char", "extern", "return", "union", "const", "float",
    "short", "unsigned", "continue", "for", "signed", "void",
    "defualt", "goto", "sizeof", "volatile", "do", "while",
    "static", "if"
};
//界符运算符表
static char pT[43][10] = {
   
    "+", "+=", "++", "-", "-=", "--", "*", "*=", "/", "/=",
    "<", "<=", ">", ">=", "=", "==", "!", "!=", "&", "&&",
    "|", "||", "%", "%=", "<<", ">>", "->", "[", "]", "{",
    "}", ".", "\?", ":", "{", "}", ";", "(", ")", "^",
    ",", "#", "~"
};
vector<string> cT;//字符数组,包含单引号
vector<string> sT;//字符串数组,包含双引号
vector<string> iT;//标识符表
vector<double> CT;//常数表

//判定函数
bool is_atoZ(char ch){
   
    if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
        return true;
    else
        return false;
}
bool is_1to9(char ch){
   
    if(ch >= '1' && ch <= '9')
        return true;
    else
        return false;
}
bool is_0to9(char ch){
   
    if(ch >= '0' && ch <= '9')
        return true;
    else
        return false;
}
bool is_1to7(char ch){
   
    if(ch >= '1' && ch <= '7')
        return true;
    else
        return false;
}
bool is_0to7(char ch){
   
    if(ch >= '0' && ch <= '7')
        return true;
    else
        return false;
}
bool is_num_of_0x(char ch){
   
    if((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))
        return true;
    else
        return false;
}
bool is_num_of_0x_nz(char ch){
   
    if((ch >= '1' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))
        return true;
    else
        return false;
}
bool is_none(char ch){
   
    if(ch == ' ' || ch == '\n' || ch == '\t')
        return true;
    else
        return false;
}

//情况判定
int get_case(char c){
   
    if(is_atoZ(c))
        return 2;//转至iT/kT
    else if(is_0to9(c))
        return 5;//转至CT
    else if(is_none(c))
        return 8;//转至空白处理
    else if(c == '_')
        return 1;//转至iT
    else if(c == '/')
        return 3;//转至注释
    else if(c == '0')
        return 4;//转至8/16进制
    else if(c == '\'')
        return 6;//转至cT
    else if(c == '\"')
        return 7;//转至sT
    else
        return 9;//转至pT查表
}

//具体处理
void get_iT(void){
   
    bool flag = true;
    token += tmp;
    while(!in.eof()){
   
        tmp = in.get();
        if(is_atoZ(tmp) || is_0to9(tmp) || tmp == '_')
            token += tmp;
        else
            break;
    }
    in.seekg(-1
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值