[antlr-interest] rule != token? And Atomic rules... 2 questions + included grammar

Dave Elliot symbioid at gmail.com
Wed Jan 16 12:07:03 PST 2008


Greetings!  Kay found one of my blog posts and pointed me this way, so
I figured I'd best join this community :)

I have a couple questions, I'm taking posting my source code
management commit notes:



 1 error listed still. Had 3.
 e.ast_node not recognized.
 I shouldn't be using ast_node with tokens, only rules.
 I converted tokens to merely "return new ASTNode();" (as we did for
atom), and this gets rid of 2 errors.
 I left the old actions as comments (below the lines they affect).


 However, there still seems to be something registering as a token,
but I'm not sure what.
 It isn't the "atom" alternative in the factor rule, in fact,
 that may need to be set back to the full addChild actions,
 as it doesn't affect the errors either way.


 The errors generated are for the rongoParser.java, not the grammar
file itself, so I don't see lines.
 However, based upon the number of the remaining error compared to the
ones I fixed,
 it appears the error is coming up earlier rather than later in the
code. Possibly above the "io" rule.

Also:
 Why, in the "io" rule, do I have to do "LPAREN e = (expr) RPAREN"
 but in "factor" I have to do "LPAREN e = expr RPAREN"???
 If I do vice versa for either, I find I get a "Token!=Rule" or some
such thing.

Below is the grammar:  (I apologize, I copied it from track, so you
see the line numbers and such)
Next time, I can just work with the relevant parts, but figured the
full grammar so far is the best to deal with.  If anyone needs
clarification of my questions (that is, I don't have the error info
here, nor the rongoParser code to help detect any problem there),
please ask, and I can try to get more detailed info tonight if
necessary.

Thanks for any help!

Dave

------------------------
1 grammar rongo;
2

3 @header {
4 package org.rongo;
5 import java.util.HashMap;
6 import org.rongo.ASTNode;
7 import java.util.ArrayList;
8 }
9

10 @lexer::header { package org.rongo; }
11

12 @members {
13 /* Map variable name to Integer object holding value */
14 HashMap memory = new HashMap();
15 ASTNode root;
16 ArrayList alist = new ArrayList();
17 }
18

19 prog returns[ASTNode ast_node]
20         :       e = stat+ {ASTNode myNode=new ASTNode();
myNode.addChild($e.ast_node); myNode.visit(); alist.add(myNode); root
= myNode;}
21         ;
22

23 stat returns [ASTNode ast_node]
24         :       e = type_decl {ASTNode myNode=new ASTNode();
myNode.addChild($e.ast_node); myNode.visit(); alist.add(myNode);
return myNode;} NEWLINE
25         |       e = expr {ASTNode myNode=new ASTNode();
myNode.addChild($e.ast_node); myNode.visit(); alist.add(myNode);
return myNode;} NEWLINE
26         |       e = io {ASTNode myNode=new ASTNode();
myNode.addChild($e.ast_node); myNode.visit(); alist.add(myNode);
return myNode;} NEWLINE
27         ;
28
29 io returns [ASTNode ast_node]
30         :       'print' LPAREN e = (expr) RPAREN {ASTNode
myNode=new ASTNode(); myNode.addChild($e.ast_node); myNode.visit();
alist.add(myNode); return myNode;}
31         |       'read' LPAREN e = ID RPAREN {return new ASTNode();}
32         // {ASTNode myNode=new ASTNode();
myNode.addChild($e.ast_node); myNode.visit(); alist.add(myNode);
return myNode;}
33         ;
34
35 type_decl returns [ASTNode ast_node]
36         :       'int:' e = ID (EQUALS INT)?     {return new ASTNode();}
37 //      {ASTNode myNode=new ASTNode();
myNode.addChild($e.ast_node); myNode.visit(); alist.add(myNode);
return myNode;}
38         ;
39
40 expr returns[ASTNode ast_node]
41         :       e = mexpr ((PLUS|MINUS) mexpr)* {ASTNode myNode=new
ASTNode(); myNode.addChild($e.ast_node); myNode.visit();
alist.add(myNode); return myNode;}
42         ;
43
44 mexpr returns[ASTNode ast_node]
45         :       e = factor  ((TIMES|DIV) factor)* {ASTNode
myNode=new ASTNode(); myNode.addChild($e.ast_node); myNode.visit();
alist.add(myNode); return myNode;}
46         ;
47

48 factor returns[ASTNode ast_node]
49         :       LPAREN e = expr RPAREN {ASTNode myNode=new
ASTNode(); myNode.addChild($e.ast_node); myNode.visit();
alist.add(myNode); return myNode;}
50         |       e = atom {return new ASTNode();}
51         //{ASTNode myNode=new ASTNode();
myNode.addChild($e.ast_node); myNode.visit(); alist.add(myNode);
return myNode;}
52         ;
53
54 atom returns[ASTNode ast_node]
55         :       INT {return new ASTNode();}
56         |       ID {return new ASTNode(); }
57         ;
58
59 WS      : (' '  | '\t')+ {skip();}
60         ;
61 NEWLINE
62         :       ( '\r\n'
63         |       '\r'
64         |       '\n')
65         ;
66

67 COMMENT :       '//' (~('\n'|'\r'|'\r\n'))*
68 {skip();}
69 ;
70

71 INT             :       ('0'..'9')+;
72 PLUS    :       '+';
73 MINUS   :       '-';
74 TIMES   :       '*';
75 DIV             :       '/';
76 LPAREN  :       '(';
77 RPAREN  :       ')';
78 ID              :       ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9')*;
79 COLON   :       ':';
80 EQUALS  :       '=';


More information about the antlr-interest mailing list