MESSAGE
DATE | 2016-04-08 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Hangout-NYLXS] Antlr visitor Programming - kicking it around
|
So kicking this around, forget that this HW is now a month old...
I did this which fails, and I know why...I think
Note that it fails on contex objects that have no ID()
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(); //STMT -at-Override public Void visitAssignID(CalcParser.AssignIDContext ctx) { TerminalNode id = ctx.ID(); if(id != null) { System.out.println(id.getText()); vars.add(id.getText()); } return super.visitAssignID(ctx); } -at-Override public Void visitPrint(CalcParser.PrintContext ctx) { TerminalNode id = ctx.ID(); if(id != null) { System.out.println(id.getText()); vars.add(id.getText()); } return super.visitPrint(ctx); }
//EXPR
-at-Override public Void visitNegative(CalcParser.NegativeContext ctx) { TerminalNode id = ctx.ID(); if(id != null) { System.out.println("expresion: " + id.getText()); vars.add(id.getText()); } return super.visitNegative(ctx); }
-at-Override public Void visitProducts_quotents(CalcParser.Products_quotentsContext ctx) { TerminalNode id = ctx.ID(); if(id != null) { System.out.println("expresion: " + id.getText()); vars.add(id.getText()); } return super.visitProducts_quotents(ctx); }
-at-Override public Void visitSums(CalcParser.SumsContext ctx) { TerminalNode id = ctx.ID(); if(id != null) { System.out.println("expresion: " + id.getText()); vars.add(id.getText()); } return super.visitSums(ctx); }
-at-Override public Void visitParans(CalcParser.ParansContext ctx) { TerminalNode id = ctx.ID(); if(id != null) { System.out.println("expresion: " + id.getText()); vars.add(id.getText()); } return super.visitParans(ctx); }
-at-Override public Void visitQuantity(CalcParser.QuantityContext ctx) { TerminalNode id = ctx.ID(); if(id != null) { System.out.println("expresion: " + id.getText()); vars.add(id.getText()); } return super.visitQuantity(ctx); }
-at-Override public Void visitVariables(CalcParser.VariablesContext ctx) { TerminalNode id = ctx.ID(); if(id != null) { System.out.println("expresion: " + id.getText()); vars.add(id.getText()); } return super.visitVariables(ctx); } }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ruben-at-manjaro calculator]$ javac Main.java ./CollectVarsVisitor.java:25: error: cannot find symbol TerminalNode id = ctx.ID(); ^ symbol: method ID() location: variable ctx of type PrintContext ./CollectVarsVisitor.java:37: error: cannot find symbol TerminalNode id = ctx.ID(); ^ symbol: method ID() location: variable ctx of type NegativeContext ./CollectVarsVisitor.java:48: error: cannot find symbol TerminalNode id = ctx.ID(); ^ symbol: method ID() location: variable ctx of type Products_quotentsContext ./CollectVarsVisitor.java:58: error: cannot find symbol TerminalNode id = ctx.ID(); ^ symbol: method ID() location: variable ctx of type SumsContext ./CollectVarsVisitor.java:68: error: cannot find symbol TerminalNode id = ctx.ID(); ^ symbol: method ID() location: variable ctx of type ParansContext ./CollectVarsVisitor.java:78: error: cannot find symbol TerminalNode id = ctx.ID(); ^ symbol: method ID() location: variable ctx of type QuantityContext Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. 6 errors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cat 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 ;
So I'm only visiting visitAssignID and visitQuantity and visitVariables?
Ruben
-- 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/
|
|