[antlr-interest] Re: SQL grammar tree construction problem

Thomas Brandon tom at psy.unsw.edu.au
Wed Nov 12 16:38:07 PST 2003


Not sure that this is the problem but...
I don't think Antlr requires rooted trees. Without a root it should 
just generate a sequence of tokens. However, the ASTFrame does 
require a single root, if you have multiple roots it only displays 
the first one. So, if the problem is with ASTFrame you might try one 
of the AST print functions and see if that gives the same output.
If that is the problem then ASTFrame can be patched to add a hidden 
root node or you can add a root node to your grammar.

Tom.
--- In antlr-interest at yahoogroups.com, "Joshua Davis" 
<joshua.davis at k...> wrote:
> Just a quick follow up.  The Parser did not build an AST because 
the 
> rule that had fired did not specify which token should be the root 
of 
> the sub-tree.  For example:
> 
> fromClause : FROM identifier (AS identifier)?
> 
> The AST builder seems to require that one of the tokens be 
designated 
> as the root.  I changed it to:
> 
> fromClause : FROM^ identifier (AS identifier)?
> 
> and it worked.
> 
> --- In antlr-interest at yahoogroups.com, "Joshua Davis" 
> <joshua.davis at k...> wrote:
> > My example is even simpler.  It's *supposed* to generate a node 
for 
> > every rule if the buildAST is set to true isn't it?  I was under 
> the 
> > impression that explicitly creating nodes is not necessary if 
> > buildAST is enabled.
> > 
> > --- In antlr-interest at yahoogroups.com, Gustav Boström 
> <i93gusbo at h...> 
> > wrote:
> > > Hi!
> > > 
> > > Thanks for the help!
> > > 
> > > I think I'm trying the same thing as you, but also without 
luck.
> > > I try to do:
> > > sql_stmt : 
> > > 	  sql_data_stmt  { #sql_stmt = #([SQL_STMT,"sql_stmt"], 
> > > #sql_stmt); }
> > > 
> > > , but I never find this node in the tree. The code seems to be 
> > > generated, but has no effect. Could it be that no root node is 
> ever 
> > > created?
> > > 
> > > I also tried this:
> > > sql_script : 
> > > 	 (sql_stmt)? { #sql_script = #
> > > ([Token.MIN_USER_TYPE,"sql_script"], #sql_script); } ( 
SEMICOLON 
> > > (sql_stmt)? )*
> > > 
> > > "sql_script" is the first rule and now a node is in fact 
created, 
> > but 
> > > it has no children. I thought that all the subsequent nodes 
would 
> > be 
> > > added as children automatically.
> > > 
> > > Qoute from doc:
> > > "Setting the return tree is very useful in combination with 
> normal 
> > > tree construction because you can have ANTLR do all the work 
of 
> > > building a tree and then add an imaginary root node such as: 
> > >   
> > > decl : ( TYPE ID )+
> > >        { #decl = #([DECL,"decl"], #decl); }
> > >      ;
> > > ANTLR allows you to assign to #rule anywhere within an 
> alternative 
> > of 
> > > the rule. ANTLR ensures that references of and assignments to 
> #rule 
> > > within an action force the parser's internal AST construction 
> > > variables into a stable state. After you assign to #rule, the 
> state 
> > > of the parser's automatic AST construction variables will be 
set 
> as 
> > > if ANTLR had generated the tree rooted at #rule. For example, 
any 
> > > children nodes added after the action will be added to the 
> children 
> > > of #rule." 
> > >  
> > > This is exactly what I want, but I must be doing something 
wrong.
> > > 
> > > 
> > > 
> > > /Gustav
> > > 
> > > 
> > > 
> > > --- In antlr-interest at yahoogroups.com, "Joshua Davis" 
> > > <joshua.davis at k...> wrote:
> > > > ANTLR newbie question:
> > > > 
> > > > I've been having a similar problem I think.  I made a very 
> simple 
> > > > grammar based on L. V.'s grammar.   It seems to be always 
> > producing 
> > > > only one root node, with no children.  What steps should I 
take 
> > to 
> > > > debug this?  I have tried adding actions to the rules, and 
it 
> > looks 
> > > > like the rules are being triggered correctly, but no AST is 
> > > produced.
> > > > 
> > > > --- In antlr-interest at yahoogroups.com, "lgcraymer" 
<lgc at m...> 
> > wrote:
> > > > > Gustav--
> > > > > 
> > > > > You cannot make a rule a root--that's what ANTLR is 
> complaining 
> > > > about. 
> > > > >  Rules may represent subtrees, so only nodes with token 
ids 
> > (TEXT 
> > > > or 
> > > > > STRING, for example) can be followed with ^.
> > > > > 
> > > > > --Loring
> > > > > 
> > > > > 
> > > > > --- In antlr-interest at yahoogroups.com, Gustav Boström 
> > > > <i93gusbo at h...> 
> > > > > wrote:
> > > > > > Hello!
> > > > > > 
> > > > > > I'm trying to use Lubos Vnuk's (Thanks for doing all the 
> > work!) 
> > > > SQL-
> > > > > > grammar in order to do some 
> > > > > > processing on SQL-statments. I would for example like to 
> find 
> > > out 
> > > > > > which columns are affected by a DML-statement and which 
> > > > parameters 
> > > > > > correspond to which columns.
> > > > > > 
> > > > > > I'm doing this is in Java , so I've changed the grammar 
> > > slightly.
> > > > > > I've removed the C++ actions or replaced them with Java 
> > > versions.
> > > > > > 
> > > > > > My thought was to add AST-construction and then navigate 
> the 
> > > tree 
> > > > to 
> > > > > > find out the stuff I need. This is where I run into 
> problems.
> > > > > > I tried to annotate the rules with ^ to construct my 
tree, 
> > but 
> > > it 
> > > > > > keeps giving me "unexpected token:^" when I try to 
dothis 
> for 
> > > > > example:
> > > > > > sql_stmt : 
> > > > > > 	  sql_data_stmt^ 
> > > > > > ;
> > > > > > 
> > > > > > I've set the options to buildAST=true;
> > > > > > I'm using version 2.7.2.
> > > > > > 
> > > > > > Any ideas on what can be wrong?
> > > > > > 
> > > > > > Cheers,
> > > > > > 
> > > > > > Gustav Boström


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list