ANTLR 3.2中的C ++代码生成准备好了吗?

时间:2009-12-02 08:46:28

标签: c++ code-generation parsing lexer antlr3

我努力让ANTLR 3.2在C ++中生成解析器/词法分析器。它毫无结果。 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

等其他语言。

我的C.g文件:

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

2 个答案:

答案 0 :(得分:9)

听起来你已经回答了自己的问题:ANTLR的C ++词法分析器/解析器生成器还没有运行。

对于它的价值,仍然可以使用ANTLR通过C目标从C ++进行解析。我使用ANTLR生成一个C语言词法分析器和解析器,然后我编译并链接到我的C ++代码。

我有一个C ++文件,它将ANTLR解析树转换为我的目标抽象语法树类,而我的其余代码并不关心AST的来源。它在实践中运作良好!使用不同的解析器生成器替换ANTLR会很容易,我发现分离会导致更清晰的ANTLR语法。

答案 1 :(得分:4)

我已经为ANTLR发布了一个C ++目标。请检查一下。