网站开发样板,怎么样更好的做网站,wordpress编辑父主题,网站开发与制作中期报告示例#xff1a;SimpleSelectParser 解析 select 11; 输出 2#xff1b;
0#xff09;总结
编写 JavaCC 模板#xff0c;*.jj 文件。
编译生成代码文件。
移动代码文件到对应的包下。
调用生成的代码文件。
1#xff09;JavaCC 模板
main/javacc/SimpleSelectParse…示例SimpleSelectParser 解析 select 11; 输出 2
0总结
编写 JavaCC 模板*.jj 文件。
编译生成代码文件。
移动代码文件到对应的包下。
调用生成的代码文件。
1JavaCC 模板
main/javacc/SimpleSelectParser.jj
options {IGNORE_CASE true;// 允许被多次初始化STATIC false;
}PARSER_BEGIN(SimpleSelectParser)package cn.com.ptpress.cdm.parser.select;
import java.io.* ;public class SimpleSelectParser {private String sql;public void parse() throws ParseException {SelectExpr(sql);}public SimpleSelectParser(String expr) {this((Reader)(new StringReader(expr)));this.sql expr;}public static void main(String[] args) throws Exception{final SimpleSelectParser parser new SimpleSelectParser(String.join( , args));parser.parse();}
}PARSER_END(SimpleSelectParser)void SelectExpr(String sql) :
{int res;
}
{SELECTres Expression(){System.out.println(sql res);}
}int Expression() :
{int res 0;int v;
}
{res Number()(ADDv Number(){res v;}|SUBv Number(){res - v;})*{return res;}
}int Number() :
{Token t;
}
{t NUMBER{return Integer.parseInt(t.image);}
}TOKEN :
{ SELECT: SELECT
| NUMBER: ([0-9])
| ADD:
| SUB: -
}/*
跳过的制表符
* */
SKIP :
{
| \t
| \n
| \r
| \f
}2Java CC 的 Maven 插件
buildpluginsplugingroupIdorg.codehaus.mojo/groupIdartifactIdjavacc-maven-plugin/artifactIdversion2.6/versionexecutionsexecutionphasegenerate-sources/phaseidjavacc/idgoalsgoaljavacc/goal/goalsconfigurationsourceDirectory${basedir}/src/main/javacc/sourceDirectoryincludesinclude**/*.jj/include/includes!-- lookAhead2/lookAhead--!-- isStaticfalse/isStatic--outputDirectory${basedir}/generated-sources//outputDirectory/configuration/execution/executions/plugin/pluginsresourcesresourcedirectorysrc/main/resources/directory/resource/resources/build3执行编译命令
mvn org.codehaus.mojo:javacc-maven-plugin:2.6:javacc生成的文件 4主类调用
1.将生成的文件移动到 .jj 文件配置的包下 2.启动 TestParser 类
package cn.com.ptpress.cdm.parser.select;public class TestParser {public static void main(String[] args) throws ParseException {parseSelect(select 11);parseSelect(select 111);parseSelect(select 1 3 - 5);}private static void parseSelect(String sql) throws ParseException {final SimpleSelectParser parser new SimpleSelectParser(sql);// 解析的核心方法parser.parse();}
}