Domanda

Stavo cercando difficile fare ANTLR 3.2 generare parser / lexer in C ++. E 'stato inutile. Le cose sono andate bene con Java e C però.

stavo usando questo tutorial per iniziare: http: / /www.ibm.com/developerworks/aix/library/au-c_plusplus_antlr/index.html

Quando ho controllato i file * .stg, ho scoperto che:

CPP ha solo

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

C ha così tanti file:

./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 così altre lingue.

Il mio file 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

Errori:

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 così via.

Si prega gentilmente di comunicare. Grazie! Sto 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
È stato utile?

Soluzione

Sembra che tu hai risposto alla tua domanda:. C ++ generatori lexer / parser di ANTLR non sono ancora funzionali

Per quello che vale, è ancora possibile utilizzare ANTLR per l'analisi da C ++, attraverso l'obiettivo C. Io uso ANTLR per generare un lexer linguaggio C e parser, che ho quindi compilare e linkare il mio codice C ++.

Ho un file C ++ che traduce un albero sintattico ANTLR al mio obiettivo classi astratte albero di sintassi, e il resto del mio codice non importa dove l'AST proviene. Funziona abbastanza bene in pratica! Sarebbe facile da sostituire ANTLR con un generatore di parser diverso, e trovo che la separazione porta a più puliti grammatiche ANTLR.

Altri suggerimenti

Ho inviato un target C ++ per ANTLR. Si prega di check it out.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top