7

flex を使用してプロジェクトのレクサーを構築し始めたばかりですが、それを機能させる方法がわかりません。チュートリアルにあるサンプル コードをコピーし、tut ファイルを引数として flex++ を実行しようとしましたが、毎回エラーが発生します。例えば

入力ファイル(calc.l)

%name Scanner
%define IOSTREAM

DIGIT   [0-9]
DIGIT1  [1-9]

%%

"+"               { cout << "operator <" << yytext[0] << ">" << endl; }
"-"               { cout << "operator <" << yytext[0] << ">" << endl; }
"="               { cout << "operator <" << yytext[0] << ">" << endl; }
{DIGIT1}{DIGIT}*  { cout << "  number <" << yytext    << ">" << endl; }
.                 { cout << " UNKNOWN <" << yytext[0] << ">" << endl; }

%%

int main(int argc, char ** argv)
{
    Scanner scanner;
    scanner.yylex();
    return 0;
}

このコードで私は得る

flex++ calc.l
calc.l:1: 不正な文字: % calc.l:1: 不明なエラー処理セクション 1
calc.l:1: 不明なエラー処理セクション 1
calc.l:1: 不明なエラー処理セクション 1
calc.l :2: 認識されない '%' ディレクティブ

誰かが私がここで間違っていることを理解するのを手伝ってくれますか? 乾杯

4

2 に答える 2

3

次のようなものを試すことができます。

  • %{ ... %}ファイルの最初の数行に追加する
  • #include <iostream>andを追加using namespace std; (スキャナーを定義しようとする代わりに)
  • ルールセクション%option noyywrapの上に追加
  • just を使用するyylex() (存在しない Scanner のメソッドを呼び出そうとする代わりに)

あなたの例では、次のようになります。

%{
#include <iostream>
using namespace std;
%}

DIGIT   [0-9]
DIGIT1  [1-9]

/* read only one input file */
%option noyywrap

%%
"+"               { cout << "operator <" << yytext[0] << ">" << endl; }
"-"               { cout << "operator <" << yytext[0] << ">" << endl; }
"="               { cout << "operator <" << yytext[0] << ">" << endl; }
{DIGIT1}{DIGIT}*  { cout << "  number <" << yytext    << ">" << endl; }
.                 { cout << " UNKNOWN <" << yytext[0] << ">" << endl; }
%%

int main(int argc, char** argv)
{
    yylex();
    return 0;
}
于 2013-03-14T16:26:26.863 に答える
0

使用しているflex++のバージョンは何ですか? 「関数: fast 字句解析ジェネレーター C/C++ V2.3.8-7 (flex++), based on 2.3.8 and modified by coetmeur@icdc.fr for c++」 (-? オプション) を使用すると、cacl.c は完全に処理されます..

Win32 の場合、このバージョンの Flex++/Bison++ はこちら

于 2013-08-28T08:05:34.383 に答える