使用BNFC来确定命题逻辑的基本语言(语法错误)

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

我想使用BNFC解析命题逻辑中的句子。我编写了以下BNF语法来简化此操作:

Negation.     N ::= "(" "-" L")";
Conjuction.   C ::= "(" L "&" L ")";
Disjuction.   D ::= "(" L "|" L ")";
Implication.  I ::= "(" L "=>" L ")";
Equivalence.  E ::= "(" L "<=>" L ")";
Atom.         L ::= Ident | N | C | D | I | E ;

但是,在这种构造下,出现以下错误:

syntax error at line 6, column 27 before `|'

我提供的规范在语法上有何不正确?

编辑1

好,所以看起来bnfc确实不喜欢使用符号|进行联合的想法。如果不通过联合,那么如何将多个生产分配给单个规则?我不想定义Atom1. L ::= Ident ;Atom2. L ::= N ;等,但是如果我想使它起作用,是否有必要?

编辑2

确定,因此为每个L产品赋予不同的标签,如

Negation.     N ::= "(" "-" L")";
Conjuction.   C ::= "(" L "&" L ")";
Disjuction.   D ::= "(" L "|" L ")";
Implication.  I ::= "(" L "=>" L ")";
Equivalence.  E ::= "(" L "<=>" L ")";
Atom1.        L ::= Ident ;
Atom2.        L ::= N ;
Atom3.        L ::= C ;
Atom4.        L ::= D ;
Atom5.        L ::= I ;
Atom6.        L ::= E ;

允许文件logic.cf毫无问题地通过bnfc。但是,使用以下命令编译文件时

bnfc -m -c file.cf

然后我尝试运行make,当Make尝试在bnfc生成的文件gcc上运行Printer.c时,出现以下错误:

gcc -g -W -Wall -c Absyn.c
flex -Plogic -oLexer.c logic.l
gcc -g -W -Wall -c Lexer.c 
Lexer.c:1477:16: warning: ‘input’ defined but not used [-Wunused-function]
     static int input  (void)
                ^~~~~
Lexer.c:1434:17: warning: ‘yyunput’ defined but not used [-Wunused-function]
     static void yyunput (int c, char * yy_bp )
                 ^~~~~~~
bison -t -plogic logic.y -o Parser.c
gcc -g -W -Wall -c Parser.c
gcc -g -W -Wall -c Printer.c
Printer.c: In function ‘ppL’:
Printer.c:289:20: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     ppIdent(_p_->u.atom_.ident_, 0);
                    ^~~~~
                    atom1_
Printer.c:296:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     ppN(_p_->u.atom_.n_, 0);
                ^~~~~
                atom1_
Printer.c:303:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     ppC(_p_->u.atom_.c_, 0);
                ^~~~~
                atom1_
Printer.c:310:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     ppD(_p_->u.atom_.d_, 0);
                ^~~~~
                atom1_
Printer.c:317:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     ppI(_p_->u.atom_.i_, 0);
                ^~~~~
                atom1_
Printer.c:324:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     ppE(_p_->u.atom_.e_, 0);
                ^~~~~
                atom1_
Printer.c: In function ‘ppInteger’:
Printer.c:336:31: warning: unused parameter ‘i’ [-Wunused-parameter]
 void ppInteger(Integer n, int i)
                               ^
Printer.c: In function ‘ppDouble’:
Printer.c:342:29: warning: unused parameter ‘i’ [-Wunused-parameter]
 void ppDouble(Double d, int i)
                             ^
Printer.c: In function ‘ppChar’:
Printer.c:348:25: warning: unused parameter ‘i’ [-Wunused-parameter]
 void ppChar(Char c, int i)
                         ^
Printer.c: In function ‘ppString’:
Printer.c:354:29: warning: unused parameter ‘i’ [-Wunused-parameter]
 void ppString(String s, int i)
                             ^
Printer.c: In function ‘ppIdent’:
Printer.c:360:28: warning: unused parameter ‘i’ [-Wunused-parameter]
 void ppIdent(String s, int i)
                            ^
Printer.c: In function ‘shL’:
Printer.c:507:20: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     shIdent(_p_->u.atom_.ident_);
                    ^~~~~
                    atom1_
Printer.c:522:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     shN(_p_->u.atom_.n_);
                ^~~~~
                atom1_
Printer.c:537:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     shC(_p_->u.atom_.c_);
                ^~~~~
                atom1_
Printer.c:552:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     shD(_p_->u.atom_.d_);
                ^~~~~
                atom1_
Printer.c:567:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     shI(_p_->u.atom_.i_);
                ^~~~~
                atom1_
Printer.c:582:16: error: ‘union <anonymous>’ has no member named ‘atom_’; did you mean ‘atom1_’?
     shE(_p_->u.atom_.e_);
                ^~~~~
                atom1_
Makefile:42: recipe for target 'Printer.o' failed
make: *** [Printer.o] Error 1

我不知道这是什么意思。当我没有在atom_中指定这样的内容时,为什么要尝试找到logic.cf?如果有人对bnfc的内部结构有更丰富的经验,我不会介意您的来信。

编辑3

好,所以将标签写为

Negation.     N ::= "(" "-" L ")";
Conjuction.   C ::= "(" L "&" L ")";
Disjuction.   D ::= "(" L "|" L ")";
Implication.  I ::= "(" L "=>" L ")";
Equivalence.  E ::= "(" L "<=>" L ")";
Atom.         L ::= Ident;
AtomN.        L ::= N ;
AtomC.        L ::= C ;
AtomD.        L ::= D ;
AtomI.        L ::= I ;
AtomE.        L ::= E ;

以某种方式神奇地允许make通过。但是,我的解析器无法正常工作,就像[]

echo "p" | ./Testlogic

返回

error: line 1: syntax error at p

p不是有效的标识符,因此,产品Atom. L ::= Ident;应该允许它通过吗?为什么不是这种情况?

我想使用BNFC解析命题逻辑中的句子。我编写了以下BNF语法来简化此操作:否定。 N :: =“(”“-” L“)”;结合。 C :: =“(” L“&” L“)”; ...

syntax syntax-error bnfc
1个回答
0
投票

事实证明,BNFC在制作顺序方面有点挑剔。我不得不写

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