简体   繁体   English

BNF命题逻辑ANTLR的语法

[英]BNF Grammar for propositional logic ANTLR

I'm trying to create a BNF Grammar in Antlr for propositional logic but I keep getting the error: 我正在尝试在Antlr中为命题逻辑创建BNF语法,但我不断收到错误消息:

java.lang.NoSuchFieldError: offendingToken java.lang.NoSuchFieldError:offendingToken

As there is no line number displayed, I don't know where the error is. 由于没有显示行号,所以我不知道错误在哪里。 The build is successful, but when I type in an example input, the tree stops at sentence, the first item defined in the BNF. 构建成功,但是当我输入示例输入时,树停在句子中,这是BNF中定义的第一项。

Here is my BNF: 这是我的BNF:

grammar Grammar;

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

@header { 
    package antlr;
}

@members { 

}

@lexer::header { //lexer
    package antlr;
}

@lexer::members {

}

sentence: atomicsentence | complexsentence;

atomicsentence: 'T' | 'F' | symbol;

complexsentence: unop sentence | sentence binop sentence | (sentence);

unop: 'NOT';

binop: 'AND' | 'OR' | 'IMPLIES' | 'EQUIVALENT'; 

symbol: (LEXRULE)+;

LEXRULE: ('a'..'z')|('A'..'Z');

If you comment out complexsentence in sentence, the atomicsentence part works, until it terminates because there is no EOF. 如果您在句子中注释掉复杂句,则原子句部分会起作用,直到因为没有EOF而终止为止。 I'm unsure as to where this should go as adding it to sentence does not work. 我不确定应该在哪里,因为将其添加到句子中不起作用。

(edited) (编辑)

I have refactored your grammar, so it should work as you intended. 我已经重构了您的语法,因此它应该可以按您的预期工作。

grammar Grammar;

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

tokens {
    CODE;
       }

@header { 
    package antlr;
}

@members { 

}

@lexer::header { //lexer
    package antlr;
}

@lexer::members {

}

code    :   sentence -> ^(CODE code);

sentence: UNOP? complexsentence (BINOP sentence)?;

atomicsentence: 'T' | 'F' | SYMBOL;

complexsentence: atomicsentence | '(' sentence ')';

UNOP: 'NOT';

BINOP: 'AND' | 'OR' | 'IMPLIES' | 'EQUIVALENT'; 

SYMBOL: LEXRULE+;

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

Your grammar is left recursive, which ANTLR mentions when trying to generate a parser: 您的语法是递归的,ANTLR在尝试生成解析器时会提到:

[17:31:32] error(210): The following sets of rules are mutually left-recursive [complexsentence, sentence] [17:31:32] Aborting because the following rules are mutually left-recursive: [[T.complexsentence,index=4,line=15], [T.sentence,index=2,line=11]] [17:31:32]错误(210):以下规则集相互左递归[复杂性,句子] [17:31:32]由于以下规则相互左递归而中止:[[T.complexsentence ,index = 4,line = 15],[T.sentence,index = 2,line = 11]]

The rule sentence matches a complexsentence , and the complexsentence rule in its turn matches a sentence . 规则sentence匹配complexsentence sentence ,而complexsentence sentence规则依次匹配sentence ANTLR (v3) cannot cope with such left-recursive rules. ANTLR(v3)无法应付此类左递归规则。

Another problem with your grammar is that you have no lexer rule for whiate spaces, yet your example input "NOT p" contains a white space. 语法的另一个问题是,对于whiate空间,您没有词法分析器规则,但是示例输入"NOT p"包含空格。

For a simple expression parser using ANTLR, see: 有关使用ANTLR的简单表达式解析器,请参阅:

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

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