编译原理练习一(处理c源码)

题目描述:编写一个程序,将C源码中的所有注释字母大写,注释之外的所有保留字大写,所有标识符小写

我使用的是js编程,先画出有限状态机的转换,再用双重switch,case处理自动转换机的状态,(双重循环嵌套的switch,case是

解决此类问题的一个模式,虽不是代码最简,但保证了简单清晰的结构)

首先是状态转换图:

编译原理练习一(处理c源码)_第1张图片

按照状态图,写出代码如下:

const fs = require("fs");
var data="";
const redline = require("readline");
data = fs.readFileSync("test2.txt");
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

经过简单的测试,没有问题,但是肯定不是包含了所有测试样例的。

先就这样啦。


你可能感兴趣的:(编译原理)