テキストファイル内のトークンの長さを返す字句アナライザを作成しようとしています。プログラムがYYText()またはYYLeng()を返さないのはなぜですか?
テキストファイルに「a」という文字が1つあります。次
は%option noyywrap
%{
%}
/* regular definitions */
delim [ \t\n]
ws {delim}+
letter [A-Za-z]
digit [0-9]
%%
{ws} {/* no action */}
letter {return 1;}
%%
次はYYText()とYYLeng()関数を使用するメインプログラムファイルである私のlex.lファイルです。
#include <stdio.h>
#include <stdlib.h>
#include "lex.yy.cc"
using namespace std;
int OpenInputOutput(int argc, char *argv[], ifstream & lexerFIn, ofstream & lexerFOut)
{
// open input
if (argc > 1) {
lexerFIn.open(argv[1]);
if (lexerFIn.fail()) {
cerr << "Input file cannot be opened\n";
return 0;
}
}
else {
cerr << "Input file not specified\n";
return 0;
}
// open output
lexerFOut.open("Output.txt");
if (lexerFOut.fail()) {
cerr << "Output file cannot be opened\n";
return 0;
}
}
int main(int argc, char *argv[])
{
yyFlexLexer lexer;
while (lexer.yylex() != 0) {
cout << lexer.YYText() << endl;
cout << lexer.YYLeng() << endl;
}
return 0;
}
私は、コマンド./a「sample.txtの」と、前述したテキストファイルを使用してプログラムを実行
が、それは「」ファイルに書き込みます。なぜそれはYYText()またはYYLeng()を出力しないか、または出力ファイルに文字の長さを書きませんか?
コードのどこにも、コマンドライン引数としてファイル名を使用しません。それはあなたがそれを渡した "sample.txt"を無視します。 –
なぜあなたはプログラムのどこでも開いたことのないファイルから 'a'と1を返すべきですか? – ssube
私は質問を更新しました。 – idealistikz