用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