简体   繁体   中英

ANTLR - Parsing different line types - mismatched input error

I am using ANTLR for writing grammar for ZOS JCL language. There can be 4 types of lines:

  • Type 1: All statements start with a '//' in the first 2 characters of the line
  • Type 2: Comments start with a '//*'
  • Type 3: lines starting with '/*'
  • Type 4: lines starting with any other pattern other than types 1, 2 and 3

Given below is the segment from my grammar file :

dd4:    JCLBEGIN ddname  DDWORD '*' inlinerec INLINESTMTEND?;

inlinerec: (INLINEDATA)+ ;
fragment INLINEDATA: (~[\r\n])*;
.
.
.
DDWORD:     'DD';
.
.
JCLBEGIN:       '//'    ;
COMMENTBEGIN:   '//*'   ;
INLINESTMTEND:  '/*'    ;
.
.
WS     : [\r\n] -> channel(HIDDEN);

when using AntlrWorks to run this grammar, the parser rule inlinerec is matched correctly but I get the following error:

line 24:0 mismatched input 'SORT' expecting INLINEDATA

and the section of my code where the error occurs is:

//SYSIN    DD  *                                      
SORT FIELDS=COPY
INCLUDE COND
/*  

How can I resolve this error ?

the parser rule inlinerec is matched correctly

It may look that way, but in reality, it is not matched correctly.

line 24:0 mismatched input 'SORT' expecting INLINEDATA

Fragment rules are not visible in parser rules (therefor INLINEDATA cannot be used in inlinerec ).

Simply removing the fragment keyword from INLINEDATA will also not work since that rule would greedily match any line. You will need to rethink your approach. I cannot give more specific advise since there is too little to go on. If you have follow-up questions, I suggest you create a new question here on SO with a SSCCE that exhibits the problem. Good luck!

Also see: https://github.com/antlr/antlr4/blob/master/doc/lexer-rules.md

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM