flex与bison的学习

  • 2020 年 11 月 9 日
  • 笔记

 

 

获取bison

//www.gnu.org/software/bison

 

 

 

 获取flex

//flex.sourceforge.net/

 

 

 

 本书的范例

ftp://ftp.iecc.com/pub/file/flexbison.zip 

 

 第一个Flex程序

fb1-1.l

/* Companion source 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 conditions and contact info.
* $Header: /home/johnl/flnb/code/RCS/fb1-1.l,v 2.1 2009/11/08 02:53:18 johnl Exp $
*/

/* fb1-1 just like unix wc */
%{
int chars = 0;
int words = 0;
int lines = 0;
%}

%%

[a-zA-Z]+ { words++; chars += strlen(yytext); }
\n { chars++; lines++; }
. { chars++; }

%%
main()
{
yylex();
printf(“lines:%8d\nwords:%8d\nchars:%8d\n”, lines, words, chars);
}
yywrap() { return 1; }

文件放置。

 

编译运行该文件

 

 运行结果测试