词法分析器 C++实现

本文详细介绍了如何使用C++编程语言实现一个词法分析器,涵盖了词法分析的基本概念、步骤以及关键代码实现,帮助读者理解词法分析在编译原理中的重要性,并提供了一个实际的编程实践案例。
摘要由CSDN通过智能技术生成
#include<bits/stdc++.h>
#include<cstring>
using namespace std;
//保留字表 
static char reserve[32][20] = {
    "auto", "break", "case", "char", "const", "continue",
    "default", "do", "double", "else", "enum", "extern",
    "float", "for", "goto", "if", "int", "long",
    "register", "return", "short", "signed", "sizeof", "static",
    "struct", "switch", "typedef", "union", "unsigned", "void",
    "volatile", "while"
};

//界运算符表 
static char operatorLimit[36][10] = {
    "+", "-", "*", "/", "<", "<=", ">", ">=", "=", "==",
    "!=", ";", "(", ")", "^", ",", "\"", "\'", "#", "&",
    "&&", "|", "||", "%", "~", "<<", ">>", "[", "]", "{",
    "}", "\\", ".", "\?", ":", "!"
};

char IdenOperator[10000][50] = {""};

/********查找保留字*****************/
int searchRev(char reserveWord[][20], char s[])
{
    for (int i = 0; i < 32; i++)
    {
        if (strcmp(reserveWord[i], s) == 0)
        {//若成功查找,则返回种别码
            return i + 1;//返回种别码
        }
    }
    return -1;//否则返回-1,代表查找不成功,即为标识符
}
/*编译预处理*/
void Filter(char r[],int source)
{
	char cmptString[100000];
	int count = 0;
	for(int i = 0 ; i <= sourc
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
词法分析器是编译器的第一个阶段,用于将源代码分解成单个的词语(Token)。以下是一个简单的C++实现: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; // 定义Token类 class Token { public: string type; // 类型 string value; // 值 Token(string type, string value) { this->type = type; this->value = value; } }; // 定义词法分析器类 class Lexer { private: string input; // 输入代码 int position; // 当前位置 char current; // 当前字符 public: Lexer(string input) { this->input = input; position = 0; current = input[position]; } // 获取下一个字符 void advance() { position++; if (position < input.size()) { current = input[position]; } else { current = '\0'; } } // 获取数字 Token get_number() { string number = ""; while (current >= '0' && current <= '9') { number += current; advance(); } return Token("NUMBER", number); } // 获取符号 Token get_symbol() { string symbol = ""; while (current == '+' || current == '-') { symbol += current; advance(); } return Token("SYMBOL", symbol); } // 分析输入代码 vector<Token> analyze() { vector<Token> tokens; while (current != '\0') { if (current >= '0' && current <= '9') { tokens.push_back(get_number()); } else if (current == '+' || current == '-') { tokens.push_back(get_symbol()); } else { advance(); } } return tokens; } }; int main() { string input = "12+34-56"; Lexer lexer(input); vector<Token> tokens = lexer.analyze(); for (int i = 0; i < tokens.size(); i++) { cout << "Token: " << tokens[i].type << ", Value: " << tokens[i].value << endl; } return 0; } ``` 以上代码实现了一个简单的词法分析器,可以将输入的代码分解成数字和符号两种类型的Token,并输出它们的类型和值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值