gpt4 book ai didi

java - ANTLR - 输入错误不匹配

转载 作者:行者123 更新时间:2023-11-30 02:41:21 27 4
gpt4 key购买 nike

我的语法如下所示,由特定语言的注释和控制语句组成:

语法:

grammar DD;

ddlist: (ddstmt| jclcomment)+;

ddstmt: dd1 | dd2 | dd3 | dd4 ;

dd1: JCLBEGIN ddname DDWORD 'DUMMY';
dd2: JCLBEGIN ddname DDWORD 'DYNAM';
dd3: JCLBEGIN ddname DDWORD NAME'=' ('*'|NAME);
dd4: JCLBEGIN ddname DDWORD '*' inlinerec INLINESTMTEND?;

inlinerec: (INLINEDATA )+ ;
fragment INLINEDATA: (~[\r\n])*;

ddname: NAME;

jclcomment: JCLCOMMENT+;
JCLCOMMENT: COMMENTBEGIN ~[\r\n]*;

DDWORD: 'DD';

JCLBEGIN: '//' ;
COMMENTBEGIN: '//*' ;
INLINESTMTEND: '/*' ;

NAME : [A-Z#] (ALPHA | NUMBER | SPECIALCHARS)*;

NUMBER: [0-9];
ALPHA: [A-Z];
SPECIALCHARS: '#' | '@' | '$';

STRING
: '\'' (~[\r\n"])* '\''
| '"' (~[\r\n"])* '"'
;

WS : [ \r\n] -> channel(HIDDEN);

我的输入是这样的:

//SYSIN    DD  *                                      
SORT FIELDS=COPY
INCLUDE COND
/*
//SYSPRINT DD SYSOUT=*
//* Comment line #1
//* Comment line #2
//SYSOUT DD SYSOUT=*
//CEEDUMP DD SYSOUT=*
//* Comment line #3
//SYSUDUMP DD SYSOUT=A

当我使用 AntlrWorks 通过输入运行此语法时,出现以下错误:

line 2:0 mismatched input 'SORT' expecting INLINEDATA

如何解决此错误?

最佳答案

1) 空白规则生成大量标记:

$ echo $CLASSPATH
.:/usr/local/lib/antlr-4.6-complete.jar
$ alias grun
alias grun='java org.antlr.v4.gui.TestRig'
$ grun DD ddlist -tokens jcl.txt
[@6,11:12='DD',<'DD'>,1:11]
[@7,13:13=' ',<WS>,channel=1,1:13]
[@8,14:14=' ',<WS>,channel=1,1:14]
[@9,15:15='*',<'*'>,1:15]
[@10,16:16=' ',<WS>,channel=1,1:16]
[@11,17:17=' ',<WS>,channel=1,1:17]
[@12,18:18=' ',<WS>,channel=1,1:18]
[@13,19:19=' ',<WS>,channel=1,1:19]
[@14,20:20=' ',<WS>,channel=1,1:20]
[@15,21:21=' ',<WS>,channel=1,1:21]
[@16,22:22=' ',<WS>,channel=1,1:22]

您可以使用 + 修饰符(= 一个或多个)消耗单个标记中的所有连续空格:

WS     : [ \t\r\n]+ -> channel(HIDDEN);

[@3,11:12='DD',<'DD'>,1:11]
[@4,13:14=' ',<WS>,channel=1,1:13]
[@5,15:15='*',<'*'>,1:15]
[@6,16:54=' \n',<WS>,channel=1,1:16]
[@7,55:58='SORT',<NAME>,2:0]

.

2)您没有提到编译语法时出现的重要错误消息:

warning(125): DD.g4:12:12: implicit definition of token INLINEDATA in parser

在解析器中使用未定义的标记就好像您有词法分析器规则:

INLINEDATA : 'INLINEDATA' ;

这是一个字符串常量。因此解析器规则

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

意思是:我期望输入流是:

//{a name} DD * 'INLINEDATA'

但输入是:

//SYSIN    DD  *   SORT 

因此消息

line 2:0 mismatched input 'SORT' expecting INLINEDATA

.

3)我的这种作业控制语句的语法:

grammar JCL;

/* Parsing JCL, ignoring inline sysin. */

jcl
: jcl_card+ // good old punched cards :-)
;

jcl_card
: dd_statement
| COMMENT
;

dd_statement
: '//' NAME 'DD' file_type ( NL | EOF )
;

file_type
: 'DUMMY'
| 'DYNAM'
| NAME '=' ( '*' | NAME )
| '*' NL inline_sysin
;

inline_sysin
: NON_JCL_CARD* END_OF_FILE
;

NAME : [A-Z#] ( LETTER | DIGIT | SPECIAL_CHARS )* ;
COMMENT : '//*' .*? ( NL | EOF ) ;
END_OF_FILE : '/' {getCharPositionInLine() == 1}? '*' ;
NON_JCL_CARD : ~'/' {getCharPositionInLine() == 1}? .*? ( NL | EOF ) ;
STRING : '\'' .*? '\'' | '"' .*? '"' ;
NL : [\r\n] ;
WS : [ \t]+ -> skip ; // or -> channel(HIDDEN) to keep white space tokens

fragment DIGIT : [0-9] ;
fragment LETTER : [A-Z] ;
fragment SPECIAL_CHARS : '#' | '@' | '$' ;

随着输入

//SYSIN    DD  *     
SORT FIELDS=COPY
INCLUDE COND
any other program input @ $ ! & %
/*
//SYSPRINT DD SYSOUT=*
//* Comment line #1
//* Comment line #2
//SYSOUT DD SYSOUT=*
//SYSOUT DD DUMMY
//SYSIN DD *
/* not end of input
/*

它给出

$ grun JCL jcl -tokens jcl.txt
[@0,0:1='//',<'//'>,1:0]
[@1,2:6='SYSIN',<NAME>,1:2]
[@2,11:12='DD',<'DD'>,1:11]
[@3,15:15='*',<'*'>,1:15]
[@4,21:21='\n',<NL>,1:21]
[@5,22:38='SORT FIELDS=COPY\n',<NON_JCL_CARD>,2:0]
[@6,39:51='INCLUDE COND\n',<NON_JCL_CARD>,3:0]
[@7,52:85='any other program input @ $ ! & %\n',<NON_JCL_CARD>,4:0]
[@8,86:87='/*',<END_OF_FILE>,5:0]
[@9,106:106='\n',<NL>,5:20]
...
@17,131:161='//* Comment line #1 \n',<COMMENT>,7:0]
...
[@31,232:233='//',<'//'>,11:0]
[@32,234:238='SYSIN',<NAME>,11:2]
[@33,243:244='DD',<'DD'>,11:11]
[@34,247:247='*',<'*'>,11:15]
[@35,253:253='\n',<NL>,11:21]
[@36,254:278=' /* not end of input \n',<NON_JCL_CARD>,12:0]
[@37,279:280='/*',<END_OF_FILE>,13:0]
[@38,281:280='<EOF>',<EOF>,13:2]

.

尝试使用 -gui 选项来显示解析树:

$ grun JCL jcl -gui jcl.txt

关于java - ANTLR - 输入错误不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41592639/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com