MESSAGE
DATE | 2016-04-09 |
FROM | Ruben Safir
|
SUBJECT | Re: [Hangout-NYLXS] Antlr visitor Programming - kicking it around
|
On 04/09/2016 09:19 AM, Christopher League wrote: > > So I'm only visiting visitAssignID and visitQuantity and > visitVariables? > > Not even visitQuantity, I think. The purpose of CollectVarsVisitor was > to "traverse a parse tree for the calculator language and just keep > track of the set of identifiers that are being referenced (assigned or > retrieved) in the program."
Right. At this point, however, I'm more interested in the mechanics of the problem than the solution to the example. There is not a visitStmt but there is a ctx.expr() as a method of an CalcParser.AssignIDContext instance. Is there a list of potential methods available to me, like gettext()?
Also, examine line
public Void visitPrint(CalcParser.PrintContext ctx)
I have ./CalcParser.java: public void copyFrom(StmtContext ctx) {
and
[ruben-at-manjaro calculator]$ grep StmtContext ./*java ./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()); ./CollectVarsVisitor_bak.java: public Void visitStmt(CalcParser.StmtContext ctx) {
In this case I couldn't grep CalcParse.PrintContext
and
./CalcBaseVisitor.java: -at-Override public T visitPrint(CalcParser.PrintContext ctx) { return visitChildren(ctx); }
[ruben-at-manjaro calculator]$ grep CalcParser.PrintContext ./*java ./CalcBaseListener.java: -at-Override public void enterPrint(CalcParser.PrintContext ctx) { } ./CalcBaseListener.java: -at-Override public void exitPrint(CalcParser.PrintContext ctx) { } ./CalcBaseVisitor.java: -at-Override public T visitPrint(CalcParser.PrintContext ctx) { return visitChildren(ctx); } ./CalcListener.java: void enterPrint(CalcParser.PrintContext ctx); ./CalcListener.java: void exitPrint(CalcParser.PrintContext ctx); ./CalcVisitor.java: T visitPrint(CalcParser.PrintContext ctx); ./CollectVarsVisitor.java: public Void visitPrint(CalcParser.PrintContext ctx) {
Ummm where is CalcParser.PrintContext defined and declared?
hmmm
./CalcParser.java: public static class StmtContext extends ParserRuleContext {
./CalcParser.java: public static class PrintContext extends StmtContext {
Correct?
Oh and remember Unix(GNU) is you f'ing IDE... ;)
need to build ctags..
> > If we look at the grammar: > stmt : ID '=' expr #assignID > | 'print' expr #print > ; > > expr : '-' expr #negative > | expr ('*' | '/') expr #products_quotents > | expr ('+'|'-') expr #sums > | '(' expr ')' #parans > |NUM #quantity > |ID #variables > ; > > The only place that a variable is assigned is in #assignID and the only > place a variable is retrieved is in #variables. So those are the only > two visit methods we'd need to fulfill the purpose of > CollectVarsVisitor. > > The AssignIDContext and VariablesContext will each have a ctx.ID() > because ID appears in both of those rules. And because it appears > exactly once in each, the method returns just a single TerminalNode, > rather than a list of them. The other contexts (tree nodes) don't have > any ID, so there is no ctx.ID() method in those. > > CL > > Ruben Safir [1]ruben-at-mrbrklyn.com writes: > > 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 :(' `|'`|'`|'')+ ->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 > > References > > 1. mailto:ruben-at-mrbrklyn.com > > > > _______________________________________________ > hangout mailing list > hangout-at-nylxs.com > http://www.nylxs.com/ >
-- 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/
|
|