詞法分析java__java

來源:互聯網
上載者:User
詞法分析java

內容:

單詞符號 種別編碼
Main 1
[ 28
Int 2
] 29
Char 3
{ 30
If 4
} 31
Else 5
, 32
For 6
: 33
While 7
; 34
標識符ID 10
> 35
整型常數NUM 20
< 36
= 21
>= 37
+ 22
<= 38
== 39
kong 24
。= 40
& 41
( 26
&& 42
) 27

代碼

package fanshe;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class lexical_analysis {    public int ch_index;// 索引    public int ch; // 字元    public int code;// 狀態代碼    public int[] co_long = new int[] { 35, 36, 37, 38, 39, 40, 41, 42, 43, 21 };// 非單一字元的編碼    public String[] nosignal_Word = new String[] { ">", "<", ">=", "<=", "==", "!=", "&", "&&", "||", "=" };// 包含非單個的字元    public StringBuffer strToken = new StringBuffer();// 存放構成單詞符號的字串    public int[] co = new int[] { 1, 2, 3, 4, 5, 6, 7 };// 保留字的編碼    public String[] retainWord = new String[] { "main", "int", "char", "if", "else", "for", "while" };// 保留字    // 判斷是否是字母    public boolean IsLetter() {        if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)) {            return true;        }        return false;    }    // 判斷是否是數字    public boolean IsDigit() {        if (ch >= 48 && ch <= 57) {            return true;        }        return false;    }    // 判斷是否是空格    public boolean IsBC(int ch) {        if (ch == 32) {            return true;        }        return false;    }    // 判斷是否含有非單字元    public boolean IsNN(int ch) {        if (ch == 33 || ch == 60 || ch == 62 || ch == 61 || ch == 38 || ch == 124) {            return true;        }        return false;    }    // 串連字元    public void Concat(char ch) {        strToken.append(ch);    }    // 判斷是否是保留字或者包含非單一字元    public int keyword() {        for (int i = 0; i < retainWord.length; i++) {            if (strToken.toString().equals(retainWord[i])) {                ch_index = i;                return 1;            }            if (strToken.length() != 0) {                if (strToken.charAt(0) >= '0' && strToken.charAt(0) <= '9') {                    return 3;                }            }            if (strToken.length() == 0) {                return 4;            }        }        for (int i = 0; i < nosignal_Word.length; i++) {            if (strToken.toString().equals(nosignal_Word[i])) {                ch_index = i;                return 5;            }        }        return 2;    }    public void Retract() {        code = keyword();        if (code == 1) {            System.out.println("(" + co[ch_index] + "," + strToken + ")");// 保留字        } else if (code == 2) {            System.out.println("(" + 10 + "," + strToken + ")");// 標識符        } else if (code == 3) {            System.out.println("(" + 20 + "," + strToken + ")");// 整數        } else if (code == 5) {            System.out.println("(" + co_long[ch_index] + "," + strToken + ")");// 包含非單一字元        }        strToken.delete(0, strToken.length());    }    /**     * 讀取檔案並進行詞法分析     */    public void lexical_analysis() {        BufferedReader br;        try {            br = new BufferedReader(new FileReader("C:\\Users\\hp\\Desktop\\tests.txt"));// 讀取檔案            while ((ch = br.read()) != -1) {// 每次讀取一個字幕直到讀取完                if (IsBC(ch) == false) {// 判斷是否為空白格                    if (IsLetter()) {// 判斷是否為字母                        Concat((char) ch);                    } else if (IsDigit()) {// 判斷是否為數字                        Concat((char) ch);                    } else if (IsNN(ch)) {// 判斷是否為包含非單一字元                        Concat((char) ch);                    } else if ((char) ch == '+') {                        Retract();                        System.out.println("(" + 22 + "," + (char) ch + ")");                    } else if ((char) ch == '*') {                        Retract();                        System.out.println("(" + 24 + "," + (char) ch + ")");                    } else if ((char) ch == '-') {                        Retract();                        System.out.println("(" + 23 + "," + (char) ch + ")");                    } else if ((char) ch == '(') {                        Retract();                        System.out.println("(" + 26 + "," + (char) ch + ")");                    } else if ((char) ch == ')') {                        Retract();                        System.out.println("(" + 27 + "," + (char) ch + ")");                    } else if ((char) ch == '[') {                        Retract();                        System.out.println("(" + 28 + "," + (char) ch + ")");                    } else if ((char) ch == ']') {                        Retract();                        System.out.println("(" + 29 + "," + (char) ch + ")");                    } else if ((char) ch == '{') {                        Retract();                        System.out.println("(" + 30 + "," + (char) ch + ")");                    } else if ((char) ch == '}') {                        Retract();                        System.out.println("(" + 31 + "," + (char) ch + ")");                    } else if ((char) ch == ',') {                        Retract();                        System.out.println("(" + 32 + "," + (char) ch + ")");                    } else if ((char) ch == ':') {                        Retract();                        System.out.println("(" + 33 + "," + (char) ch + ")");                    } else if ((char) ch == ';') {                        Retract();                        System.out.println("(" + 34 + "," + (char) ch + ")");                    }                } else {                    Retract();                }            }        } catch (FileNotFoundException e1) {            e1.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    public static void main(String[] args) {         new lexical_analysis().lexical_analysis();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.