1

I have a simple grammar like :

grammar CellMath;

equation : expr EOF;

expr 
    :  '-'expr                  #UnaryNegation      // unary minus
    |   expr op=('*'|'/') expr  #MultiplicativeOp   // MultiplicativeOperation
    |   expr op=('+'|'-') expr  #AdditiveOp         // AdditiveOperation
    |   FLOAT                   #Float              // Floating Point Number
    |   INT                     #Integer            // Integer Number
    |   '(' expr ')'            #ParenExpr          // Parenthesized Expression
    ;

MUL :   '*' ;
DIV :   '/' ;
ADD :   '+' ;
SUB :   '-' ;

FLOAT
    :   DIGIT+ '.' DIGIT*
    |   '.' DIGIT+ 
    ;

INT :    DIGIT+ ;

fragment
DIGIT : [0-9] ; // match single digit

//fragment
//ATSIGN : [@];

WS :     [ \t\r\n]+ -> skip ;

ERRORCHAR : . ;

Not able to throw an exception in case of special char in between expression

[{Number}{SPLChar}{Chars}] Ex: "123@abc", "123&abc".

I expecting an exception to throw

For Example: Input stream : 123@abc Just like in ANTLR labs Image

But in my case Output : '123' without any errors

I'm using Listener pattern, Error nodes are just ignored not going through VisitTerminal([NotNull] ITerminalNode node) / VisitErrorNode([NotNull] IErrorNode node) in the BaseListener class. Also all the BaseErrorListener class methods has been overridden not even there.

Thanks in advance for your help.

  • What target is this? CSharp or Java or what? Note, there's no 4.6.1 Antlr Tool .jar file in the downloads directory. https://github.com/antlr/website-antlr4/tree/gh-pages/download , in Maven Central, only in Nuget. – kaby76 Dec 01 '22 at 16:08
  • Cannot reproduce with Antlr4.Runtime.Standard v4.6, CSharp target. (Defined the VisitErrorNode() method in your listener class. `public class Foo : ArithmeticBaseListener { public override void VisitErrorNode([NotNull] IErrorNode node) { System.Console.WriteLine("Err node "+ node.GetText()); } }` and walked with `var vis = new Foo(); var w = new ParseTreeWalker(); w.Walk(vis, tree);`.) – kaby76 Dec 01 '22 at 17:04
  • @kaby76 Target is Csharp – Hemanth Desu Dec 02 '22 at 08:26
  • @kaby76 - [![Version of Antlr](https://i.stack.imgur.com/fRCuM.png)](https://i.stack.imgur.com/fRCuM.png) – Hemanth Desu Dec 02 '22 at 08:42
  • @kaby76 Here Parser itself not generating nodes/errors for me. [Sample code for issue](https://i.stack.imgur.com/ilxoT.png)] In this image 'CellMathParser' is the Antlr generated class. – Hemanth Desu Dec 02 '22 at 10:23
  • Here's what it should look like. https://github.com/kaby76/so74642814 The code works, error nodes are printed. `dotnet build; ./bin/Debug/net6.0/Test.exe -input '123@abc' -tokens`. Also, you should be using the official Antlr4 tool and code, not an unsupported fork--and a 6 year old version of it at that. – kaby76 Dec 02 '22 at 12:04
  • Here is the solution. Instead of using **parser.expr()** replaced it with **parser.equation()** without updating Antlr version. Now I see all the nodes that are supposed to generate. – Hemanth Desu Dec 06 '22 at 16:18
  • Unless you know what you are doing, always start the parse using a rule which has an EOF symbol at the end of the rule. – kaby76 Dec 06 '22 at 16:46

0 Answers0