# 编译原理练习一（处理c源码）

const fs = require("fs");
var data="";
const redline = require("readline");
var tem = data.toString();
var keys = ["auto","break","case","switch","char","const","continue","default","do","while",
"double","else","if","enum","extern","float","for","goto","int","long","register","return","short",
"signed","sizeof","static","struct","typedef","unino","unsigned","void","volatile"]; //关键字的定义
var charArray = "qwertyuiopasdfghjklzxcvbnm"
+"QWERTYUIOPASDFGHJKLZXCVBNM";
var digitArray = [0,1,2,3,4,5,6,7,8,9];
//对每一行的数据进行判断
//用循环case处理
var t1 = judge(tem);
//console.log(t1);
fs.writeFileSync("test3.txt",t1.join(""));
console.log("写入完成");

function judge(str) {
var m=0;
var n=0;
var tokens=[];
for (m=0;m<str.length;) {
var  state=1;
while((state===1||state===2||state===3||state===4||state===6||state===9)
&& (m<str.length)){
switch (state){
case 1:
if(str.charAt(m) === '/'){
n=m;
m=m+1;
state=2;
break;
}else if ((charArray.indexOf(str.charAt(m))!==-1)||(str.charAt(m) === '_')){
n=m;
m=m+1;
state=9;
break;
} else{
n=m;
m=m+1;
state=11;
break;
}
case 2:
if(str.charAt(m) === '/'){
m=m+1;
state=6;
break;
}else if (str.charAt(m) === '*'){
m=m+1;
state=3;
break;
} else{
m=m+1;
state=8;
break;
}
case 3:
if(str.charAt(m) === '*'){
m=m+1;
state=4;
break;
}else{
m=m+1;
state=3;
break;
}
case 4:
if(str.charAt(m) === '*'){
m=m+1;
state=4;
break;
}else if (str.charAt(m) === '/'){
m=m+3;
state=5;
break;
} else{
m=m+1;
state = 3;
break;
}

case 6:
if(str.charAt(m) === '\r'){
m=m+2;
state=7;
break;
}else{
m=m+1;
state = 6;
break;
}
case 9:
if((str.charAt(m) === '_')||(charArray.indexOf(str.charAt(m))!==-1)||(digitArray.indexOf(str.charAt(m))!==-1)){
m=m+1;
state=9;
break;
} else{
m=m+1;
state = 10;
break;
}
}
}
switch (state){
case 5:
state=1; //块注释
var tem1=[];
tem1 =str.slice(n,m).split("");
for(var mm=0;mm<tem1.length;mm++){
if(charArray.indexOf(tem1[mm])!==-1){
tem1[mm] = tem1[mm].toUpperCase();
}
}
tokens.push(tem1.join(""));
break;
case 8:   //其他字符
state = 1;
break;
case 11:  //其他字符
state =1;
tokens.push(str.slice(n,m));
break;
case 7:  //行注释
state = 1;
var tem2=[];
tem2 =str.slice(n,m).split("");
for(var mm2=0;mm2<tem2.length;mm2++){
if(charArray.indexOf(tem2[mm2])!==-1){
tem2[mm2]= tem2[mm2].toUpperCase();
}
}
tokens.push(tem2.join(""));
break;
case 10:  //标识符或关键字
m=m-1;
state = 1;
var tem3=str.slice(n,m);
if(keys.indexOf(tem3.toLowerCase())!==-1){
tem3=tem3.toUpperCase();
}else {
tem3= tem3.toLowerCase();
}
tokens.push(tem3);
break;
}
}
// n保存的是m的初值，也就是token的第一个指针指向的字符，m保存的是token的末尾的索引值
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120