Windows環境下lex入門

來源:互聯網
上載者:User

標籤: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入門

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.