Home > Software engineering >  antlr4 nodes without labels
antlr4 nodes without labels

Time:12-28

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"?

CodePudding user response:

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

  • Related