lex词法分析

注重版权,若要转载烦请附上作者和链接

作者:Joshua_yi

链接:https://blog.csdn.net/weixin_44984664/article/details/109655609


一、代码

%option noyywrap
%option yylineno
%top{
   #include<map>
   #include<fstream>
   #include<string.h>
   #include<iostream>
   #include <iomanip>
   using std::map;	
   std::ifstream input("./testin");
   using std::cout;
}
%{
   map<std::string,int> map_keyword;
   map<std::string,int> map_id;
   int i=0,j=0;
%}

KEYWORD "auto"|"break"|"continue"|"case"|"char"|"const"|"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"|"define"
DIGIT [0-9]
LETTER [a-zA-Z]
ID ({LETTER}|_)({LETTER}|_|{DIGIT})*
NUMBER [1-9]{DIGIT}*|0
OPT "+="|"-="|"*="|"/="|"+"|"-"|"*"|"/"|"<="|">="|"=="|"="|"<"|">"
LEFTPAREN "("
RIGHTPAREN  ")" 
LEFTBRACKET "["
RIGHTBRACKET "]"
LEFTBRACE "{"
RIGHTBRACE "}"
SEMICOLON ";"
COMMA ","
COLON ":"
SQU "'"
LINES "\n"
SPACE " "
linecommentbegin "//"
linecomment_element .
linecomment_end \n
commentbegin "/*"
commentelement .|\n
commentend "*/"
%x COMMENT
%x LINECOMMENT
%%
{KEYWORD} {
   auto it=map_keyword.find(yytext);
   if(it!=map_keyword.end()){
   	cout<<std::setw(20)<<"KEYWORDS"<<std::setw(20)<<yytext<<std::setw(20)<<it->second<<std::setw(20)<<yylineno<<std::endl;
   }
   else{
   	map_keyword[yytext]=i;
   	cout<<std::setw(20)<<"KEYWORDS"<<std::setw(20)<<yytext<<std::setw(20)<<i<<std::setw(20)<<yylineno<<std::endl;	
   	i++;
   }
}
{ID} {
   auto iter=map_id.find(yytext);
   if(iter!=map_id.end()){
   	cout<<std::setw(20)<<"ID"<<std::setw(20)<<yytext<<std::setw(20)<<iter->second<<std::setw(20)<<yylineno<<std::endl;
   }
   else{
   	map_id[yytext]=j;
   	cout<<std::setw(20)<<"ID"<<std::setw(20)<<yytext<<std::setw(20)<<j<<std::setw(20)<<yylineno<<std::endl;
   	j++;
   }
}
{LEFTPAREN} {cout<<std::setw(20)<<"LEFTPAREN"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{RIGHTPAREN} {cout<<std::setw(20)<<"RIGHTPAREN"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{LEFTBRACKET} {cout<<std::setw(20)<<"LEFTBRACKET"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{RIGHTBRACKET} {cout<<std::setw(20)<<"RIGHTBRACKET"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{LEFTBRACE} {cout<<std::setw(20)<<"LEFTBRACE"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{RIGHTBRACE} {cout<<std::setw(20)<<"RIGHTBRACE"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{SEMICOLON} {cout<<std::setw(20)<<"SEMICOLON"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{COMMA}  {cout<<std::setw(20)<<"COMMA"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{COLON}  {cout<<std::setw(20)<<"COLON"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{SQU}  {cout<<std::setw(20)<<"SQU"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{NUMBER} {cout<<std::setw(20)<<"NUMBER"<<std::setw(20)<<yytext<<std::setw(20)<<yytext<<std::setw(20)<<yylineno<<std::endl;}
{OPT} {cout<<std::setw(20)<<"OPT"<<std::setw(20)<<yytext<<std::setw(20)<<' '<<std::setw(20)<<yylineno<<std::endl;}
{LINES} {}
{SPACE} {}
{linecommentbegin} {BEGIN LINECOMMENT;}
<LINECOMMENT>{linecomment_element} {}
<LINECOMMENT>{linecomment_end} {BEGIN INITIAL;}
{commentbegin} {BEGIN COMMENT;}
<COMMENT>{commentelement} {}
<COMMENT>{commentend} {BEGIN INITIAL;}
%%
int main()
{
   yyFlexLexer lexer(&input);
   lexer.yylex();
   return 0;
}

二、makefile

lcc:
	flex -+ sysycc.l
	g++ lex.yy.cc -o lcc.out
	./lcc.out

三、testin

/**I'm a function*/
int f() {
    int a;
    a = 0;
    while(a < 10){
        a *= 2;
        }
    return a;
}
int main(){
    int a;
    a = 0;
    if(a==0) {
        int a;
        a=a+1;
    }
    // Comment line
    return 0;
}

四、结果演示

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Lex词法分析器模拟器 Lex是一个由Douglas Crockford编写的词法分析器生成器,用于将正则表达式转换为C语言代码。Lex词法分析器模拟器是一个工具,用于模拟Lex生成的词法分析器的行为。这个项目旨在帮助学生理解Lex的工作原理,以及如何将正则表达式转换为C语言代码。 **项目介绍**: **目标**:创建一个Lex词法分析器模拟器,用于模拟Lex生成的词法分析器的行为。 **主要任务**: 1. **解析Lex源代码**: - 编写解析器,用于解析Lex源代码文件。 - 将Lex源代码转换为内部数据结构,以便于模拟。 2. **模拟词法分析过程**: - 实现模拟器的核心逻辑。 - 使用模拟器模拟Lex生成的词法分析器的行为。 3. **生成词法单元**: - 生成词法单元序列,模拟Lex生成的词法分析器的输出。 **技术要求**: - 熟悉C编程语言。 - 了解编译原理中的词法分析概念。 - 熟悉正则表达式和有限自动机理论。 **开发工具**: - C编译器,如GCC、Clang或MSVC。 - 代码编辑器或IDE,如Visual Studio、Code::Blocks或Eclipse。 **适合人员**: - 计算机科学或相关领域的学生:此项目能够帮助他们实践编译原理和C编程知识。 - 软件开发者:特别是那些对编译器和解释器如何工作感兴趣的程序员。 - 语言处理领域的研究者:此项目可以作为自然语言处理和编译技术的一个研究起点。 通过实现这样一个模拟器,开发者可以深入理解编译器的工作原理,提高C编程技能,并为进一步学习编译原理和编程语言理论打下基础。此外,这个项目对于希望进入编译器设计、程序分析和代码生成等领域的人来说,是一个很好的实践机会。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Joshua_yi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值