1

I am parsing the grammar with Antlr 4 (v4.9.2)

The following is the grammar:

grammar Test;

start
:
    command+ EOF
;
command: commandType params ;
commandType: 'DISPLAY' ;
params: param (',' param)*
    | ;//parameter list
param: ID;
ID : [a-z]+[a-z0-9]*
;
WS
:
    [ \t\r\n]+ -> skip
;

The input text for the grammar is:

DISPLAY hello, really, sdfdsf
DISPLAY one more now

The following is the parse tree generated. (start (command (commandType DISPLAY) (params (param hello) , (param really) , (param sdfdsf))) (command (commandType DISPLAY) (params (param one))) more now )

The nodes "more now" is not labelled.

Why the parser did not throw error for "more now"?

Mike Cargal
  • 6,610
  • 3
  • 21
  • 27

1 Answers1

1

It does produce an error, but ANTLR's default strategy is to (try to) recover from invalid input, and continue parsing.

This is what is displayed when trying to parse your input:

enter image description here

enter image description here

Try Google for "ANTLR error strategy", "ANTLR error handling", or look at this Q&A: Handling errors in ANTLR4

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Thanks a lot @Bart Kiers. This is awesome feature, which I didn't realise. – Arock Durai Dec 27 '21 at 19:14
  • Hi @Bart Kiers, how did you display the parse tree in GUI? any links or tool name. – Arock Durai Dec 27 '21 at 19:16
  • @ArockDurai I used IntelliJ with the ANTLR4 plugin: https://plugins.jetbrains.com/plugin/7358-antlr-v4 . Note that the plugin works with most of Jetbrains' IDE's (PyCharm, Rider, Webstorm, ...), not just with IntelliJ. – Bart Kiers Dec 27 '21 at 19:50
  • Oh, I’m a bit of a JetBrains fan-boy, but there’s also VS Code that has a plugin that looks really nice: https://marketplace.visualstudio.com/items?itemName=mike-lischke.vscode-antlr4 – Bart Kiers Dec 30 '21 at 22:58