Windows環境下lex入門


下載部署:

  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

  編譯運行,大功告成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM