標籤:lines 入口 window 分享 line hide lap bison amp
下載部署:
https://sourceforge.net/projects/winflexbison/ 下載 win_flex_bison-latest.zip ,解壓到C:\win_flex_bison
編輯lex檔案:
lex檔案使用“%%”隔成三個段,分別是:定一段,規則短,使用者程式碼片段;
這裡給出一個簡單的現成的例子:
%{int num_lines = 0, num_chars = 0;%}%%\n ++num_lines; ++num_chars;. ++num_chars;%%int yywrap(){return 1;}
View Code
編譯lex檔案:
>c:\win_flex_bison\win_flex.exe --wincompat a.l // wincompat參數不能省
沒有訊息就是好訊息,產生了檔案 lex.yy.c
匯入VS2013項目:
使用VS2013建立Win32控制台項目
將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;}
View Code
編譯運行,大功告成。
Windows環境下lex入門