简体   繁体   English

Java Antlr4一阶逻辑语法

[英]Java Antlr4 first order logic grammar

I want to parse strings in first order logic and turn them into a specific class structure. 我想解析一阶逻辑中的字符串,并将它们转换为特定的类结构。 For instance, I want to parse a formula such as 例如,我想解析一个公式,例如

∀x∃y∃z((R(x,y) ∨ Px)→(Qx→(Px∧Zx))) 

and turn it into a Universal class which has a Variable field and a Formula quantifiedFormula field which stands for the rest of the formula. 并将其转换为通用类,该类具有“变量”字段和一个“公式quantifiedFormula”字段,该字段代表该公式的其余部分。 I have troubles with the grammar, though. 我在语法上有麻烦。 When I parse that formula with the antlr generated code I get 当我用antlr生成的代码解析该公式时,

line 1:11 extraneous input '(' expecting {'\u2200', '\u2203', '\u00ac'}

'\∀' is ∀, \∃ is ∃ and \¬ is ¬, the negation sign. '\\ u2200'是∀,\\ u2203是∃,\\ u00ac是¬(否定符号)。

This is my grammar file. 这是我的语法文件。 I put it together following the FOL.g file found on the antlr3 site. 我将其放置在antlr3站点上的FOL.g文件之后。 I am using antl4, however. 我正在使用antl4。

grammar FOL; 语法FOL;

options{
    language=Java;
    output=AST;
    ASTLabelType = CommonTree;
    backtrack=true;
}

tokens{
    PREDICATE,
    FUNCTION
}

/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/

condition: formula EOF ;

formula 
:     (forall | exists)* bidir ;
forall  :   FORALL VARIABLE ;
exists  :   EXISTS VARIABLE ;

bidir   :   implication (BIDIR implication)*;

implication
:   disjunction (IMPL disjunction)*;

disjunction
:   conjunction (OR conjunction)* ;

conjunction
:   negation (AND negation)* ;

negation 
:   NOT (predicate | LPAREN* formula RPAREN*) ;

predicate 
:   PREPOSITION predicateTuple (PREDICATE PREPOSITION predicateTuple)
|   PREPOSITION ;

predicateTuple
:   LPAREN term (',' term)* RPAREN ;

term    :   function | VARIABLE ;

 function:  CONSTANT functionTuple (FUNCTION CONSTANT functionTuple)
|   CONSTANT;

functionTuple
    :   LPAREN (CONSTANT | VARIABLE) (',' (CONSTANT | VARIABLE) )* RPAREN;

/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
LPAREN: '(';
RPAREN: ')';
FORALL: '\u2200';
EXISTS: '\u2203';
NOT:'\u00ac';
IMPL: '\u2192';
BIDIR: '\u2194';
OR: '\u2228';
AND: '\u2227';
EQ: '=';


VARIABLE:  (('q'..'z') ) CHARACTER* ;

CONSTANT: (('a'..'p') ) CHARACTER* ;

PREPOSITION: ('A'..'Z') CHARACTER* ;

fragment CHARACTER: ('a'..'z' | 'A'..'Z' | '_') ;

WS : (' ' | '\t' | '\r' | '\n')+ -> skip  ;

That seems unsurprising. 这似乎不足为奇。

According to your grammar, a formula is some number of exists and forall clauses followed by a bidir . 根据您的语法, formulaexists若干个和forall子句后跟一个bidir If you trace through the productions for bidir , it becomes clear that it must start with a negation and that, in turn, must start with NOT . 如果您跟踪bidir的生产, bidir很明显,它必须以negation开头,而必须以NOT开头。 So while you scan the formula , you must see clauses headed by one of the three tokens EXISTS , FORALL or NOT . 因此,当您扫描formula ,必须看到以三个标记EXISTSFORALLNOT之一标记的子句。

Your negation needs to include the possibility that it is not a negation. 您的negation需要包括它不是否定的可能性。 You could, for example, make NOT optional. 例如,您可以使NOT可选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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