1. Native JavaScript is not very suitable for document flow operations, so we use jQuery to write a lexical analyzer

$(document).ready(function() {$("#put").click(function(){// Get text data and respond to eventsCopy the code

2. Delete the content in the blank line feed and comment from the text data

 var
text=$('#in').val(); Console. log(text) // Document stream fetch text= text.replace(/(? :^|\n|\r)\s*\/\*[\s\S]*? \*\/\s*(? :\r|\n|$)/g,'\n').replace(/(? :^|\n|\r)\s*\/\/.*(? :\r|\n|$)/g,'\n'); // Delete comments (this regular expression is too dirty!!) // Text = text.replace(/\ +/g,""); Text = text.replace(/[\r\n]/g,""); Text = text.replace(/\s*/g,' '); Var text = text.split(' ');

     


   
text=text.toString().replace(/,/g,""); // Remove the output comma $('#scanf').text(text)

Copy the code

4. Match different species

 var reg1 = RegExp(/include/);
      var reg2 = RegExp(/int/);
      var reg3 = RegExp(/main/);
      var reg4 = RegExp(/printf/);
     var reg5 = RegExp(/char/);
     var reg6 = RegExp(/union/);
    for(var i=1; i<=6; i++){ switch(i) {case 1:
  if(reg1.exec(text)){
          $('#include').text(reg1)
       }
  break;
case 2:
   if(reg2.exec(text)){
          $('#int').text(reg2)
       }
    break;
case 3:
   if(reg3.exec(text)){
          $('#main').text(reg3)
       }
  break;
case 4:
   if(reg4.exec(text)){
          $('#printf').text(reg4)
       }
    break;
 case 5:
   if(reg5.exec(text)){
          $('#char').text(reg5)
       }
break;
case 6:
   if(reg6.exec(text)){
          $('#union').text(reg6)
       }
    break;
   
default:
  
}}Copy the code

5. Program operation effect

Source code address: github.com/tanxiaofeng…