我正在尝试为这样的源代码创建解析器:
[code table 1.0]
code table code_table_name
id = 500
desc = "my code table one"
end code table
...下面是我定义的语法:
PARSER_BEGIN(CodeTableParser)
...
PARSER_END(CodeTableParser)
/* skip spaces */
SKIP: {
" "
| "\t"
| "\r"
| "\n"
}
/* reserved words */
TOKEN [IGNORE_CASE]: {
<CODE_TAB_HEADER: "[code table 1.0]">
| <CODE_TAB_END: "end" (" ")+ <CODE_TAB_BEGIN>>
| <CODE_TAB_BEGIN: <IDENT> | "code" (" ")+ "table">
| <ID: "id">
| <DESC: "desc">
}
/* token images */
TOKEN: {
<NUMBER: (<DIGIT>)+>
| <IDENT: (<ALPHA>)+>
| <VALUE: (<ALPHA> ["[", "]"])+>
| <STRING: <QUOTED>>
}
TOKEN: {
<#ALPHA: ["A"-"Z", "a"-"z", "0"-"9", "$", "_", "."]>
| <#DIGIT: ["0"-"9"]>
| <#QUOTED: "\"" (~["\""])* "\"">
}
void parse():
{
}
{
expression() <EOF>
}
void expression():
{
Token tCodeTab;
}
{
<CODE_TAB_HEADER>
<CODE_TAB_BEGIN>
tCodeTab = <IDENT>
(
<ID>
<DESC>
)*
<CODE_TAB_END>
}
问题是解析器正确识别了令牌(“代码表”)......但它没有识别令牌 IDENT(“code_table_name”),因为它包含已经包含在令牌 CODE_TAB_BEGIN 中的单词(即“代码”)。解析器抱怨说“代码后面跟着无效字符_”......
话虽如此,我想知道为了让解析器正常工作,我缺少什么。我是新手,任何帮助将不胜感激;-)
谢谢,j3d