我想做一个词法分析器:
1. 输入为C#源码文件.cs,碰到C#关键字则跳过,遇其他(如方法名、变量名等)则在前后加标签,最后将整个加好标签的代码输出。
2. 比如输入一个C#源码文件. 如 Test.cs 内容如下:
using System;
class Test {
private int testvar = 0;
public Test(int test)
{
this.testvar = test;
}
public static string GetName()
{
return this.testvar.ToString();
}
}
}
----------------------------------------------------
要求输出为:
using System;
class <标签1>Test<标签2> {
private int <标签1>testvar<标签2> = 0;
public <标签1>Test<标签2>(int <标签1>test<标签2>)
{
this.<标签1>testvar<标签2> = <标签1>test<标签2>;
}
public static string <标签1>GetName<标签2>()
{
return this.<标签1>testvar<标签2>.ToString();
}
}
}
3. 所使用技术不限, 但要求C#里能调用。 可以借助现成的引擎实现 (如 Yacc, Lex, Visual Parse++ 等)