2015-10-28 71 views
0

我已經用cygwin使用flex,它工作得很好,所以我安裝了flex for windows,因爲我需要一個windows版本的程序。當我試圖創建詞法分析器我得到的消息:Flex:無法創建

flex: could not create. 

這裏的文件(它具有Cygwin的作品):

%{ 
    #include "Ast.h" 
    #include "Parser.hpp" 
    #include <stdio.h> 
    #include <string> 
    #define SAVE_TOKEN_STR yylval.string = new std::string(yytext, yyleng) 
    #define TOKEN(t) (yylval.token = t) 
%} 
%% 
[0-9]+          { SAVE_TOKEN_STR; return INTEGER; } 
[0-9]+\.[0-9]+        { SAVE_TOKEN_STR; return FLOAT; } 
[0-9]+(\.[0-9]+)?[eE][-+]?[0-9]+(\.[0-9]+)? { SAVE_TOKEN_STR; return SCIENTIFIC; } 
"(" { return TOKEN(LPAR); } 
")" { return TOKEN(RPAR); } 
"{" { return TOKEN(LCBR); } 
"}" { return TOKEN(RCBR); } 
"[" { return TOKEN(LSQBR); } 
"]" { return TOKEN(RSQBR); } 
"+" { return TOKEN(PLUS); } 
"-" { return TOKEN(MINUS); } 
"*" { return TOKEN(STAR); } 
"/" { return TOKEN(SLASH); } 
"%" { return TOKEN(PERCENT); } 
"**" { return TOKEN(EXPONENT); } 
"=" { return TOKEN(ASSIGN); } 
"==" { return TOKEN(EQ); } 
"<>" { return TOKEN(NEQ); } 
"<" { return TOKEN(LESS); } 
"<=" { return TOKEN(LOE); } 
"<=>" { return TOKEN(SPACESHIP); } 
">" { return TOKEN(GREATER); } 
">=" { return TOKEN(GOE); } 
"!" { return TOKEN(NOT); } 
"&&" { return TOKEN(AND); } 
"||" { return TOKEN(OR); } 
"not" { return TOKEN(NOT); } 
"and" { return TOKEN(AND); } 
"or" { return TOKEN(OR); } 
"~" { return TOKEN(BITWISE_NOT); } 
"&" { return TOKEN(BITWISE_AND); } 
"|" { return TOKEN(BITWISE_OR); } 
"^" { return TOKEN(BITWISE_XOR); } 
"<<" { return TOKEN(BITWISE_LSHIFT); } 
">>" { return TOKEN(BITWISE_RSHIFT); } 
"~~" { return TOKEN(ROUND); } 
"." { return TOKEN(DOT); } 
".." { return TOKEN(RANGE); } 
"..." { return TOKEN(TRANGE); } 
"?" { return TOKEN(QUESTION_MARK); } 
":" { return TOKEN(COLON); } 
"in" { return TOKEN(IN); } 
"," { return TOKEN(COMMA); } 
[A-Za-z_][A-Za-z0-9_]* { SAVE_TOKEN_STR; return IDENT; } 
[ \n\t] ; 
.  { printf("Illegal token!\n"); yyterminate(); } 
%% 
#ifndef yywrap 
    yywrap() { return 1; } 
#endif 

這裏就是我試圖執行命令:

flex -o Lexer.l Lexer.cpp 

在cygwin中唯一的區別是我需要在命令中切換源和destionation文件名。

編輯:

如果我嘗試:

flex -o Lexer.cpp Lexer.l 

我得到:

flex: can't open Lexer.cpp 
+0

它是否真的產生錯誤消息'flex:could not create.' like that,a period after following'create'? – rici

+0

是的,這是確切的輸出 –

+0

嗯,這很奇怪。格式字符串是「無法創建%s」,它有一個空格並且沒有'.'。由於特殊的參數解析策略,文件名是空字符串,所以我期望錯誤信息是「無法創建」的,最後有一個不可見的空間。但我想這只是一個小細節。 – rici

回答

1
flex -o Lexer.l Lexer.cpp 

告訴柔性處理輸入文件Lexer.cpp,並把輸出-o)in Lexer.l。我猜這不是你想要做的,因爲通常Lexer.l將是輸入,並且不希望覆蓋它。

在flex的真正舊版本(和「用於windows的flex」中使用的flex 2.5.4a,算作一個真正的舊版本)時,不能在-o之後放置空格;文件名必須緊跟在字母o之後。所以,正確的命令行是:

flex -oLexer.cpp Lexer.l 

順便說一句,

#include "Ast.h" 
#include "Parser.hpp" 
#include <stdio.h> 
#include <string> 

真的不是好作風。通常情況下,系統(庫)頭應該首先爲#included,通常使用C++,則使用#include <cstdio>而不是C頭stdio.h。但是這與你的問題無關。

+0

在窗戶上,這是另一種方式。如果我彈出-o Lexer.cpp Lexer.l,則得到:flex:無法打開Lexer.cpp –

+0

@PeterLenkefi:Windows機器上的flex版本是什麼?我向你保證,任何解釋'-o'選項的flex版本都會將其視爲「將輸出放入名稱後面的文件中」。 – rici

+0

我正在使用2.5.4a-1 –