词法分析程序(Lexical Analyzer)要求:java
- 从左至右扫描构成源程序的字符流spring
- 识别出有词法意义的单词(Lexemes)springboot
- 返回单词记录(单词类别,单词自己)数据结构
- 滤掉空格测试
- 跳过注释blog
- 发现词法错误字符串
程序结构:get
输入:字符流(什么输入方式,什么数据结构保存)hash
处理:table
–遍历(什么遍历方式)
–词法规则
输出:单词流(什么输出形式)
–二元组
单词类别:
1.标识符(10)
2.无符号数(11)
3.保留字(一词一码)
4.运算符(一词一码)
5.界符(一词一码)
单词符号 |
种别码 |
单词符号 |
种别码 |
begin |
1 |
: |
17 |
if |
2 |
:= |
18 |
then |
3 |
< |
20 |
while |
4 |
<= |
21 |
do |
5 |
<> |
22 |
end |
6 |
> |
23 |
l(l|d)* |
10 |
>= |
24 |
dd* |
11 |
= |
25 |
+ |
13 |
; |
26 |
- |
14 |
( |
27 |
* |
15 |
) |
28 |
/ |
16 |
# |
0 |
本案例使用Java编写解析器
package com.lzh.springbootstudytestcache; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * @author lzh * create 2019-10-09-21:13 */ public class homework3 { public static HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入字符串"); String str = sc.nextLine(); parse(str); } public static void parse(String str) { hashMap.put(0, "#"); hashMap.put(1, "begin"); hashMap.put(2, "if"); hashMap.put(3, "then"); hashMap.put(4, "while"); hashMap.put(5, "do"); hashMap.put(6, "end"); hashMap.put(10, "l(l|d)*"); hashMap.put(11, "dd*"); hashMap.put(13, "+"); hashMap.put(14, "-"); hashMap.put(15, "*"); hashMap.put(16, "/"); hashMap.put(17, ":"); hashMap.put(18, ":="); hashMap.put(20, "<"); hashMap.put(21, "<="); hashMap.put(22, "<>"); hashMap.put(23, ">"); hashMap.put(24, ">="); hashMap.put(25, "="); hashMap.put(26, ";"); hashMap.put(27, "("); hashMap.put(28, ")"); //测试数据 //dsf 324 df(sdf if ad<= fds >sdf //while( 324 > f) do if( 22 <= a) asf //初始化 String stmp = "" + str.toCharArray()[0]; for (int i = 1; i < str.length(); i++) { //读取到空格输出 if (str.toCharArray()[i] == ' ') { // System.out.println("stmp = "+ stmp); String regex = "\\d\\d*"; boolean matches = stmp.matches(regex); //是数字 if (matches) { System.out.println("(11," + stmp + ")"); } //判断是不是关键词 for (Map.Entry<Integer, String> entry : hashMap.entrySet()) { if (stmp.equals(entry.getValue())) { System.out.println("(" + entry.getKey() + "," + entry.getValue() + ")"); stmp = ""; break; } } stmp = ""; } else { //读取到非空格 stmp += str.toCharArray()[i]; for (Map.Entry<Integer, String> entry1 : hashMap.entrySet()) { //读取一个字符,若是是关键词则输出 if (("" + str.toCharArray()[i]).equals(entry1.getValue())) { int flag = 0; for (Map.Entry<Integer, String> entry11 : hashMap.entrySet()) { //当前字符是关键词,而且下一个也是关键词 if (i < str.length() && ("" + str.toCharArray()[i + 1]).equals(entry11.getValue())) { System.out.println("(" + hashMapGetKey("" + str.toCharArray()[i] + str.toCharArray()[i + 1]) + "," + str.toCharArray()[i] + str.toCharArray()[i + 1] + ")"); stmp = ""; i += 1; flag = 1; break; } } //不是两个链接在一块儿的关键词 /*if (flag == 0) { System.out.println("(" + entry1.getKey() + "," + entry1.getValue() + ")"); }*/ break; } else { //不是关键词,追加 //判断追加后是否为关键词 for (Map.Entry<Integer, String> entry2 : hashMap.entrySet()) { if (stmp.equals(entry2.getValue())) { System.out.println("(" + entry2.getKey() + "," + entry2.getValue() + ")"); stmp = ""; break; } } } } } } } //经过键获取值 public static int hashMapGetKey(String value) { for (Map.Entry<Integer, String> integerStringEntry : hashMap.entrySet()) { if ((integerStringEntry.getValue()).equals(value)) { return integerStringEntry.getKey(); } } return -1; } }