ANTLR/C#: The Visitor class doesn't get created?

506 views
Skip to first unread message

M B

unread,
Aug 26, 2021, 8:49:21 AM8/26/21
to antlr-discussion

Hello,

Maybe I misunderstood something. I don't get a Visitor class, only a BaseVisitor and an IVisitor despite using the -visitor option.

I followed the steps there and there.

Details :
I've created the most basic .g4 file, then run antlr4 with the -visitor option. I don't use any UI for that, it's straight from the command line.

It creates those files :
  • MyGrammarBaseVisitor.cs
  • MyGrammarVisitor.cs
Then in my program I try this :
                ...
                MyGrammarVisitor visitor = new MyGrammarVisitor();
                ...

The class MyGrammarVisitor is not recognized. MyGrammarVisitor.cs only contains the interface : IMyGrammarVisitor

What am I missing? Am I supposed to implement the Visitor class myself? If so, then it's not mentioned clearly in the tutorials, it sounds like it's only if you want to change the way the tree gets traversed.

Regards,



M B

unread,
Aug 26, 2021, 9:38:58 AM8/26/21
to antlr-discussion
OK so apparently I was wrong. I'm supposed to create my own Visitor class and override at least the basic methods.
Sorry!

M Berg

unread,
Aug 31, 2021, 10:35:22 AM8/31/21
to antlr-di...@googlegroups.com

System : Windows 10
Terminal : Command line


Scenario :

  1. Created all the CLASPATH, PATH and other aliases
  2. Created simple grammar (see below)
  3. Generated it : antlr4 -visitor -no-listener -o ./outputJava -Dlanguage=Java MyGrammar.g4
  4. Built it in the output folder : javac MyGrammar*.java
  5. Ran this :
    grun SectraGrammar line -tree
  6. I can now type whatever I want into the Terminal. Unlike other noobs, I do type something.
  7. I press return. Nothing happens. Silly me, unlike more noobs I do press Ctrl+D
  8. ^D appears in the Terminal.
  9. PROBLEM: Nothing else gets displayed. No tree, no message.
  10. SAME ISSUE with -gui instead of -tree. No reaction from grun.


Grammar :

grammar MyGrammar;

line : instruction ;
instruction : 
      QUIT
    | operation ;

operation  : 
    SYMBOL OPERATOR NUMBER_INT 
    | SYMBOL OPERATOR SYMBOL;

QUIT            : Q U I T;

NUMBER_INT     : [0-9]+ ;
SYMBOL         : [a-zA-Z0-9]+ ;

OPERATOR       : 
    A D D ;

WS                  : (' '|'t'|'r'? 'n' | 'r')+ -> skip ;


fragment A:('a'|'A');
fragment B:('b'|'B');
...

Susan Jolly

unread,
Aug 31, 2021, 11:56:24 AM8/31/21
to antlr-di...@googlegroups.com
Try Ctrl+z

Works for me in Windows 10 command window

HTH, SJ
Reply all
Reply to author
Forward
0 new messages