2009-12-02 10 views
7

Estaba intentando hacer ANTLR 3.2 generar analizador/lexer en C++. Fue infructuoso. Las cosas fueron bien con Java & C though.¿Está lista la generación de código C++ en ANTLR 3.2?

yo estaba usando este tutorial para empezar: http://www.ibm.com/developerworks/aix/library/au-c_plusplus_antlr/index.html

Al revisar los archivos * .stg, me encontré con que:

CPP tiene solamente

./tool/src/main/resources/org/antlr/codegen/templates/CPP/CPP.stg 

C tiene tantos archivos :

./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 

Y así otros idiomas.

archivo Mi 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 

Errores:

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] 

... y así sucesivamente.

Tenga en cuenta amablemente. ¡Gracias! Estoy usando Leopard 10.5.8 con

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 

Respuesta

9

Suena como que haya respondido a su propia pregunta: C++ generadores de analizador léxico/analizador de ANTLR aún no son funcionales.

Por lo que vale, aún es posible usar ANTLR para analizar desde C++, a través del objetivo C. Uso ANTLR para generar un analizador y analizador de lenguaje C, que luego compilo y vinculo a mi código C++.

Tengo un archivo C++ que traduce un árbol de análisis ANTLR a mis clases de árbol de sintaxis abstracta de destino, y al resto de mi código no le importa de dónde proviene el AST. ¡Funciona bastante bien en la práctica! Sería fácil reemplazar ANTLR con un generador de analizador diferente, y creo que la separación conduce a gramáticas ANTLR más limpias.

+0

Hola Ben, ¿podrías liberar tu herramienta C++ con licencia de código abierto? ¡Gracias! – Viet

4

He publicado un C++ Target para ANTLR. Por favor, míralo.

+0

¿Podría publicar el enlace? ¡Gracias! – Viet

+1

http://www.antlr.org/wiki/pages/viewpage.action?pageId=29130826 – Gokul

+0

+1 genial! ¡Gracias por compartir! – Viet

Cuestiones relacionadas