【发布时间】:2017-01-11 15:00:52
【问题描述】:
在 cygwin 环境中使用 flex/bison 而不是 lex/yacc 运行 this howto(第 4.3 节)中的示例 6 时,我得到了意外/丢失的输出。
我从下载部分下载并解压example files。在文件 example6.compile 中,我将 'lex' 替换为 'flex',否则保持原样(yacc 命令在 cygwin 上执行 exec '/usr/bin/bison' -y "$@")。然后我运行example6.compile。它运行没有错误,但有一些警告(见附录)。
然后我运行 example6,并输入示例文本:
zone "." {
type hint;
file "/etc/bind/db.root";
type hint;
};
预期的输出是:
A zonefile name '/etc/bind/db.root' was encountered
Complete zone for '.' found
实际输出为:
A zonefile name '' was encountered
Complete zone for '' found
为什么伪变量的值会丢失?
附录
example6.compile:
flex example6.l
yacc --verbose --debug -d example6.y
cc lex.yy.c y.tab.c -o example6
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 */;
%%
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
编译时的警告:
example6.l: In function ‘yylex’:
example6.l:10:7: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
[a-zA-Z][a-zA-Z0-9]* yylval=strdup(yytext); return WORD;
^
example6.l:11:7: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
[a-zA-Z0-9\/.-]+ yylval=strdup(yytext); return FILENAME;
^
example6.y:19:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main()
^
example6.y: In function ‘main’:
example6.y:21:2: warning: implicit declaration of function ‘yyparse’ [-Wimplicit-function-declaration]
yyparse();
^
y.tab.c: In function ‘yyparse’:
y.tab.c:1164:16: warning: implicit declaration of function ‘yylex’ [-Wimplicit-function-declaration]
yychar = yylex ();
【问题讨论】:
标签: bison flex-lexer