3

For the question and the grammar suggested by @BartKiers (Thank you!), I added the options block to specify the output to be

options{
language=Java;
output=AST;
ASTLabelType=CommonTree;
}

However, I am not able to figure out how to access the output i.e. AST. I need to traverse through the tree and process each operation that was specified in the input.


Using your example here, I am trying to implement rules returning values. However, I am running into following errors:

relational    returns [String val]                   
        :  STRINGVALUE ((operator)^ term)?
            {val = $STRINGVALUE.text + $operator.text + $term.text; }
                                    ;

term returns [String rhsOperand]                    
        :  QUOTEDSTRINGVALUE  {rhsOperand = $QUOTEDSTRINGVALUE.text;}
                                    |  NUMBERVALUE               {rhsOperand = $NUMBERVALUE.text; }
                                    | '(' condition ')'
                                     ;

Compilation Error:

Checking Grammar RuleGrammarParser.g...
\output\RuleGrammarParser.java:495: cannot find symbol
symbol  : variable val
location: class RuleGrammarParser
            val = (STRINGVALUE7!=null?STRINGVALUE7.getText():null) + (operator8!=null?input.toString(operator8.start,operator8.stop):null) + (term9!=null?input.toString(term9.start,term9.stop):null); 
            ^
\output\RuleGrammarParser.java:612: cannot find symbol
symbol  : variable rhsOperand
location: class RuleGrammarParser
                    rhsOperand = (QUOTEDSTRINGVALUE10!=null?QUOTEDSTRINGVALUE10.getText():null);
                    ^
\output\RuleGrammarParser.java:632: cannot find symbol
symbol  : variable rhsOperand
location: class RuleGrammarParser
                    rhsOperand = (NUMBERVALUE11!=null?NUMBERVALUE11.getText():null); 
                    ^
3 errors

Can you please help me understand why this fails to compiler?


Added the pastebin: http://pastebin.com/u1Bv3L0A

Community
  • 1
  • 1
name_masked
  • 9,544
  • 41
  • 118
  • 172

1 Answers1

4

By simply adding output=AST to the options section you don't create a AST, but a flat, 1 dimensional list of tokens. To mark certain tokens as root (or children), you need to do a bit of work.

Checkout this answer which explains how to create a proper AST and get access to the tree the parser then produces (the CommonTree tree in the main method of the answer I mentioned).

Note that you can safely remove language=Java;: by default the target language is Java (no harm in leaving it there though).

Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • That is awesome! three cheers for @BartKiers !! (& that rhymed :) ) – name_masked Mar 02 '12 at 17:23
  • Can you please help me out with my query above? I have updated the question. – name_masked Mar 02 '12 at 22:00
  • Bart, found the fix. Maybe you have to update your example to include `$` for all variables defined in the rule. – name_masked Mar 02 '12 at 22:45
  • @darkie15, yeah, in early version of ANTLR 3, you could safely omit the `$`, which is now mandatory in ANTLR 3.4 (and perhaps 3.3 as well). Note that in the tutorial I provide ANTLR 3.2, which works with all code posted in the tutorial, so I don't think I'll be changing much: I might break something accidentally. Glad to hear you solved your problem, of course. – Bart Kiers Mar 03 '12 at 06:52
  • I am stuck again :(. I have attached the pastebin link in my post. Basically, I am trying to get the condition as a return value once the parser has validated the input. So for an input like `condition1` where `condition1` might be `NAME = "BATMAN"`, I would get the return value, but for multiple conditions like `NAME = "BATMAN" AND SSN = 0000`, the return value is only the 1st condition and not the second one. Can you please help me out – name_masked Mar 03 '12 at 22:17
  • @darkie15, not quite sure what you're asking. Better you create a new question and post the code and a decent explanation there, no external links (pastebin) please. – Bart Kiers Mar 03 '12 at 22:26