[antlr-interest] <AST>: unexpected AST node: (
Ning Ji
ning883 at gmail.com
Tue Jan 2 14:18:10 PST 2007
hi everyone, i have this very simple code exercising AST,
when i input (1+2), it gives me
/home/antlr>./calc
(1+2)
<AST>: unexpected AST node: (
( 1 + 2 ) = 0
Anyone can give me some hints ? thanks !
------------------------- this is the main.cpp ---------------
//main.cpp
#include "CalcLexer.hpp"
#include "CalcParser.hpp"
#include "CalcTreeWalker.hpp"
#include <iostream.h>
using namespace std;
using namespace antlr;
int main()
{
try {
CalcLexer lexer(cin);
CalcParser parser(lexer);
ASTFactory factory;
parser.initializeASTFactory(factory);
parser.setASTFactory(&factory);
parser.expr();
RefAST t = parser.getAST();
CalcTreeWalker walker;
float r = walker.expr(t);
cout << t->toStringList() << " = " << r << endl;
} catch(exception& e) {
cerr << "exception: " << e.what() << endl;
}
return 0;
}
----------- this is the *.g --------------------------------------------
// calc.g
options
{
language="Cpp";
}
class CalcLexer extends Lexer;
public
INTEGER: (DIGIT)+;
LPAREN: '(';
RPAREN: ')';
PLUS: '+';
protected
DIGIT: '0'..'9';
class CalcParser extends Parser;
options
{
buildAST=true;
}
expr: LPAREN INTEGER (PLUS INTEGER)* RPAREN;
class CalcTreeWalker extends TreeParser;
expr returns [float r]
{
float a,b;
r=0;
}
: #(PLUS a=expr b=expr) {r = a+b;}
| i:DIGIT {r = atof(i->getText().c_str());}
;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070102/88ca0d93/attachment.html
More information about the antlr-interest
mailing list