• 编译原理实验1_词法成分(标识符)分析


    编译原理实验一
    词法成分(标识符)分析

    一、实验要求

    1. 编写一个函数,功能为对输入字符串进行分析,判断是否满足下面规则:
      第一个字符为字母或下划线,其后为字母或者数字的任意组合。
    2. 编写一个主程序,对要求一的函数进行验证,方法如下:
      用户输入字符串,改程序判断输入字符串是否为标识符。
    3. 编写一个主程序,读入文本文件,判断文本文件中的字符串是否为标识符
      文本文件内容如下:
      main int sum 90 _i2 x234 ufds

    核心代码我是用js做的,页面也比较简单

    image-20210409171049568

    代码:

    <!DOCTYPE html>
    <html lang="zh-CN">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="author" content="Liwker" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <style>
        #is1 {
           200px;
          height: 200px;
          border: 1px #666 solid;
          margin-bottom: 50px;
        }
    
        #out1 {
          height: 10px;
          line-height: 10px;
        }
    
        #is2 {
           400px;
          height: 200px;
          border: 1px #666 solid;
        }
      </style>
    </head>
    
    <body>
      <main>
        <div id="is1">
          <!-- 用户输入框 -->
          <input id="input1" type="text" />
          <!-- 结果框 -->
          <p id="out1"></p>
          <!-- 按钮 -->
          <button id="btn1"> 判定 </button>
        </div>
    
        <div id="is2">
          <!-- 文件输入 -->
          <input id="input2" type="file" accept="text/plain" />
          <!-- 结果框 -->
          <p id="out2"></p>
          <!-- 按钮 -->
        </div>
      </main>
    
      <script type="text/javascript">
        // 判断是否为标识符函数
        function isIdent(str) {
          // 规则:第一个字符为字母或下划线,其后为字母或数字的任意组合(不包含下划线)
          let reg = /^[A-z]{1}[0-9a-zA-Z]+$/;
          return reg.test(str);
        }
    
        // 获取btn1、输入、结果框1
        let btn1 = document.getElementById("btn1");
        let input1 = document.getElementById("input1");
        let out1 = document.getElementById("out1");
        // btn1绑定事件
        btn1.onclick = function () {
          let str = input1.value;
          if (isIdent(str)) {
            out1.innerHTML = str + " 是标识符";
          } else {
            out1.innerHTML = str + " 不是标识符";
          }
        };
    
    
        // 获取输入、结果框2
        let input2 = document.getElementById("input2");
        let out2 = document.getElementById("out2");
        // 读取文本文件
        function getFileContent(fileInput, callback) {
          if (fileInput.files && fileInput.files.length > 0 && fileInput.files[0].size > 0) {
            let file = fileInput.files[0];
            if (window.FileReader) {
              let reader = new FileReader();
              reader.onloadend = function (evt) {
                if (evt.target.readyState == FileReader.DONE) {
                  callback(evt.target.result);
                }
              };
              reader.readAsText(file);
            }
          }
        };
    
        // 处理上传文件
        document.getElementById('input2').onchange = function () {
          getFileContent(this, function (str) {
            let arr = str.split(/s+/);
            let obj = {};
            obj.__proto__.toString = function () {
              let str = "";
              for (s in obj) {
                str += (""" + s + """ + ": " + obj[s] + ", ");
              }
              return "{" + str + "}";
            };
            arr.forEach(function (str) {
              obj[str] = isIdent(str);
            });
            // 输出
            out2.innerHTML = obj;
    
          });
        };
      </script>
    </body>
    
    </html>
    
  • 相关阅读:
    Nodejs in Visual Studio Code 06.新建Module
    Nodejs in Visual Studio Code 05.Swig+Bootstrap
    Nodejs in Visual Studio Code 04.Swig模版
    Nodejs in Visual Studio Code 03.学习Express
    Nodejs in Visual Studio Code 02.学习Nodejs
    Nodejs in Visual Studio Code 01.简单介绍Nodejs
    Visual Studio Code 与 Github 集成
    Windows 10 代理上网用户的正确使用姿势
    Visual Studio创建跨平台移动应用_03.AppBuilder Extension
    Visual Studio创建跨平台移动应用_02.Cordova Extension
  • 原文地址:https://www.cnblogs.com/Liwker/p/14638016.html
Copyright © 2020-2023  润新知