专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Flex

Win flex-bison 的容易使用

发布时间:2011-06-27 19:22:37 文章来源:www.iduyao.cn 采编人员:星星草
Win flex-bison 的简单使用

Win flex-bison 的简单使用

Win flex-bison 的官网http://sourceforge.net/projects/winflexbison/

Win flex-bison isa port Flex & Bison tools to the Windows platform

 

学习编译原理的朋友,都会看到书中提到的 lex& yaccflex &bison工具组合。这两组工具在Unix,Linux,BSD上使用不会有太大的问题,但在Windows上使用通常需要安装MinGW+ (MsysMsys2 )或者CygwinWin flex-bison 提供了flex &bison Windows平台上另外一种移植,不需要依赖 m4,生成的c文件可以用VC编译。

下面以 Win flex_bison 2.5.1和《flex &bison》第一章的简单计算器fb1-5为例,简单介绍如何使用。

首先从 http://sourceforge.net/projects/winflexbison/ 下载已经编译好的压缩文件 win_flex_bison-2.5.1.zip(不到700kb),

Update: 2014-01-04

Description

Win flex-bison is a windows port the Flex(the fast lexical analyser) and Bison (GNU parser generator). win_flex based onFlex version 2.5.37 source code and win_bisonbased on Bison version 2.7 and they depend on system libraries only.

UPDATE1: Bison version 3.0 available in Files section in win_flex_bison-2.5.zippackage.

UPDATE2: Now "winflexbison" available as package in Chocolatey (http://chocolatey.org/packages/winflexbison)

Win flex-bison WebSite

Categories

Libraries

License

GNUGeneral Public License version 3.0 (GPLv3)

 

解压到某一文件夹(如 winFlexBison),将这个文件夹增加到 path 环境变量中,就可以命令行使用了。

 

Bison规则描述文件 fb1-5.y

 

/* Companionsource code for "flex & bison", published by O'Reilly

 * Media, ISBN 978-0-596-15597-1

 * Copyright (c) 2009, Taughannock Networks.All rights reserved.

 * See the README file for license conditionsand contact info.

 * $Header: /home/johnl/flnb/code/RCS/fb1-5.y,v2.1 2009/11/08 02:53:18 johnl Exp $

 */

 

/* simplestversion of calculator */

 

%{

#  include <stdio.h>

%}

 

/* declare tokens*/

%token NUMBER

%token ADD SUB MULDIV ABS

%token OP CP

%token EOL

 

%%

 

calclist: /*nothing */

 | calclist exp EOL { printf("= %d\n>", $2); }

 | calclist EOL { printf("> "); }/* blank line or a comment */

 ;

 

exp: factor

 | exp ADD exp { $$ = $1 + $3; }

 | exp SUB factor { $$ = $1 - $3; }

 | exp ABS factor { $$ = $1 | $3; }

 ;

 

factor: term

 | factor MUL term { $$ = $1 * $3; }

 | factor DIV term { $$ = $1 / $3; }

 ;

 

term: NUMBER

 | ABS term { $$ = $2 >= 0? $2 : - $2; }

 | OP exp CP { $$ = $2; }

 ;

%%

main()

{

  printf("> ");

  yyparse();

}

 

yyerror(char *s)

{

  fprintf(stderr, "error: %s\n", s);

}
 

Flex词法分析文件 fb1-5.l

/* Companionsource code for "flex & bison", published by O'Reilly

 * Media, ISBN 978-0-596-15597-1

 * Copyright (c) 2009, Taughannock Networks.All rights reserved.

 * See the README file for license conditionsand contact info.

 * $Header: /home/johnl/flnb/code/RCS/fb1-5.l,v2.1 2009/11/08 02:53:18 johnl Exp $

 */

 

/* recognizetokens for the calculator and print them out */

 

%{

# include"fb1-5.tab.h"

%}

 

%%

"+"  { return ADD; }

"-"   { return SUB; }

"*"  { return MUL; }

"/"   { return DIV; }

"|"     { return ABS; }

"("     { return OP; }

")"     { return CP; }

[0-9]+    { yylval = atoi(yytext); return NUMBER; }

 

\n      { return EOL; }

"//".* 

[ \t]   { /* ignore white space */ }

.      { yyerror("Mystery character%c\n", *yytext); }

%%

手工运行以下命令

win_bison -d fb1-5.y

生成 fb1-5.tab.h 和fb1-5.tab.c 文件

win_flex --nounistdfb1-5.l win_flex --wincompat fb1-5.l

生成 lex.yy.c 文件。--nounistd–wincompat选项使生成的 lex.yy.c 不依赖<unistd.h> 可以用 VC 编译,否则就只能用 gcc 编译了。

为了项目管理方便可将 lex.yy.c 重命名为 fb1-5.c ,用 cl 手工编译

cl fb1-5.cfb1-5.tab.c libfl.acl fb1-5.c fb1-5.tab.c libfl.lib

生成 fb1-5.exe ,由于用到 yywrap() 需要链接 flex 的 libfl.a 库。用VC编译的话,可以将 libfl.a 重名为libfl.lib 更直观。

运行一下fb1-5.exe

 

你也可以建立一个MakeFile文件 makefile.mak

# Build a Simple Calc with fb1-5.y and fb1-5.l

!IF "$(OS)" == "Windows_NT"
NULL=
!ELSE 
NULL=nul
!ENDIF 

CC=cl
CFLAGS=/DNDEBUG /D_SCL_SECURE_NO_WARNINGS /O2 /W3
LD=link
LDFLAGS=/nologo
LIBS=libfl.lib
MOVE=move /Y

ALL : fb1-5.exe

CLEAN :
	-@erase "*.obj"
	-@erase "fb1-5.c"
	-@erase "fb1-5.tab.c"
	-@erase "fb1-5.tab.h"
	-@erase "fb1-5.exe"

OBJS=\
	fb1-5.obj \
	fb1-5.tab.obj \

"fb1-5.tab.c" : fb1-5.y
    if not exist "$@/$(NULL)" win_bison -d $**

"fb1-5.tab.h" : fb1-5.y
    if not exist "$@/$(NULL)" win_bison -d $**

"fb1-5.c" : fb1-5.l
	if not exist "$@/$(NULL)" win_flex --nounistd $** && $(MOVE) lex.yy.c $@

"fb1-5.exe" : $(OBJS)
	$(LD) $(LDFLAGS) /out:$@ $** $(LIBS)

"fb1-5.obj" : fb1-5.c fb1-5.tab.h
	$(CC) $(CFLAGS) /c fb1-5.c

"fb1-5.tab.obj" : fb1-5.tab.c fb1-5.tab.h
	$(CC) $(CFLAGS) /c fb1-5.tab.c
nmake /f makefile.mak 来构建。


另外一个国外图文并茂的教程为 Using flex andbison in MSVC++

http://www.di-mgt.com.au/flex_and_bison_in_msvc.html

本文就是从那演化来的。

 

libfl.a ( v2.5.37 )可以从http://pan.baidu.com/s/1eQcKMgM下载;或从 GnuWin ,Msys , Msys2 中提取。

 

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: