用java写的C语言词法分析
来源:互联网 发布:淘宝如何设置单件包邮 编辑:程序博客网 时间:2024/06/11 19:50
- 做编译原理实验,从网上找的一个词法分析的程序,记不得从哪里下载的了,是用java写的C语言的词法分析,自己改了改,共享一下,给需要的人参考一下。有基本关键字的识别,注释行删除,没有浮点数的识别,需要的话可以自己加,应该很简单就加上去了。
- keywords.txt
- 1
- 2
- 3
- / 4
- < 5
- <= 6
- == 7
- != 8
9
= 10
- & 11
- && 12
- || 13
- = 14
- ( 15
- ) 16
- [ 17
- ] 18
- { 19
- } 20
- 21
; 22
, 23
void 24
int 25
float 26
char 27
if 28
else 29
while 30
do 31
! 32
main 33
整数 34
标识符 35
input.txt
void main()
/这是一个c程序/
{
//注释 测试
int sum;
int x;
sum=0;
x=1;
int a;
while( x<=100 && x>=2 )
{
sum=sum+x;
if(x>50) x=x+2;
else x=x+1;
}
}
}
package Complication;import java.io.*;import java.net.Inet4Address;import java.util.*;import java.util.regex.*;public class MyCompiler { static ArrayList<String> keylist = new ArrayList<String>(); public static void main(String[] args) throws FileNotFoundException { try { FileReader keyReader = new FileReader("keywords.txt"); BufferedReader bfKeyReader = new BufferedReader(keyReader); String text = null; while ((text = bfKeyReader.readLine()) != null) { String[] texts = text.split(" "); keylist.add(texts[0]); } // System.out.println(keylist); } catch (Exception e) { e.printStackTrace(); } Pattern pattern = Pattern.compile("\\/\\/[^\\n]*|\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*\\/"); try { FileReader codeReader = new FileReader("input.txt"); BufferedReader bfcodeReader = new BufferedReader(codeReader); String text = null; int line = 0; while ((text = bfcodeReader.readLine()) != null) { Matcher matcher = pattern.matcher(text); String s = matcher.replaceAll(""); line++; System.out.println("行数"+line+":"+s); } } catch (Exception e) { e.printStackTrace(); } try { FileReader codeReader = new FileReader("input.txt"); BufferedReader bfcodeReader = new BufferedReader(codeReader); String text = null; int line = 0; while ((text = bfcodeReader.readLine()) != null) { Matcher matcher = pattern.matcher(text); String s = matcher.replaceAll(""); line++; //System.out.println("行数"+line+":"+s); Scanner sc = new Scanner(s); while (sc.hasNext()) { String str = addBlank(sc.next()); search(str,line); } } } catch (Exception e) { e.printStackTrace(); } } public static String addBlank(String str) { StringBuffer strbuf = new StringBuffer(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); // System.out.println(c); if (Character.isLetterOrDigit(c) || c == '_') { strbuf.append(c); // System.out.println(strbuf.append(c)); } else if ((c == '<' && str.charAt(i + 1) == '=') || (c == '>' && str.charAt(i + 1) == '=') || (c == '!' && str.charAt(i + 1) == '=') || (c == '=' && str.charAt(i + 1) == '=') || (c == '&' && str.charAt(i + 1) == '&') || (c == '|' && str.charAt(i + 1) == '|')) { strbuf.append(":" + c + str.charAt(i + 1) + ":"); i++; } else strbuf.append(":" + c + ":"); } String string = strbuf.toString(); // System.out.println(string); if (string.charAt(0) == ':') strbuf.deleteCharAt(0); // string=strbuf.toString(); // System.out.println(string); for (int i = 0; i < string.length() - 1; i++) { if (string.charAt(i) == ':' && string.charAt(i + 1) == ':') strbuf.deleteCharAt(i); } return strbuf.toString(); } public static void search(String str,int line) { boolean flag = true; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ':') flag = false; } if (flag) { int index = keylist.indexOf(str); if (index >= 0) System.out.println(str + "\t\t" + (index + 1)); else { boolean bool = isNumeric(str); if (!bool) System.out.println(str + "\t\t" + "34"); else Idenfiy(str,line); //System.out.println(str + "\t\t" + "35"); } } else { String[] s = str.split(":"); for (int i = 0; i < s.length; i++) { int index1 = keylist.indexOf(s[i]); if (index1 >= 0) System.out.println(s[i] + "\t\t" + (index1 + 1)); else { boolean bool = isNumeric(s[i]); if (!bool) System.out.println(s[i] + "\t\t" + "34"); else Idenfiy(s[i],line); //System.out.println(s[i] + "\t\t" + "35"); } } } } public static void Idenfiy(String str,int line) { char c = str.charAt(0); if (Character.isLetter(c) || c == '_') { System.out.println(str + "\t\t" + "35"); } else { System.out.println(str + "\t\t" + "非法字符串,行数:"+line); } } public static boolean isNumeric(String str) { for (int i = 0; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) return true; } return false; }}
0 0
- 用java写的C语言词法分析
- 用C语言写的Pascal语言词法分析器
- 一个用C#写的词法分析程序
- 一个用C#写的词法分析程序
- 一个用C#写的词法分析程序
- c语言 词法分析
- Python实现的C语言词法分析
- C语言,词法分析的“贪心法”
- C++写的一个简单的词法分析器(分析C语言)
- C++写的一个简单的词法分析器(分析C语言)
- Java编写的C语言词法分析器
- 用c写的词法分析器
- UCC--C语言词法分析
- C语言实现词法分析
- 大一结束时候写的课程设计:C词法分析
- java写的词法分析器
- java写的词法分析器
- Micro语言的词法分析程序 Java代码
- .NET事务时间限制
- 分享到QQ空间的icon的坑
- C#接口
- MYSQL使用group by时,查询结果的总记录数
- 80后的互联网北漂:你的未来在哪?
- 用java写的C语言词法分析
- 挑战面试编程:大整数的加、减、乘、除
- Android 编程下通过 Theme 和 Style 避免 APP 启动闪黑屏
- 编程之美 之 让CPU占用率听你指挥
- jsp页面的ajax取值
- Android 避免APP启动闪黑屏(Theme和Style)
- 前端菜鸟的成长学习之路(一)
- YGM欧版磨粉机微粉磨生产线
- 数据持久化(一)-----归档 读写 文件路径