gpt4 book ai didi

syntax-error - Yacc/Bison : what's wrong with my syntax equations?

转载 作者:行者123 更新时间:2023-12-03 08:28:16 26 4
gpt4 key购买 nike

我正在编写某种“编译器”:它读取游戏的描述(包括房间,角色,事物等)。将其视为冒险类游戏的视觉版本,但存在很多简单问题。

当我运行“编译器”时,输入中出现语法错误,我不知道为什么。这是我的yacc输入的相关部分:

character
: char-head general-text character-insides { PopChoices(); }
;
character-insides
: LEFTBRACKET options RIGHTBRACKET
;
char-head
: char-namesWT opt-imgsWT char-desc opt-cond
;
char-desc
: general-text { SetText($1); }
;
char-namesWT
: DOTC ID WORD { AddCharacter($3, $2); expect(EXP_TEXT); }
;
opt-cond
: %empty
| condition
;
condition
: condition-reason condition-main general-text
{ AddCondition($1, $2, $3); }
;
condition-reason
: DOTU { $$ = 'u'; }
| DOTV { $$ = 'v'; }
;
condition-main
: money-conditionWT
| have-conditionWT
| moves-conditionWT
| flag-conditionWT
;
have-conditionWT
: PERCENT_SLASH opt-bang ID
{ $$ = MkCondID($1, $2, $3) ; expect(EXP_TEXT); }
;
opt-bang
: %empty { $$ = TRUE; }
| BANG { $$ = FALSE; }
;
ID: WORD
;

所有大写字母表示的是终端符号,小写或混合大小写的表示非终端符号。如果非终端以WT结尾,则它“想发送文本”。也就是说,它期望之后的内容可能是任意文本。

背景:我已经用C++编写了自己的 token 识别器,因为(*)我希望语法能够改变词法分析器行为的方式。两种类型的 token 仅在语法需要时才应匹配:FILENAME(带有斜杠和其他非字母字符)和TEXT,这意味着“从此处到行尾的所有文本”(但不能以某些关键字开头) )。

函数“expect”告诉词法分析器何时查找这两个符号。返回每个 token 后,期望值将重置为EXP_NORMAL。

我在yylex中添加了代码,在yylex识别出 token 的情况下将其打印出来,在我看来, token 生成器工作正常-返回了我期望的 token 。

(*)也是因为我希望能够向 token 生成器询问发生错误的列,并获得当时正在扫描的行的内容,因此我可以打印出更有用的错误消息。

这是输入的相关部分:
.c Wendy wendy
OK, now you caught me, what do you want to do with me?
.u %/lasso You won't catch me like that.
[

这是yylex调试输出的最后一部分:
token: 262: DOTC/
token: 289: WORD/Wendy
token: 289: WORD/wendy
token: 292: TEXT/OK, now you caught me, what do you want to do with me?
token: 286: DOTU/
token: 274: PERCENT_SLASH/%/
token: 289: WORD/lasso
token: 292: TEXT/You won't catch me like that.
token: 269: LEFTBRACKET/

这是我的错误信息:
:第124行,第3-4列:语法错误,意外的LEFTBRACKET,期待TEXT
[

为了帮助您理解上面的等式,这是我从中编写yacc代码的输入语法说明的相关部分。
// Character:
// .c id charactername,[imagename,[animationname]]
// description-text
// .u condition on the character being usable [optional]
// .v condition on the character being visible [optional]
// [
// (options)
// ]
// Conditions:
// %$[-]n Must [not] have at least n dollars
// %/[-]name Must [not] have named thing
// %t-nnn At/before specified number of moves
// %t+nnn At/after specified number of moves
// %@[-]name named flag must [not] be set
// Condition-char: $, /, t, or @, as described above
//
// Condition:
// % condition-char (identifier/int) ['/' text-if-fail ]
// description-text: Can be either on-line text or multi-line text
// On-line text is the rest of the line

方括号标记了可选的非终结符,但是一个单独的括号(在yacc中由LEFTBRACKET和RIGHTBRACKET表示)是实际的 token ,例如
//[
//(选项)
//]
以上。

我究竟做错了什么?

最佳答案

要调试语法中的解析问题,您需要了解yacc/bison产生的移位/归约机器(在.output选项产生的-v文件中进行了描述),并且需要查看解析器经过的状态轨迹解决您看到的问题。

要在解析器中启用调试代码(可以打印状态和移位并减少发生的 Action ),您需要使用-DYYDEBUG进行编译,或者将#define YYDEBUG 1放在语法文件的顶部。调试代码由全局变量yydebug控制-设置为非零以打开跟踪,设置为零以关闭跟踪。我经常在main中使用以下内容:

#ifdef YYDEBUG
extern int yydebug;
if (char *p = getenv("YYDEBUG"))
yydebug = atoi(p);
#endif

然后,您可以在编译器标志中包括 -DYYDEBUG以进行调试版本,并通过使用 setenv YYDEBUG 1之类的工具打开调试代码以在运行程序之前设置envvar。

关于syntax-error - Yacc/Bison : what's wrong with my syntax equations?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37336429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com