Pregunta

Yo estaba tratando duro para hacer antlr 3,2 generar analizador / analizador léxico en C ++. Fue infructuosa. Las cosas fueron bien con Java y C embargo.

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, encontré 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.

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

Por favor, amablemente aconsejar. ¡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
¿Fue útil?

Solución

Parece que usted ha contestado a su propia pregunta:. C ++ generadores de analizador léxico / analizador de ANTLR aún no son funcionales

Por lo que vale, sigue siendo posible utilizar antlr para analizar de C ++, a través del objetivo C. Yo uso antlr para generar un analizador léxico analizador de lenguaje C y, que luego de compilación y enlace a mi código C ++.

Tengo un archivo C ++ que traduce un árbol de análisis sintáctico antlr a mis objetivo clases abstractas del árbol de sintaxis, y el resto de mi código no importa donde el AST viene. Funciona bastante bien en la práctica! Sería fácil de reemplazar antlr con un generador de análisis diferente, y me parece que la separación conduce a las gramáticas ANTLR más limpias.

Otros consejos

He publicado un objetivo C ++ para antlr. Por favor, comprobar que funciona.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top