[antlr-interest] Wildcard in tree grammar? - was validating semantic predicates
Harald M. Müller
harald_m_mueller at gmx.de
Fri Nov 30 14:12:16 PST 2007
Sorry - I had the wrong output in my posting: The ignore version of course
outputs simply
whoaa
as intended ...
Regards
Harald M.
------------------------------
Hi tree grammar gurus -
I wrote to mark about that "ignore rule" - but now I found on the Wiki page
http://www.antlr.org/wiki/display/ANTLR3/Simple+tree-based+interpeter that
"The wildcard says skip the entire subtree (new in 3.0b6)."
However, the program below does NOT work (at least for me?) when I replace
the call to ignore with a simple wildcard (dot). Instead, it says
whoaa
Tree.g: node from line 1:37 no viable alternative at input 'if'
:-(
- whereas the "ignore" version simply outputs
whoaa
:-(
as intended.
Has anyone a clue why's that so?? (it seems to have to do something with the
lookahead on the tree node stream, doesn't it?)
Regards
Harald M.
P.S. Mark, there's your running example :-)
-----------------
grammar LexerParser;
options {
language=CSharp;
output=AST;
ASTLabelType=CommonTree;
}
file
: (statement ';'!)+
;
statement
: simpleStatement
| 'if'^ condition statement
;
simpleStatement
: 'print'^ 'true'
| 'print'^
;
condition
: 'true'^
| 'false'^
;
WS : (' '|'\r'|'\t'|'\f'|'\n')+ { $channel=HIDDEN; };
-----------------
tree grammar Tree;
options {
language=CSharp;
tokenVocab=LexerParser;
ASTLabelType=CommonTree;
}
file
: (statement)+
;
statement
: simpleStatement
|
^('if' x=condition ( { x }? =statement
| . // instead of ignore - the
Wiki says: "The wildcard says skip the entire subtree (new in 3.0b6)."
)
)
;
//ignore
//// : . (DOWN (ignore)* UP)?
// : ^(. (ignore)*)
// ;
simpleStatement
: ^('print' 'true') { Console.Out.WriteLine("whoaa"); }
| 'print' { Console.Out.WriteLine(":-("); }
;
condition returns [bool result]
: 'true' { result = true; }
| 'false' { result = false; }
;
------------------
using System;
using Antlr.Runtime;
using Antlr.Runtime.Tree;
class Program {
static void Main() {
ICharStream s = new ANTLRStringStream("if true if
true print true; if false if true print;");
LexerParserLexer l = new LexerParserLexer(s);
CommonTokenStream ts = new CommonTokenStream(l);
LexerParserParser parser = new LexerParserParser(ts);
ITreeNodeStream tns = new
CommonTreeNodeStream(parser.file().Tree);
new Tree(tns).file();
Console.In.Read();
}
}
More information about the antlr-interest
mailing list