我正在尝试从此HOW-TO运行示例(4.3。处理配置文件) 。这是作为 L1 语言输入的代码:
zone "." {
type hint;
file "/etc/bind/db.root";
};
这些是我正在使用的来源:
LEX-файл (example6.l)
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
zone return ZONETOK;
file return FILETOK;
[a-zA-Z][a-zA-Z0-9]* yylval=strdup(yytext); return WORD;
[a-zA-Z0-9\/.-]+ yylval=strdup(yytext); return FILENAME;
\" return QUOTE;
\{ return OBRACE;
\} return EBRACE;
; return SEMICOLON;
\n /* ignore EOL */;
[ \t]+ /* ignore whitespace */;
%%
下面是一个带有语法 (yacc) 的文件 (example6.y)
%{
#include <stdio.h>
#include <string.h>
#define YYSTYPE char *
int yydebug=0;//пока без дебага
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
%}
%token WORD FILENAME QUOTE OBRACE EBRACE SEMICOLON ZONETOK FILETOK
%%
commands:
|
commands command SEMICOLON
;
command:
zone_set
;
zone_set:
ZONETOK quotedname zonecontent
{
printf("Complete zone for '%s' found\n",$2);
}
;
zonecontent:
OBRACE zonestatements EBRACE
quotedname:
QUOTE FILENAME QUOTE
{
$$=$2;
}
;
zonestatements:
|
zonestatements zonestatement SEMICOLON
;
zonestatement:
statements
|
FILETOK quotedname
{
printf("A zonefile name '%s' was encountered\n", $2);
}
;
block:
OBRACE zonestatements EBRACE SEMICOLON
;
statements:
| statements statement
;
statement: WORD | block | quotedname
我正在编译上述文件:
yacc -d example6.y
lex (example6.l)
cc lex.yy.c y.tab.c -o example6
我发现了分段错误
elvin@ubuntu:~/Documents/myfirstlex/conf_file$ ./example6
zone "." {
type hint;
file "/etc/bind/db.root";
};Segmentation fault (core dumped)
elvin@ubuntu:~/Documents/myfirstlex/conf_file$
试图找出错误,我在调试模式下运行
elvin@ubuntu:~/Documents/myfirstlex/conf_file$ ./example6
Starting parse
Entering state 0
Reducing stack by rule 1 (line 30):
-> $$ = nterm commands ()
Stack now 0
Entering state 1
Reading a token: zone "." {
type hint;
file "/etc/bind/db.root";
type hint;
};Next token is token ZONETOK ()
Shifting token ZONETOK ()
.............................
Stack now 0 1 3 7 10 13 15
Entering state 18
Reducing stack by rule 10 (line 65):
$1 = token FILETOK ()
$2 = nterm quotedname ()
Segmentation fault (core dumped)
你能解释一下可能是什么问题吗?如果 strdup 是问题所在,如何避免使用它?
您需要在 LEX 文件中添加
#define YYSTYPE char *
before 。#include "y.tab.h"
该错误是平庸的,编译器应该明确警告这一点:
HOWTO
刻度是在那些管时代写的,当指针适合时int
,因此他们并没有太在意它。今天,尝试int
在 32-bit 中传输 64-bitchar *
,当然以 UB 和崩溃结束......