2015-10-28 69 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 –