2009-12-02 13 views
7

Stavo cercando di rendere ANTLR 3.2 generare parser/lexer in C++. Era infruttuoso. Le cose sono andate bene con Java & C.La generazione del codice C++ in ANTLR 3.2 è pronta?

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.

file di mio 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 avvisare. 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 

risposta

9

Suona come hai risposto alla tua domanda: C++ generatori lexer/parser di ANTLR non sono ancora funzionali.

Per quello che vale, è ancora possibile usare ANTLR per l'analisi da C++, attraverso l'obiettivo C. Io uso ANTLR per generare un lessico e un parser in linguaggio C, che poi compilo e link al mio codice C++.

Ho un file C++ che traduce un albero di analisi ANTLR per le mie classi di albero di sintassi abstract di destinazione e il resto del mio codice non interessa da dove proviene l'AST. Funziona abbastanza bene nella pratica! Sarebbe facile sostituire ANTLR con un diverso generatore di parser, e trovo che la separazione porti a grammatiche ANTLR più pulite.

+0

Ciao Ben, ti piacerebbe rilasciare il tuo strumento C++ sotto licenza open source? Grazie! – Viet

4

Ho inviato un target C++ per ANTLR. Per favore controlla.

+0

Potresti postare il link? Grazie! – Viet

+1

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

+0

+1 fantastico! Grazie per la condivisione! – Viet

Problemi correlati