Pergunta

Eu estava se esforçando para fazer o ANTLR 3.2 gerar analisador/lexer em C ++. Foi infrutífero. As coisas correram bem com Java & c embora.

Eu estava usando este tutorial para começar: http://www.ibm.com/developerworks/ix/library/au-c_plusplus_antlr/index.html

Quando verifiquei os arquivos *.stg, achei que:

CPP tem apenas

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

C tem tantos arquivos:

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

E assim outros idiomas.

Meu arquivo 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

Erros:

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]

... e assim por diante.

Por favor, avise. Obrigada! Estou usando o Leopard 10.5.8 com

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
Foi útil?

Solução

Parece que você respondeu sua própria pergunta: os geradores de Lexer/analisador da ANTLR ainda não estão funcionais.

Pelo que vale a pena, ainda é possível usar o ANTLR para analisar do C ++, através do alvo C. Eu uso o ANTLR para gerar um idioma C Lexer e o analisador, que eu compilar e vincular ao meu código C ++.

Eu tenho um arquivo C ++ que traduz uma árvore de análise ANTLR para as classes de árvores de sintaxe abstrata de destino, e o restante do meu código não se importa de onde vem o AST. Funciona muito bem na prática! Seria fácil substituir o ANTLR por um gerador de analisador diferente, e acho que a separação leva a gramáticas ANTLR mais limpas.

Outras dicas

Eu publiquei um alvo C ++ para Antlr. Por favor, confira.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top