编译原理,做一个简易词法分析器(C)

废话不多说,先上结果

无描述
在这里插入图片描述

实验环境

VS code、Google Chrome

设计概要

提前建立关键字表,输入一段C语言文本;
经过词法分析器分析,要输出识别出经其分析的关键字、标识符、常数、算符、特殊字符。同时,建立标识符表和常数表;
除此之外,还要对无效字符进行过滤(块级注释、行级注释),并且能识别出错误的标识符(实现错误定位,找出原程序中所有词法错误)

单词分类表

关键字:

'int', 'auto', 'double', 'struct', 'break', 'else', 'long', 'switch',
'case', 'enum', 'register', 'typedef', 'char', 'extern', 'return', 
'union', 'const', 'float', 'short', 'unsigned', 'continue', 'for', 
'void', 'default', 'goto', 'sizeof', 'do', 'if', 'while', 'static'

特殊符号:

  + - * / ! # % ^ = > < ? ( ) { } [ ] | | & == += -= , : ; “ ~ <= >=

代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            *{
     
                padding: 0;
                margin: 0;
            }
            input{
     
                width: 170px;
                height: 35px;
                line-height: 35px;
                opacity: 0;
                position: absolute;
                left: 10px;
                top: 5px;
            }
            .btn{
     
                width: 170px;
                height: 24px;
                padding: 10px;
                background-color: rgb(255, 212, 219);
                margin-top: 10px;
                margin-left: 10px;
                text-align: center;
                border-radius: 12px;
                position: relative;
                box-shadow: 0px 0px 8px -4px black;
                transition: all .5s;
                opacity: 0.8;
            }
            .btn:hover{
     
                transform: scale(0.9, 0.9);  
                opacity: 0.95;
            }
            #output{
     
                width: 500px;
                height: 500px;
                margin-top: 10px;
                margin-left: 10px;
                border-radius: 20px;
                border:0.5px solid gray;
                padding: 10px;
                font-family: Microsoft YaHei;
            }
        </style>
    </head>
    
    <body>
        <div class="btn">
            点击此处选择文件
            <input type='file' accept='text/plain' onchange='openFile(event)'><br></div>
        <div id='output'>
            <pre></pre>
        </div>

        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
        <script type="text/javascript">
            function getXiaBiao(n, arr){
     
                for(var k = 0; k < arr.length; k
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值