1. 新建 Win32 Console Appliction 工程.
2. 添加 Parser中將會要使用的Class(CItem).
3. 編寫 name.l 文件,其中需要包含之後 name.ypp 生成的頭文件(name.tab.hpp)和Classes的頭文件(_Item.h) :
#include “Item.h”
#include “name.tab.hpp” // 兩者順序不可交換,否則編譯不過.
4. 編寫 name.ypp 文件,添加Classess頭文件和函數的聲明 :
#include "Item.h"
void yyerror (const char *error);
int yylex ();
在之後定義 Type 時,可以如下定義:
%union {
CItem * pItem ;
…….
}
5. 編譯 name.l和name.ypp
Flex –oscanner.cpp -+ name.l
Bison -d name.ypp
生成文件 scanner.cpp / name.tab.hpp / name.tab.cpp .
6. 將scanner.cpp / name.tab.hpp / name.tab.cpp 添加進 Project .
7. Project àSettingàC++àPercompiled headers 設置 Automatic use percompiled headers .
8. 從 Cygwin/Usr/Include下Copy “Flexlexer.h”到VC系統目錄.
9. 修改 “Flexlexer.h”,在 “#include <iostream>”下面添加 “using namespace std;”.
10. 添加 Flexlib.lib的鏈接.
11. 定義全局變量 “FlexLexer * g_Lexer = NULL ;”,和聲明“extern int yyparse();”.
12. 添加函數實現 :
int yylex()
{
if(g_Lexer!=NULL)
{
return g_Lexer->yylex();
}
return 0 ;
}
13. 在 main 函數中,執行以下代碼:
g_Lexer = new yyFlexLexer(
&cin, // Input stream
&cout // Output stream
);
if(g_Lexer==NULL)
{
cout<<"Create scanner fail."<<endl ;
return 0 ;
}
yyparse(); // Parsing.