弹性:无法创建

Flex: Could not create

我在 cygwin 中使用了 flex,它工作得很好,所以我为 windows 安装了 flex,因为我需要我的程序的 windows 版本。当我尝试创建词法分析器时,我收到消息:

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 中,唯一的区别是我需要在命令中切换源文件名和目标文件名。

编辑:

如果我尝试:

flex -o Lexer.cpp Lexer.l

我得到:

flex: can't open Lexer.cpp
flex -o Lexer.l Lexer.cpp

告诉 flex 处理输入文件 Lexer.cpp,并将 输出 (-o) 放入 Lexer.l。我猜这不是你想要做的,因为通常 Lexer.l 将是输入并且不希望覆盖它。

在非常旧的 flex 版本上(和 flex 2.5.4a,被 "flex for windows" 使用,算作非常旧的版本),你不能在 [=14 之后放一个 space =];文件名必须紧跟字母 o。所以正确的命令行是:

flex -oLexer.cpp Lexer.l

顺便说一句,

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

确实不是什么好作风。通常,系统(库)头文件应该首先是 #included,通常对于 C++,您会使用 #include <cstdio> 而不是 C 头文件 stdio.h。但这与你的问题无关。