我想用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-production赋予不同的标签,如下所示
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生成的文件Printer.c上运行gcc时,我得到以下错误
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我不知道这是什么意思。当我还没有在logic.cf中指定这样的东西时,为什么它要试图找到atom_呢?如果有人对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 pp不是一个有效的标识符,所以生产Atom. L ::= Ident;应该允许它通过吗?为何不是这样呢?
发布于 2020-04-11 22:55:33
事实证明,当涉及到产品的顺序时,BNFC有点挑剔。我不得不写
Atom. L ::= Ident;
AtomN. L ::= N ;
AtomC. L ::= C ;
AtomD. L ::= D ;
AtomI. L ::= I ;
AtomE. L ::= E ;
Negation. N ::= "(" "-" L ")";
Conjuction. C ::= "(" L "&" L ")";
Disjuction. D ::= "(" L "|" L ")";
Implication. I ::= "(" L "=>" L ")";
Equivalence. E ::= "(" L "<=>" L ")";除了去掉标签末端的数字外,它还可以工作。我现在能够解析谓词逻辑中的简单句子了:
$ echo "(a&(a<=>b))" | ./Testlogic
Parse Succesful!
[Abstract Syntax]
(AtomC [(Conjuction (Atom ["a"]) (AtomE [(Equivalence (Atom ["a"]) (Atom ["b"]))]))])
[Linearized Tree]
(a & (a <=> b))https://stackoverflow.com/questions/61154549
复制相似问题