·java
實現了一個簡單的java詞法分析器git
功能:詞法分析下面一段java小程序小程序
1 int sum = 0; for(int i = 1; i <= 100; i = i + 1) sum += i;#
1.程序片斷中使用到的關鍵字、運算符和界符:app
關鍵詞:ide
Int forspa
運算符:設計
+ =指針
界符:blog
( ) <= #token
2.單詞和單詞種別碼設計
單詞符號 |
種別碼 |
Int |
1 |
For |
2 |
Letter(letter|digit)* |
3 |
Digit|digit* |
4 |
* |
5 |
/ |
6 |
+ |
7 |
- |
8 |
> |
9 |
>= |
10 |
< |
11 |
<= |
12 |
= |
13 |
; |
14 |
( |
15 |
) |
16 |
# |
17 |
詞法分析器源代碼
1 package com.gxf.lexical; 2 3 import java.util.Scanner; 4 5 public class Lexical { 6 String input = ""; //源程序 7 String keyWords[] = {"int", "for"}; //關鍵詞 8 int point = 0; //全局指針指向源程序 9 int syn = 0; //單詞種別碼 10 int sum = 0; //數字的總和 11 StringBuffer token = new StringBuffer(""); //存放單詞 12 13 public static void main(String[] args) { 14 Scanner scanner = new Scanner(System.in); 15 Lexical lexical = new Lexical(); 16 17 lexical.input = scanner.nextLine(); 18 // System.out.println(lexical.input);//輸入源程序 19 scanner.close(); 20 21 do{ 22 lexical.scanner(); 23 switch(lexical.syn){ 24 case 4: 25 System.out.println("(" + lexical.syn + "," + lexical.sum + ")"); 26 break; 27 default: 28 System.out.println("(" + lexical.syn + "," + lexical.token + ")"); 29 break; 30 } 31 }while(lexical.syn != 17); 32 } 33 34 /** 35 * 詞法分析器 36 */ 37 public void scanner(){ 38 //將單詞置爲空 39 token = new StringBuffer(); 40 while(' ' == input.charAt(point)) 41 point++; //去掉空格 42 if((input.charAt(point) >= 'a' && input.charAt(point) <= 'z') || 43 (input.charAt(point) >= 'A' && input.charAt(point) <= 'Z')){//關鍵詞或者標識符 44 syn = 3;//種別碼爲3 45 while((input.charAt(point) >= 'a' && input.charAt(point) <= 'z') || 46 (input.charAt(point) >= 'A' && input.charAt(point) <= 'Z') || 47 (input.charAt(point) >= '0' && input.charAt(point) <= '9')){ 48 token.append(input.charAt(point)); 49 point++; 50 } 51 // point--;//後退一個位置 52 for(int i = 0; i < keyWords.length; i++){ 53 if(keyWords[i].equals(String.valueOf(token))){ 54 syn = i + 1;//修改種別碼 55 break; 56 } 57 } 58 }//if 59 else if(input.charAt(point) >= '0' && input.charAt(point) <= '9'){//若是是數字 60 syn = 4; 61 sum = 0; 62 while(input.charAt(point) >= '0' && input.charAt(point) <= '9'){ 63 sum = sum * 10 + (input.charAt(point) - '0'); 64 point++; 65 } 66 //point--;//後退一個字符 67 }//else if 68 else{//其餘字符 69 switch(input.charAt(point)){ 70 case '>'://大於符號 71 token = new StringBuffer(">");//重置token 72 point++; 73 if(input.charAt(point) == '='){ 74 token.append("="); 75 syn = 10; 76 }else{ 77 syn = 9; 78 point--; 79 } 80 point++; 81 break; 82 case '<': 83 token = new StringBuffer("<"); 84 point++; 85 if(input.charAt(point) == '='){ 86 token.append("="); 87 syn = 12; 88 }else{ 89 syn = 11; 90 point--; 91 } 92 point++; 93 break; 94 case '*': 95 token = new StringBuffer("*"); 96 syn = 5; 97 point++; 98 break; 99 case '/': 100 token = new StringBuffer("/"); 101 syn = 6; 102 point++; 103 break; 104 case '+': 105 token = new StringBuffer("+"); 106 syn = 7; 107 point++; 108 break; 109 case '-': 110 token = new StringBuffer("-"); 111 syn = 8; 112 point++; 113 break; 114 case ';': 115 token = new StringBuffer(";"); 116 syn = 14; 117 point++; 118 break; 119 case '(': 120 token = new StringBuffer("("); 121 syn = 15; 122 point++; 123 break; 124 case ')': 125 token = new StringBuffer(")"); 126 syn = 16; 127 point++; 128 break; 129 case '#': 130 token = new StringBuffer("#"); 131 syn = 17; 132 point++; 133 break; 134 case '=': 135 token = new StringBuffer("="); 136 syn = 13; 137 point++; 138 break; 139 } 140 141 } 142 } 143 }
注意程序片斷要以#號結束
其實,上面參考的博客寫得還不錯能夠看看