MESSAGE
DATE | 2016-04-08 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Hangout-NYLXS] visitor programming
|
Good Day
I was working on a project involving visitors and I had
part of this working and then made a minor change
which seemed to change the functionality of antlr
and I'm confused now and would appreciate some feedback
I can a simple gammer I called Calc.g4
grammar Calc;
prog : (stmt ';')*
;
stmt : ID '=' expr #assignID
| 'print' expr #print
;
expr : '-' expr #negative
| expr ('*' | '/') expr #products_quotents
| expr ('+'|'-') expr #sums
| '(' expr ')' #parans
|NUM #quantity
|ID #variables
;
NUM : [0-9]+ ('.' [0-9]+)?
;
ID :[a-zA-Z]+
;
WS :(' '|'\n'|'\t'|'\r')+ ->skip
;
I ran antlr4 -visit and produced all the various files.
I then wrote a Class called Main.java
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import java.util.HashSet;
public class Main {
¦public static void main(String[] args) {
¦ANTLRInputStream input = new ANTLRInputStream("y = 6*4 + 9 /8 / 2.5 + -3.21 * RUBEN; C = 5/9 * F + 32;");
¦CalcLexer lexer = new CalcLexer(input);
¦CommonTokenStream tokens = new CommonTokenStream(lexer);
¦CalcParser parser = new CalcParser(tokens);
¦CollectVarsVisitor my_visitor = new CollectVarsVisitor();
// parser.prog();
¦ParseTree tree = parser.prog();
¦tree.accept(my_visitor);
¦ ¦System.out.println(my_visitor.vars);
¦}
}
and an accompanying class CollectVarsVisitor.java
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import java.util.HashSet;
public class CollectVarsVisitor extends CalcBaseVisitor {
¦ HashSet vars = new HashSet();
-at-Override
¦ public Void visitStmt(CalcParser.StmtContext ctx) {
TerminalNode id = ctx.ID();
¦ ¦if(id != null) {
¦ System.out.println(id.getText());
¦ ¦ ¦ vars.add(id.getText());
¦ ¦}
¦ ¦return super.visitStmt(ctx);
¦ }
¦ -at-Override
¦ public Void visitExpr(CalcParser.ExprContext ctx) {
¦ ¦ TerminalNode id = ctx.ID();
¦ ¦ if(id != null) {
¦ System.out.println("expresion: " + id.getText());
¦ ¦ ¦ vars.add(id.getText());
¦ ¦ }
¦ ¦ return super.visitExpr(ctx);
¦ }
¦}
Now this compiled before, but not it seems to not to work
and it can't get line 11 in CollectVarsVisitor. I thought
I messed up my Java Environment but after grepping the
directory I see LOW and BEHOLD, no visitStmt as I expected.
[ruben-at-manjaro calculator]$ grep Stmt ./*
Binary file ./CalcParser$AssignIDContext.class matches
Binary file ./CalcParser.class matches
./CalcParser.java: public List stmt() {
./CalcParser.java: return getRuleContexts(StmtContext.class);
./CalcParser.java: public StmtContext stmt(int i) {
./CalcParser.java: return getRuleContext(StmtContext.class,i);
./CalcParser.java: public static class StmtContext extends ParserRuleContext {
./CalcParser.java: public StmtContext(ParserRuleContext parent, int invokingState) {
./CalcParser.java: public StmtContext() { }
./CalcParser.java: public void copyFrom(StmtContext ctx) {
./CalcParser.java: public static class PrintContext extends StmtContext {
./CalcParser.java: public PrintContext(StmtContext ctx) { copyFrom(ctx); }
./CalcParser.java: public static class AssignIDContext extends StmtContext {
./CalcParser.java: public AssignIDContext(StmtContext ctx) { copyFrom(ctx); }
./CalcParser.java: public final StmtContext stmt() throws RecognitionException {
./CalcParser.java: StmtContext _localctx = new StmtContext(_ctx, getState());
Binary file ./CalcParser$PrintContext.class matches
Binary file ./CalcParser$ProgContext.class matches
Binary file ./CalcParser$StmtContext.class matches
./CollectVarsVisitor.java: public Void visitStmt(CalcParser.StmtContext ctx) {
./CollectVarsVisitor.java: return super.visitStmt(ctx);
Instead my 'comments' ended up as visitor methods. I know they tag the grammer in
the grun generated diagrams. I didn't know they had this affect.
[ruben-at-manjaro calculator]$ grep visitor ./*java
./CalcBaseVisitor.java: * which can be extended to create a visitor which only needs to handle a subset
./CalcParser.java: public T accept(ParseTreeVisitor extends T> visitor) {
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitProg(this);
./CalcParser.java: else return visitor.visitChildren(this);
./CalcParser.java: public T accept(ParseTreeVisitor extends T> visitor) {
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitPrint(this);
./CalcParser.java: else return visitor.visitChildren(this);
./CalcParser.java: public T accept(ParseTreeVisitor extends T> visitor) {
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitAssignID(this);
./CalcParser.java: else return visitor.visitChildren(this);
./CalcParser.java: public T accept(ParseTreeVisitor extends T> visitor) {
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitNegative(this);
./CalcParser.java: else return visitor.visitChildren(this);
./CalcParser.java: public T accept(ParseTreeVisitor extends T> visitor) {
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitVariables(this);
./CalcParser.java: else return visitor.visitChildren(this);
./CalcParser.java: public T accept(ParseTreeVisitor extends T> visitor) {
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitProducts_quotents(this);
./CalcParser.java: else return visitor.visitChildren(this);
./CalcParser.java: public T accept(ParseTreeVisitor extends T> visitor) {
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitQuantity(this);
./CalcParser.java: else return visitor.visitChildren(this);
./CalcParser.java: public T accept(ParseTreeVisitor extends T> visitor) {
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitSums(this);
./CalcParser.java: else return visitor.visitChildren(this);
./CalcParser.java: public T accept(ParseTreeVisitor extends T> visitor) {
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitParans(this);
./CalcParser.java: if ( visitor instanceof CalcVisitor ) return ((CalcVisitor extends T>)visitor).visitParans(this);
./CalcParser.java: else return visitor.visitChildren(this);
./CalcVisitor.java: * This interface defines a complete generic visitor for a parse tree produced
./CalcVisitor.java: * -at-return the visitor result
./CalcVisitor.java: * -at-return the visitor result
./CalcVisitor.java: * -at-return the visitor result
./CalcVisitor.java: * -at-return the visitor result
./CalcVisitor.java: * -at-return the visitor result
./CalcVisitor.java: * -at-return the visitor result
./CalcVisitor.java: * -at-return the visitor result
./CalcVisitor.java: * -at-return the visitor result
./CalcVisitor.java: * -at-return the visitor result
./Main.java: CollectVarsVisitor my_visitor = new CollectVarsVisitor();
./Main.java: tree.accept(my_visitor);
./Main.java: System.out.println(my_visitor.vars);
There is no visitorStmt, only visitPrint etal.
Is that observation correct? How can I walk through stmt then?
--
So many immigrant groups have swept through our town
that Brooklyn, like Atlantis, reaches mythological
proportions in the mind of the world - RI Safir 1998
http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
http://www.nylxs.com - Leadership Development in Free Software
http://www2.mrbrklyn.com/resources - Unpublished Archive
http://www.coinhangout.com - coins!
http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps,
but incompatible with living as a free human being. -RI Safir 2013
_______________________________________________
hangout mailing list
hangout-at-nylxs.com
http://www.nylxs.com/
|
|