Parsing ANTLR4解析Wiktionary文章时异常失败

Parsing ANTLR4解析Wiktionary文章时异常失败,parsing,antlr,mediawiki,antlr4,text-parsing,Parsing,Antlr,Mediawiki,Antlr4,Text Parsing,我正在尝试解析mediawiki标记,特别是英文维基词典文章中使用的标记。 它不是一种编程语言,对空格和换行符的处理有点奇怪,而且我觉得每一步都是尝试和(很多)错误 以下是回购协议: 测试输入文件是pie文章: () 注意:我正在解析wiktionary的整个XML转储,因此我宁愿找到一个使用Antlr解析的解决方案,而不是像使用一些在线API那样得到建议 wikitext.g4 grammar wikitext; /** Grammar */ page: EOL? ((wikitem

我正在尝试解析mediawiki标记,特别是英文维基词典文章中使用的标记。
它不是一种编程语言,对空格和换行符的处理有点奇怪,而且我觉得每一步都是尝试和(很多)错误

以下是回购协议:

测试输入文件是pie文章:
()

注意:我正在解析wiktionary的整个XML转储,因此我宁愿找到一个使用Antlr解析的解决方案,而不是像使用一些在线API那样得到建议

wikitext.g4

grammar wikitext;

/**
 Grammar
 */

page: EOL? ((wikitem | bullet_line) EOL? )+ EOF;

wikitem:
      wikitem wikitem
    | title 
    | template
    | link
    | text
    ;

title: title2 | title3 | title4 | title5;
title5: '=====' text '=====';
title4: '====' text '====';
title3: '===' text '===';
title2: '==' text '==';

template: '{{' parameter ('|' parameter)* '}}';
link: '[[' parameter ('|' parameter)* ']]';

parameter: wikitem?; // parameter can be empty, I.E. {{a|}}

bullet: ('*'|'#'|'#:'|'#*');
bullet_line: WS? EOL WS? bullet WS? wikitem;

text: (CHAR | WS)+;

/**
 Lexicon
 */
EOL: [\f\r\n]+;
CHAR: ~[ \t\f\r\n];
WS: [ \t]+;  
错误:

> cd ./java && grun wikitext page -gui ../data/pie.txt

line 190:137 no viable alternative at input 'rom {{inh|en|enm|pye}}, from {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'om {{inh|en|enm|pye}}, from {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'm {{inh|en|enm|pye}}, from {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' {{inh|en|enm|pye}}, from {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' from {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'from {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'rom {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'om {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'm {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' {{der|en|fro|pie}}, from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'from {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'rom {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'om {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'm {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' {{der|en|la|pīca}}, feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'feminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'eminine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'minine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'inine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'nine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'ine of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'ne of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'e of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'of {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'f {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' {{m|la|pīcus||woodpecker}}, from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'from {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'rom {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'om {{der|en|ine-pro|*'
line 190:137 no viable alternative at input 'm {{der|en|ine-pro|*'
line 190:137 no viable alternative at input ' {{der|en|ine-pro|*'
line 190:137 extraneous input '*' expecting {'|', '}}'}
line 190:146 no viable alternative at input 's)peyk-|'
line 190:146 no viable alternative at input ')peyk-|'
line 190:146 no viable alternative at input 'peyk-|'
line 190:146 no viable alternative at input 'eyk-|'
line 190:146 no viable alternative at input 'yk-|'
line 190:146 no viable alternative at input 'k-|'
line 190:146 no viable alternative at input '-|'
line 190:146 mismatched input '|' expecting {<EOF>, '=====', '====', '===', '==', '{{', '[[', EOL, CHAR, WS}
>cd./java&&grun wikitext页-gui../data/pie.txt
第190行:137在输入‘rom{inh{en}enm{pye},从{der{en}fro{pie},从{der}en | la | pīca},从{m | la | p | cus cus},从{der | de | en | pro
第190行:137在输入“om{inh|en|enm|pye},从{der|en|fro|pie},从{der|en|la|p|ca},从{m| la|p|cus cus |啄woodpecker},从{der|en | pro 124;
第190行:137在输入'm{inh|en|enm|pye},从{der|en|fro|pie},从{der|en|la|p|ca},从{m|la|p|cus cus}啄woodpecker},从{der|en | pro 124;
第190行:137在输入{{inh|en|enm|pye}、从{der|en | fro | pie}、从{der|en | la | p|ca}、从{m| la | p|cus cus |啄woodpecker}、从{der | en | pro ine}中没有可行的替代方案
第190行:137输入“从{der|en|fro|pie}到{{der|en|la | pīca},{m|la | p|cus |啄木鸟}的女性,从{der | en | ine pro}到}没有可行的替代方案
第190行:137输入“从{der|en|fro|pie}到{{der|en|la | pīca},{m|la | p|cus |啄木鸟}的女性,从{der | en | ine pro}到}没有可行的替代方案
第190行:137在输入“rom{der{en}fro{pie}时没有可行的替代方案,来自{der{en}la | pīca},{m | la | pīcus |啄木鸟}的女性,来自{der | en | ine pro 124;*”
第190行:137输入“om{der|en | fro | pie}没有可行的替代方案,来自{der|en | la | pīca},{m | la | p | cus |啄木鸟}的女性,来自{der | en | ine pro}”
第190行:137从{der|en|fro|pie}输入,从{der|en | la | pīca}输入,从{der | en | en | en | ine pro |}输入,没有可行的替代方案
第190行:137输入{{der|en|fro|pie}没有可行的替代方案,输入{{der|en | la | pīca},输入{m | la | p| cus |啄木鸟}的女性,输入{der|en | en | ine pro 124;*}
第190行:137“从{der|en|la|pīca}输入,{m|la | pīcus |啄木鸟}的女性,从{der | en | ine pro |*”输入没有可行的替代方案
第190行:137“从{der|en|la|pīca}输入,{m|la | pīcus |啄木鸟}的女性,从{der | en | ine pro |*”输入没有可行的替代方案
第190行:137从{der|en | la | pīca}输入{m | la | pīcus |啄木鸟}的女性,没有可行的替代方案
第190行:137从{der|en | la | pīca}输入{m | la | pīcus |啄木鸟}的女性,从{der | en | ine pro |*'输入“om{der | en | la | pīca}”没有可行的替代方案
第190行:137从{der|en|la|pīca}输入的{m|la | pīcus |啄木鸟}没有可行的替代方案,来自{der|en | ine pro |*'
第190行:137从{der|en | la | pīca}输入{{m | la | pīcus |啄木鸟}到{der | en | ine pro |*}输入{{{der|en | laīca}没有可行的替代方案
第190行:137在输入{m|la | pīcus |啄木鸟}时没有可行的替代方案,来自{der | en | ine pro |*'
第190行:137在输入{m|la | pīcus |啄木鸟}时没有可行的替代方案,来自{der | en | ine pro |*'
第190行:137在{m|la | p|cus |啄木鸟}的输入“eminine of{m|la | p|cus |啄木鸟}”中没有可行的替代方案,来自{der | en | ine pro |*'
第190行:137从{der | en | ine pro |*'输入{{m | la | p | cus |啄木鸟}没有可行的替代方案
第190行:137在{m | la | p|cus | |啄木鸟}的输入“inine”,从{der | en | ine pro |*”中没有可行的替代方案
第190行:137从{der | en | ine pro |*输入的{m | la | pīcus |啄木鸟}中的九个没有可行的替代方案
第190行:137在{m | la | pīcus |啄木鸟}的输入“ine”处没有可行的替代方案,来自{der | en | ine pro |*”
第190行:137在{m | la | pīcus |啄木鸟}的输入'ne处,从{der | en | ine pro |*'没有可行的替代方案
第190行:137在{m | la | pīcus |啄木鸟}的输入'e处没有可行的替代方案,来自{der | en | ine pro |*'
第190行:137在{m|la | pīcus | |啄木鸟}的输入处没有可行的替代方案,来自{der | en | ine pro |*'
第190行:137在{m|la | pīcus | |啄木鸟}的输入处没有可行的替代方案,来自{der | en | ine pro |*'
第190行:137输入“f{m | la | pīcus | | | |啄木鸟}没有可行的替代方案,来自{der | en | ine pro |*”
第190行:137在输入{{m| la | pīcus |啄木鸟}处,从{der | en | ine pro |*'没有可行的替代方案
第190行:137“来自{der | en | ine pro |*”的输入没有可行的替代方案
第190行:137“来自{der | en | ine pro |*”的输入没有可行的替代方案
第190行:137在输入“rom{{der | en | ine pro |*”处没有可行的替代方案
第190行:137输入“om{{der | en | ine pro |*”没有可行的替代方案
第190行:137在输入“m{{der|en | ine pro |*”处没有可行的替代方案
第190行:137在输入“{{der | en | ine pro |*”处没有可行的替代方案
第190行:137外部输入'*'应为{'|','}}
第190行:146在输入(peyk-|处没有可行的替代方案
第190行:146输入“peyk-|”时没有可行的替代方案
第190行:146在输入“peyk-|”处没有可行的替代方案
第190行:146在输入“eyk-|”处没有可行的替代方案
第190行:146在输入“yk-|”处没有可行的替代方案
第190行:146在输入“k-|”处没有可行的替代方案
第190行:146输入'-|'
第190行:146不匹配的输入“|”应为{,===”,“==”,“==”,“==”,“==”,“{,”[[”,EOL,CHAR,WS}

我更改了一些规则。你能检查一下吗

grammar wikitext;

/**
 Grammar
 */

page: EOL? (wikitem EOL? )+ EOF;

wikitem:
      wikitem wikitem
    | title
    | template
    | link
    | text
    | bullet_line
    ;

title: title2 | title3 | title4 | title5;
title5: '=====' text '=====';
title4: '====' text '====';
title3: '===' text '===';
title2: '==' text '==';

template: '{{' parameter ('|' parameter)* '}}';
link: '[[' parameter ('|' parameter)* ']]';

parameter: wikitem?; // parameter can be empty, I.E. {{a|}}

bullet_line: WS? bullet=('*'|'#'|'#:'|'#*') WS? wikitem;

text: (CHAR | WS)+;

/**
 Lexicon
 */
EOL: [\f\r\n]+;
CHAR: ~[ \t\f\r\n];
WS: [ \t]+;


在Antlr中,词法分析器独立于解析器运行;没有上下文相关的词法分析。词法分析器返回“{{”,但解析器规则
text:(CHAR | WS)+;
不接受”{{第190行。您需要在解析器规则中列出所有多字符标记,或者将字符串文本分解为单个字符序列。除了编写自己的语法或使用