返回

c++-如何在flex中重新启动启动状态数据

发布时间:2022-08-03 15:36:06 232
# golang

我在flex中创建了一个开始条件(字符串),一切正常。然而,当我两次解析同一个字符串时,使用开始条件的元素消失了。我该怎么解决?请帮帮我flex文件

   
%option stack noyywrap
%{
extern int lineNumber; // definie dans prog.y, utilise par notre code pour \n
#include "h5parse.hpp"
#include 
#include 

using namespace std;
extern string initialdata;
extern string pdata;
extern bool loop;
string val;
string compile(string content);
string compilefile(string path);
void runwithargs(int argc ,char ** argv);
int saveoutput(string compileddata ,string outputpath="");

%}
%x strenv
i_command       @include
e_command       @extends
l_command       @layout
f_command       @field

command {i_command}|{e_command}|{l_command}|{f_command}

%%
"\""       { val.clear(); BEGIN(strenv); }
"\""    { BEGIN(INITIAL);sprintf(yylval.str,"%s",val.c_str());return(STRING); }
<> { BEGIN(INITIAL); sprintf(yylval.str,"%s",val.c_str());return(STRING); }
.       { val+=yytext[0]; }
{command}       {sprintf(yylval.str,"%s",yytext);return (COMMAND);}
"("             { return LPAREN; }
")"             { return RPAREN; }
"{"             { return LBRACE; }
"}"             { return RBRACE; }
.|\n            {yylval.c=yytext[0];return TXT; }
%%

//our main function 
int main(int argc,char ** argv)
{

if(argc>1)runwithargs(argc,argv);// if there are arguments run with them

system("pause");//don't quit the app at the end of assembly
return(0);
}

//run h5A by using  arguments
void runwithargs(int argc ,char ** argv)
{
    if(argc == 2)
        saveoutput(compilefile(argv[1]));
    
}

//assemble a string 
string compile(string content)
{

do 
{
loop=false;
pdata.clear();
 YY_BUFFER_STATE b =yy_scan_string(content.c_str());

yyparse();
content=pdata; 

 }while(loop==true);
return content;
}

//assemble file
string compilefile(string path)
{
string data;
ifstream inputfile(path,ios::in|ios::binary|ios::ate);
int length  = inputfile.tellg();           
inputfile.seekg(0, std::ios::beg);  
char * buffer = new char[length];// allocate memory for a buffer of appropriate dimension
inputfile.read(buffer, length);// read the whole file into the buffer
inputfile.close();  
cout<<"start assembly : "<<path<<endl;
return compile(string(buffer));
}

//save  assembled  file to a specified path 
int saveoutput(string compileddata ,string outputpath)
{

outputpath=(outputpath=="")?"output":outputpath;
ofstream outputfile  ("output");
//dhow the compiled data in console if we're in debug 
outputfile<<compileddata;
cout<<compileddata<<endl;
cout<<"operation terminated successfuly , output at :"
    <<outputpath<<endl;

return 0;

}

bison file

%{
#include 
#include 
#include
#include
using namespace std;
typedef void* yyscan_t;
int lineNumber; // notre compteur de lignes
map <string,string> clayouts;
void yyerror ( char const *msg);
typedef union YYSTYPE YYSTYPE;


void yyerror ( char const *msg);
int yylex();
bool loop;
string pdata="";
%}
/* token definition */
%token STRING 
%token COMMAND
%token LPAREN RPAREN  LBRACE RBRACE
%token TXT
%union { char c; char str [0Xfff]; double real; int integer; }
%type TXT;
%type STRING COMMAND;
%start program
%%
program:value | command_call |txt | program program ;
value: STRING {pdata+='\"'+$1+'\"'; };
command_call : COMMAND LPAREN STRING  RPAREN   { 
    
    if(string($1)=="@field")
    {
         cout<<"define field :"<<$3;
    }
    else if(string($1)=="@include")
    {
        ifstream t;
        int length;
        char * buffer;
        t.open($3);     
        t.seekg(0, std::ios::end);    
        length = t.tellg();           
        t.seekg(0, std::ios::beg);  
        buffer = new char[length];    
        t.read(buffer, length);       
        t.close(); 
        pdata+=buffer;
    }
    else if (string($1)=="@layout")
    {
        cout<<"define layout for field "<<$3;
    }
    else if (string($1)=="@repeat")
    {
        cout<<"reapeat instruction"<<$3;
    }
    else
    {
        cout<<"extend with : "<<$3;
         ifstream t;
        int length;
        char * buffer;
        t.open($3);     
        t.seekg(0, std::ios::end);    
        length = t.tellg();           
        t.seekg(0, std::ios::beg);  
        buffer = new char[length];    
        t.read(buffer, length);       
        t.close(); 
    }
    loop=true;
     };//LPAREN RPAREN ;
txt: TXT {pdata+=$1;};
%%
void yyerror (const char *msg)
{
    cout<<msg;
}

 

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像
下一篇
Javascript从字符串中获取数字 2022-08-03 13:49:41