废话不多说,先上结果
实验环境
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