是 C++ ANTLR 3.2 中的代码生成准备好了吗?
我正在努力让 ANTLR 3.2 在 C++ 中生成解析器/词法分析器。但毫无结果。 Java 和 Java 的一切进展顺利。尽管是C。
我使用本教程开始: http://www. ibm.com/developerworks/aix/library/au-c_plusplus_antlr/index.html
当我检查 *.stg 文件时,我发现:
CPP 只有
./tool/src/main/resources/org/antlr/codegen/templates/CPP/CPP.stg
C 有这么多文件:
./tool/src/main/resources/org/antlr/codegen/templates/C/AST.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/ASTDbg.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/ASTParser.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/ASTTreeParser.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/C.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/Dbg.stg
其他语言也是如此。
我的 Cg 文件:
grammar C;
options { language='CPP'; }
/** Match things like "call foo;" */
r : 'call' ID ';' {System.out.println("invoke "+$ID.text);} ;
ID: ('a'..'z'|'A'..'Z'|'_')('0'..'9'|'a'..'z'|'A'..'Z'|'_')* ;
WS: (' ' |'\n' |'\r' )+ {$channel=HIDDEN;} ; // ignore whitespace
错误:
error(10): internal error: group Cpp does not satisfy interface ANTLRCore: missing templates [lexerRuleRefAndListLabel, parameterSetAttributeRef, scopeSetAttributeRef, returnSetAttributeRef, lexerRulePropertyRef_text, lexerRulePropertyRef_type, lexerRulePropertyRef_line, lexerRulePropertyRef_pos, lexerRulePropertyRef_index, lexerRulePropertyRef_channel, lexerRulePropertyRef_start, lexerRulePropertyRef_stop, ruleSetPropertyRef_tree, ruleSetPropertyRef_st]
error(10): internal error: group Cpp does not satisfy interface ANTLRCore: mismatched arguments on these templates [outputFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass, literals), optional headerFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass, literals), lexer(grammar, name, tokens, scopes, rules, numRules, labelType, filterMode, superClass), rule(ruleName, ruleDescriptor, block, emptyRule, description, exceptions, finally, memoize), alt(elements, altNum, description, autoAST, outerAlt, treeLevel, rew), tokenRef(token, label, elementIndex, hetero), tokenRefAndListLabel(token, label, elementIndex, hetero), listLabel(label, elem), charRangeRef(a, b, label), ruleRef(rule, label, elementIndex, args, scope), ruleRefAndListLabel(rule, label, elementIndex, args, scope), lexerRuleRef(rule, label, args, elementIndex, scope), lexerMatchEOF(label, elementIndex), tree(root, actionsAfterRoot, children, nullableChildList, enclosingTreeLevel, treeLevel)]
error(10): internal error: C.g : java.lang.IllegalArgumentException: Can't find template actionGate.st; group hierarchy is [Cpp]
...等等。
请各位指教。谢谢你!我正在使用 Leopard 10.5.8
CLASSPATH=:/Users/vietlq/projects/antlr-3.2.jar:/Users/vietlq/projects/stringtemplate-3.2.1/lib/stringtemplate-3.2.1.jar:/Users/vietlq/projects/stringtemplate-3.2.1/lib/antlr-2.7.7.jar
I was trying hard to make ANTLR 3.2 generate parser/lexer in C++. It was fruitless. Things went well with Java & C though.
I was using this tutorial to get started: http://www.ibm.com/developerworks/aix/library/au-c_plusplus_antlr/index.html
When I checked the *.stg files, I found that:
CPP has only
./tool/src/main/resources/org/antlr/codegen/templates/CPP/CPP.stg
C has so many files:
./tool/src/main/resources/org/antlr/codegen/templates/C/AST.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/ASTDbg.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/ASTParser.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/ASTTreeParser.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/C.stg
./tool/src/main/resources/org/antlr/codegen/templates/C/Dbg.stg
And so other languages.
My C.g file:
grammar C;
options { language='CPP'; }
/** Match things like "call foo;" */
r : 'call' ID ';' {System.out.println("invoke "+$ID.text);} ;
ID: ('a'..'z'|'A'..'Z'|'_')('0'..'9'|'a'..'z'|'A'..'Z'|'_')* ;
WS: (' ' |'\n' |'\r' )+ {$channel=HIDDEN;} ; // ignore whitespace
Errors:
error(10): internal error: group Cpp does not satisfy interface ANTLRCore: missing templates [lexerRuleRefAndListLabel, parameterSetAttributeRef, scopeSetAttributeRef, returnSetAttributeRef, lexerRulePropertyRef_text, lexerRulePropertyRef_type, lexerRulePropertyRef_line, lexerRulePropertyRef_pos, lexerRulePropertyRef_index, lexerRulePropertyRef_channel, lexerRulePropertyRef_start, lexerRulePropertyRef_stop, ruleSetPropertyRef_tree, ruleSetPropertyRef_st]
error(10): internal error: group Cpp does not satisfy interface ANTLRCore: mismatched arguments on these templates [outputFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass, literals), optional headerFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name, tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode, profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass, literals), lexer(grammar, name, tokens, scopes, rules, numRules, labelType, filterMode, superClass), rule(ruleName, ruleDescriptor, block, emptyRule, description, exceptions, finally, memoize), alt(elements, altNum, description, autoAST, outerAlt, treeLevel, rew), tokenRef(token, label, elementIndex, hetero), tokenRefAndListLabel(token, label, elementIndex, hetero), listLabel(label, elem), charRangeRef(a, b, label), ruleRef(rule, label, elementIndex, args, scope), ruleRefAndListLabel(rule, label, elementIndex, args, scope), lexerRuleRef(rule, label, args, elementIndex, scope), lexerMatchEOF(label, elementIndex), tree(root, actionsAfterRoot, children, nullableChildList, enclosingTreeLevel, treeLevel)]
error(10): internal error: C.g : java.lang.IllegalArgumentException: Can't find template actionGate.st; group hierarchy is [Cpp]
... and so on.
Please kindly advise. Thank you! I'm using Leopard 10.5.8 with
CLASSPATH=:/Users/vietlq/projects/antlr-3.2.jar:/Users/vietlq/projects/stringtemplate-3.2.1/lib/stringtemplate-3.2.1.jar:/Users/vietlq/projects/stringtemplate-3.2.1/lib/antlr-2.7.7.jar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您已经回答了自己的问题:ANTLR 的 C++ 词法分析器/解析器生成器尚未运行。
无论如何,仍然可以使用 ANTLR 通过 C 目标从 C++ 进行解析。我使用 ANTLR 生成 C 语言词法分析器和解析器,然后将其编译并链接到我的 C++ 代码。
我有一个 C++ 文件,它将 ANTLR 解析树转换为我的目标抽象语法树类,而我的其余代码并不关心 AST 来自哪里。在实践中效果非常好!用不同的解析器生成器替换 ANTLR 很容易,而且我发现这种分离会导致更清晰的 ANTLR 语法。
It sounds like you've answered your own question: ANTLR's C++ lexer/parser generators are not yet functional.
For what it's worth, it's still possible to use ANTLR for parsing from C++, via the C target. I use ANTLR to generate a C language lexer and parser, which I then compile and link to my C++ code.
I have one C++ file that translates an ANTLR parse tree to my target abstract syntax tree classes, and the rest of my code doesn't care where the AST comes from. It works pretty well in practice! It would be easy to replace ANTLR with a different parser generator, and I find that the separation leads to cleaner ANTLR grammars.
我已经发布了 ANTLR 的 C++ 目标。请检查一下。
I have posted a C++ Target for ANTLR. Please check it out.