ANTLR4 - 令牌识别错误和输入不匹配

问题描述 投票:0回答:1

我对 ANTLR 语法相当陌生。这是我的 g4 文件中的内容:

tptp_file               : tptp_input* EOF;
tptp_input              : annotated_formula | include;

annotated_formula : fof_annotated  |  cnf_annotated;
fof_annotated : 'fof('name','formula_role','fof_formula annotations').';

name : atomic_word  |  integer;

atomic_word : lower_word  |  single_quoted;

lower_word : lower_alpha alpha_numeric'*';

lower_alpha : '[a-z]';
upper_alpha : '[A-Z]';
numeric : '[0-9]';

alpha_numeric : '('lower_alpha | upper_alpha | numeric | '[_])';

...

我尝试在这个包含以下内容的测试文件上使用 ANTLR 解析器:

fof(an,axiom,p).

但是我收到一条错误消息:

line 1:4 token recognition error at: 'a'
line 1:5 token recognition error at: 'n'
line 1:7 token recognition error at: 'a'
line 1:8 token recognition error at: 'x'
line 1:9 token recognition error at: 'io'
line 1:11 token recognition error at: 'm'
line 1:13 token recognition error at: 'p'
line 1:6 mismatched input ',' expecting {'(', ''', '[1-9]', '[a-z]'}
line 1:12 mismatched input ',' expecting '[a-z]'
line 1:14 mismatched input ').' expecting {'(', '[', '[]', '!', '~', '?', '#', '["]', ''', '[1-9]', '[a-z]', '[A-Z]', '[$]'}

任何人都可以帮助我理解我做错了什么以及如何解决它吗?谢谢。

我将 lower_alpha 声明为片段。

parsing antlr antlr4 grammar
1个回答
0
投票

乍一看,这些事情要么是错误的,要么是不好的做法:

语法中的许多文字标记

在解析器规则中执行

').'
只会匹配
).
而不是
') .'
(它们之间有空格)。尝试最小化解析器规则中的文字标记(除非您知道自己在做什么)并在词法分析器规则中定义它们:

OPAR : '(';
CPAR : ')';
DOT : '.';

并在解析器规则中使用这些标记。

没有词法分析器规则

您没有词法分析器规则,但尝试在解析器规则中创建它们。规则:

lower_alpha : '[a-z]';

匹配文本

[a-z]
不是小写字母。改为这样做:

// Lexer rules start with a capital and no quotes around it
Lower_alpha : [a-z];

限定词错误

如果你想匹配零个或多个东西,你不应该做

Alpha_numeric'*'
,而应该做
Alpha_numeric*
(不带引号)。

修复

这是适用于给定输入的语法

fof(an,axiom,p).

grammar T;

tptp_file
 : tptp_input* EOF
 ;

tptp_input
 : annotated_formula
 | include
 ;

annotated_formula
 : fof_annotated
 | cnf_annotated
 ;

fof_annotated
 : FOF OPAR name COMMA formula_role COMMA fof_formula annotations? CPAR DOT
 ;

name
 : WORD
 | INTEGER
 ;

// TODO implement these parser rules yourself
formula_role : name;
include : name;
cnf_annotated : name;
fof_formula : name;
annotations : name;

OPAR : '(';
CPAR : ')';
DOT : '.';
COMMA : ',';
FOF : 'fof';
WORD : [a-zA-Z]+;
INTEGER : [0-9]+;

parse tree of the example input

© www.soinside.com 2019 - 2024. All rights reserved.