lexer and parser 的java简单实现

A pragmatic benefit of using a dedicated upfront lexing phase is that you don’t couple the subsequent parser with lexical detail. This is useful during early programming language development, when the lexical and syntactic details are still changing frequently
使用专用的前期lexing阶段的一个实际好处是,您不会将后续解析器与词法细节相结合。这在早期编程语言开发期间很有用,因为词法和句法细节仍然经常变化。
参考

  1. Why separate lexing and parsing?
  2. https://stackoverflow.com/questions/2842809/lexers-vs-parsers
  3. https://www.zhihu.com/question/31065265

1. Lexer

github:compile/src/lexer
java实现一个词法分析器,参考link可以识别加法与乘法中的token。

token种别码
EOI0
SEMI(;)1
PLUS(+)2
TIMES(*)3
LP( ( )4
RP( ) )5
NUM6
INT7
EQ( =)8
ID9

运行src/lexer
input:

int a = 1 ;
int b = a + 1;
end

output:
在这里插入图片描述

实现思路就是根据那个表,用switch…case…框架和最长匹配原则实现了词法分析。

2.parse

Grammar:

statements -> expression ;
	   | expression ; statements

expression -> term 
 	   |term + expression
  
term -> factor 
           | factor * term
 
 factor -> NUM_OR_ID
	   | LP expression RP

github:compile/src/lexer
语法太过简单,用递归下降算法可得:

input:

1+2+3
end

output

在这里插入图片描述
input :

213+123+23989+4546;
34545*233+8980;
end		

output:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值