下載部署:ide
https://sourceforge.net/projects/winflexbison/ 下載 win_flex_bison-latest.zip ,解壓到C:\win_flex_bison函數
編輯lex文件:flex
lex文件使用「%%」隔成三個段,分別是:定一段,規則短,用戶代碼段;spa
這裏給出一個簡單的現成的例子:.net
%{ int num_lines = 0, num_chars = 0; %} %% \n ++num_lines; ++num_chars; . ++num_chars; %% int yywrap() { return 1; }
編譯lex文件:code
>c:\win_flex_bison\win_flex.exe --wincompat a.l // wincompat參數不能省blog
沒有消息就是好消息,生成了文件 lex.yy.cip
導入VS2013項目:部署
使用VS2013新建Win32控制檯項目io
將lex.yy.c複製到項目下,並更名爲 lex.yy.cpp,加入項目
在頭部加入:#include "stdafx.h"
main函數源文件以下:
// testbench.cpp : 定義控制檯應用程序的入口點。 // #include "stdafx.h" #include <stdio.h> extern int num_lines; extern int num_chars; extern FILE *yyin; extern int yylex(void); int _tmain(int argc, _TCHAR* argv[]) { fopen_s(&yyin, "D:\\STUDY\\flexbison\\linecounter\\a.l", "r"); yylex(); fclose(yyin); printf("lines = %d, chars = %d\n", num_lines, num_chars); return 0; }
編譯運行,大功告成。