最近项目需要想用到hive中antlr的sql语法解析做二次开发,之前看hive源码的时候编译过,也遇到不少问题,现在居然忘了,看来好记性不如烂笔头,差记性就更加得多写写了。
编译是在windows xp环境下进行的,下载所需要的文件如下:
antlr-2.7.7.jar
antlr-3.0.1.jar
stringtemplate-3.1b1.jar
Hive.g
文件放在目录C:\hive\BAK目录下,DOS命令行进入到目录,输入:
- C:\hive\BAK>java -cp antlr-2.7.7.jar;antlr-3.0.1.jar;stringtemplate-3.1b1.jar or
- g.antlr.Tool Hive.g
C:\hive\BAK>java -cp antlr-2.7.7.jar;antlr-3.0.1.jar;stringtemplate-3.1b1.jar or
g.antlr.Tool Hive.g
会输出:
- ANTLR Parser Generator Version 3.0.1 (August 13, 2007) 1989-2007
- warning(200): Hive.g:1461:5: Decision can match input such as "Identifier DOT Id
- entifier" using multiple alternatives: 1, 2
- As a result, alternative(s) 2 were disabled for that input
ANTLR Parser Generator Version 3.0.1 (August 13, 2007) 1989-2007
warning(200): Hive.g:1461:5: Decision can match input such as "Identifier DOT Id
entifier" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
再看目录下会增加几个文件
HiveParser.java
Hive.tokens
Hive__.g
HiveLexer.java
HiveLexer.java和 HiveParser.java分别是生成的对应词法和语法分析类文件
Hive__.g对应HiveLexer.java的词法分析规范
Hive.tokens定义了词法分析后的所有token
需要说明的是antlr-3.0.1.jar是在antlr-2.7.7.jar的基础上进行的开发,有依赖关系。
编译中遇到的错误:
1、有帖子说编译格式为:
- C:\hive\BAK>java antlr.Tool Hive.g
- ANTLR Parser Generator Version 2.7.7 (20060906) 1989-2005
- Hive.g:17:1: unexpected token: grammar
- error: Token stream error reading grammar(s):
- Hive.g:247:1: unexpected char: '@'
- Hive.g:17:1: rule classDef trapped:
- Hive.g:17:1: unexpected token: grammar
- error: aborting grammar 'unknown grammar' due to errors
- TokenStreamException: unexpected char: '@'
C:\hive\BAK>java antlr.Tool Hive.g
ANTLR Parser Generator Version 2.7.7 (20060906) 1989-2005
Hive.g:17:1: unexpected token: grammar
error: Token stream error reading grammar(s):
Hive.g:247:1: unexpected char: '@'
Hive.g:17:1: rule classDef trapped:
Hive.g:17:1: unexpected token: grammar
error: aborting grammar 'unknown grammar' due to errors
TokenStreamException: unexpected char: '@'
原因是没有找到对应版本的jar包,应该运行是antlr-3.0.1.jar,而且antlr-3.0.1.jar对应Tool的目录是
org.antlr.Tool
2、编译时没有加classpath
- C:\hive\BAK>java org.antlr.Tool Hive.g
- Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/Tool
C:\hive\BAK>java org.antlr.Tool Hive.g
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/Tool
3、NoClassDefFoundError错误
- Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/stringtempl
- ate/StringTemplateErrorListener
- at org.antlr.Tool.main(Tool.java:67)
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/stringtempl
ate/StringTemplateErrorListener
at org.antlr.Tool.main(Tool.java:67)
是因为用到的stringtemplate-3.1b1.jar包没有导入到classpath中
总的来说antlr和flex+yacc比较像,使用起来还算比较方便,细心点就行~