无法识别 yylex() 产生的错误

Cannot identify the error yylex() produces

我正在尝试为类 Pascal 语言制作一个 .l 文件。当我 运行 将它与 g++ 结合使用时,它在使用不同文件的解析过程中的第 20 步之后就崩溃了,一个定义多了一个,定义少了一个。我试图得到错误,但它只发送 3 个零。我错过了什么地方吗?

这是 Utile.h 文件

#include <map>
#include <iterator>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

std::vector<std::string> TS;  


typedef struct {
 int n;
 int elem[20][2];
} FIP;




void addFIP(int code, int posTS, FIP& f){
 f.elem[f.n][0]=code;
 f.elem[f.n++][1]=posTS;
}

FIP fip;
int pozTS=0;

void printFIP(FIP& f){
    ofstream fipFile;
    fipFile.open("FIP.txt");
    cout<<"nr elem fip"<<f.n<<endl;
    for(int i=0;i<f.n;i++)
        fipFile<<f.elem[0]<<" "<<f.elem[1]<<endl;
    fipFile.close();
}

这是我的 specs.l 文件

%{

#include "Utile.h"
%}

%option noyywrap
%option caseless



LETTER      [A-Za-z]
NR_ZEC  [0-9]
NR_NZ    [1-9] 
ZERO        [0]      
ID  {LETTER}({LETTER}|{NR_ZEC})*
NR_BASE10   {NR_NZ}+{NR_ZEC}*|{ZERO}
NR_REAL     {NR_BASE10}"."{NR_BASE10}*
DELIMIT     [;.,:]
SIR_CAR     [\"][^\n]*[\"]
CARACTER    "'"[^\n]"'"
ERR_NR_START [0-9]+[a-zA-Z0-9]*
DOT             "\."
COLON           "\:"
SEMICOLON       "\;"
COMMA           "\,"
PLUS            "\+"

%%

[ \t\n]
[0-9]+[a-zA-Z]+[a-zA-Z0-9]* {printf("Eroare - identificator incepe cu cifra %s \n", yytext);}



read        {addFIP(19,-1,fip);printf("%s\n", yytext);}
write       {addFIP(20,-1,fip);printf("%s\n", yytext);}
then        {addFIP(21,-1,fip);printf("%s\n", yytext);}
variabiles   {addFIP(22,-1,fip);printf("%s\n", yytext);}

"="        {addFIP(200,-1,fip);printf("%s\n", yytext);}
\(         {addFIP(101,-1,fip);printf("%s\n", yytext);}
\)         {addFIP(102,-1,fip);printf("%s\n", yytext);}
\;         {addFIP(103,-1,fip);printf("%s\n", yytext);}   
\,         {addFIP(104,-1,fip);printf("%s\n", yytext);}
\.         {addFIP(105,-1,fip);printf("%s\n", yytext);}
\:         {addFIP(106,-1,fip);printf("%s\n", yytext);}    

"+"     {addFIP(300,-1,fip);printf("%s", yytext);}
\-      {addFIP(301,-1,fip);printf("%s", yytext);}

integer     {addFIP(29,-1,fip);printf("%s", yytext);}
real        {addFIP(30,-1,fip);printf("%s", yytext);}




{ID}    {addFIP(0,pozTS++,fip);printf("%s\n", yytext);}
{NR_BASE10} {
             addFIP(1,pozTS++,fip);
             printf("\n%d\n", 1);
        }
{NR_REAL}   {
            addFIP(1,pozTS++,fip);
            printf("\n%d\n", 1);
        }
"'"[^\n]"'" {
            addFIP(1,pozTS++,fip);
            printf("\n%d\n", 1);
        }
{SIR_CAR}   {addFIP(1,pozTS++,fip);printf("\n%d\n", 1);}



. printf("Error %s\n", yytext);

%%
void yyerror (char const *s) {
   fprintf (stderr, "%s\n", s);
 }



extern FILE *yyin;




main(int argc, char *argv[])
{
    yyin= fopen (argv[1] , "r");
    yylex();
    cout<<yytext;
    fclose(yyin);


}

我选择打印 yytext 希望它能帮助我找出问题所在,但运气不好

另外,如果它对我有帮助,我 运行 这样

flex specs.l
g++ lex.yy.c
a.exe test.txt

您的 FIP 结构只有 20 个条目的空间,并且 addFIP 在添加新条目之前不会检查它是否已满。所以在大约 20 个标记后,您将开始覆盖随机内存。

既然你用的是C++,为什么不直接用std::vector呢?您可以只 emplace_back 个令牌,您甚至不需要跟踪有多少令牌,因为 std::vector 负责所有簿记工作。

话虽如此,创建令牌向量的理由很少。通常你一次只处理一个令牌。