⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.cpp

📁 nRF24E1 sample sensor node code
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		else			var=Match(VARID);		if(var->GetID()==comp->GetID())		    var->type=MAINID;		comp->AppendChild(var);		comp->AppendChild(MatchPair('(',')'));		if(NextToken()==':')		{			while(NextToken()!='{')				comp->AppendChild(MatchAny());		}   		m_component_table = m_current_component;    	comp->AppendChild(parse_funct_body());		m_current_component->AddToken(comp);	    m_current_component = NULL;	    m_component_table = NULL;	    comp->line_info=true;        return NULL;	case '(':   // comp(...):...{...}	    comp->type=MAINID;		comp->AppendChild(MatchPair('(',')'));		if(NextToken()==':')		{			while(NextToken()!='{')				comp->AppendChild(MatchAny());		}		if(NextToken()=='{')		{    		comp->AppendChild(parse_funct_body());	    }	    break;	default: // component instantiation	    if(NextToken()==ARRAY)	    {	        CToken* t = CToken::New(comp,UNKNOWN);	        t->text = COMPCXX "array<";	        t->AppendChild(comp);	        comp=t;	        t=Match(ARRAY);	        t->text = " >";	        comp->AppendChild(t);	        array=true;	    }	    var=Match(VARID);	    comp->AppendChild(var);	    comp->AppendChild(Match(';'));	    	    if(m_component_table==NULL)	    {            CTranslation::Error(comp,12,"component instantiation out of range");	    }	    else	    {	        ComponentClass* c = ComponentClass::Lookup(comp_name);	        assert(c!=NULL);	        ComponentInstance* inst  = new ComponentInstance (c);            inst->inst_name = var->GetID();	        	        m_component_table->AddComponent(var->GetID(),inst);	        real_comp->text=real_comp->ReplaceID(inst->class_name.c_str()); 	        inst->array = array;	        	        if(tmpl!=NULL)	        {	            CToken* t=tmpl;	            string arg;	            string id;	            bool flag=true;	            int level=0;	            do	            {	                switch(t->type)	                {	                case ARGID:	                    assert(m_current_component!=NULL);	                    id=t->GetID();	                    for(i=0;i<m_current_component->tmpl_args.size();i++)	                        if(id==m_current_component->tmpl_args[i])	                        {	                            arg += "${";	                            arg += m_current_component->tmpl_args[i];	                            arg += "$}";	                            break;	                        }	                    assert(i<m_current_component->tmpl_args.size());	                    break;	                case ',':	                	if(level==1)	                	{	                    	inst->tmpl_values.push_back(arg);	                    	arg="";	                    }	                    else	                    {		                    arg+=t->text;	                    }	                    break;	                case '>':	                	level--;	                	if(level==0)	                	{	                    	inst->tmpl_values.push_back(arg);	                    	arg="";	                    	flag=false;	                    }	                    else	                    	arg+=t->text;	                    break;	                case '<':	                	if(level!=0)arg+=t->text;	                	level++;	                    break;	                default:	                    arg+=t->text;	                    break;	                }	                t=t->sibling;	            }while(flag==true);                if(m_current_component!=NULL)                {                    if(inst->tmpl_values.size()!=inst->GetClass()->tmpl_args.size())                        CTranslation::Error(comp,20,"number of template parameters unmatched\n");                }	            	        }	    }	    if(m_current_component!=NULL)	    {	        m_current_component->composite=true;	    }	}	return comp;}CToken* Parser::parse_varid(){    CToken* mem_def, *comp, * funct_def;    CToken* h = Match(VARID), *t=NULL;    // printf("parse_varid: (%s, %d) %s\n",h->translation->filename.c_str(),h->lineno,h->text.c_str());    CToken* var = NULL;    CPort* port;    while(NextToken()!='('&&NextToken()!=COMPID&&NextToken()!=';'&&NextToken()!=':')    {        t=MatchAny();        if(t->type==VARID)var=t;        h->AppendChild(t);    }    switch(NextToken())    {    case '(':  //function        funct_def = CToken::New(h,function_definition);        if(!m_program.empty()&&m_program.back()->type==TEMPLATE)        {    	    CToken* tmpl = m_program.back();    	    m_program.pop_back();    	    funct_def->AppendChild(tmpl);        }        funct_def->AppendChild(h);        t=MatchPair('(',')');        funct_def->AppendChild(t);        if(NextToken()==';')        {            funct_def->AppendChild(Match(';'));            return funct_def;        }        else        {            while(NextToken()!='{'&&NextToken()!=';')                funct_def->AppendChild(MatchAny());            if(NextToken()=='{')                funct_def->AppendChild(parse_funct_body());            else                funct_def->AppendChild(Match(';'));        }        return funct_def;    case COMPID: //member function or member variable        comp=Match(COMPID);        comp->type=MAINID;		m_current_component=ComponentClass::Lookup(comp->GetID());		m_component_table=m_current_component;        assert(m_current_component!=NULL);            t=h;        while(t!=NULL)        {            for(unsigned int i=0;i<m_current_component->tmpl_args.size();i++)	        {	            if( t->GetID() == m_current_component->tmpl_args[i])	            {	                t->type=ARGID;	                break;	            }	        }	        if(t->sibling==NULL)t=t->child;else t=t->sibling;        }                mem_def = CToken::New(h,member_definition);        if(!m_program.empty()&&m_program.back()->type==TEMPLATE)        {    	    CToken* tmpl = m_program.back();    	    m_program.pop_back();    	    mem_def->InsertChild(tmpl);    	    mem_def->lineno=tmpl->lineno;        	tmpl->text = string("/*") + tmpl->text;        	assert(tmpl->last_child!=NULL);    	    tmpl->last_child->text += "*/";        }        mem_def->line_info=true;        mem_def -> AppendChild(h);        if(h->child!=NULL) mem_def -> AppendChild(h->child);        h->child = h->last_child = NULL;        mem_def->AppendChild(comp);        if(NextToken()=='<')        {    	    CToken* t = MatchPair('<','>');	        t->text = string("/*") + t->text;	        mem_def->AppendChild(t);	        t = CToken::New(t,UNKNOWN);	        t->text = "*/";	        mem_def->AppendChild(t);        }        mem_def->AppendChild(Match(SCOPE));        var=Match(VARID);        port= m_current_component->LookupPort(var->GetID());        if(port!=NULL)        {        	port->bound=true;        	port->implemented=true;        }        mem_def->AppendChild(var);        while(NextToken()!=';'&&NextToken()!='(')        	mem_def->AppendChild(MatchAny());        if(NextToken()=='(')        {            mem_def->AppendChild(MatchPair('(',')'));            while(NextToken()!='{')                mem_def->AppendChild(MatchAny());            mem_def->AppendChild(parse_funct_body());        }        else        {			mem_def->AppendChild(Match(';'));        }        m_current_component->AddToken(mem_def);        m_current_component=NULL;        m_component_table=NULL;        return NULL;    default:        h->AppendChild(MatchAny());    	break;    }    return h;}    CToken* Parser::parse_typename(){	CToken* t = Match(TYPENAME);	t->text = string("/*") + t->text + "*/";    CToken* mem_def = CToken::New(t,member_definition);    if(!m_program.empty()&&m_program.back()->type==TEMPLATE)    {	    CToken* tmpl = m_program.back();  	    m_program.pop_back();   	    mem_def->InsertChild(tmpl);   	    mem_def->lineno=tmpl->lineno;       	tmpl->text = string("/*") + tmpl->text;       	assert(tmpl->last_child!=NULL);   	    tmpl->last_child->text += "*/";    }        mem_def->AppendChild(t);        if(NextToken()!=COMPID)    {    	while(NextToken()!=';'&&NextToken()!='{')    		mem_def->AppendChild(MatchAny());    	if(NextToken()==';')    		mem_def->AppendChild(MatchAny());    	else    		mem_def->AppendChild(MatchPair('{','}'));    	mem_def->type=UNKNOWN;    	return mem_def;    }        t = Match(COMPID);    t -> type = MAINID;    string comp_name = t->GetID();    mem_def->AppendChild( t );    if(NextToken()=='<');    {    	t=MatchPair('<','>');    	t->text = string("/*") + t->text;    	mem_def->AppendChild(t);    	mem_def->last_child->text += "*/";    }    mem_def->AppendChild(Match(SCOPE));    while(NextToken()!=COMPID) mem_def->AppendChild(MatchAny());    t = Match(COMPID);	if(t->GetID()!=comp_name)	{        CTranslation::Error(t,21,"two component names must match\n");	}	t -> type = MAINID;	mem_def->AppendChild(t);		m_current_component=ComponentClass::Lookup(comp_name);	m_component_table=m_current_component;    assert(m_current_component!=NULL);	    if(NextToken()=='<');    {    	t=MatchPair('<','>');    	t->text = string("/*") + t->text;    	mem_def->AppendChild(t);    	mem_def->last_child->text += "*/";    }    mem_def->AppendChild(Match(SCOPE));    mem_def->AppendChild(Match(VARID));    while(NextToken()!=';'&&NextToken()!='(')      	mem_def->AppendChild(MatchAny());    if(NextToken()=='(')    {        mem_def->AppendChild(MatchPair('(',')'));        while(NextToken()!='{')           mem_def->AppendChild(MatchAny());        mem_def->AppendChild(parse_funct_body());    }    else    {		mem_def->AppendChild(Match(';'));    }        m_current_component->AddToken(mem_def);    m_current_component=NULL;    m_component_table=NULL;    return NULL;}CToken* Parser::parse_funct_body(){    ComponentTable* table=NULL;    if(m_component_table==NULL)    {        m_component_table=table=new ComponentTable;    }	CToken* funct=Match('{');	CToken* port;	int level=0;	int token;	while( (token=NextToken())!= '}' || level!=0)	{		switch(token)		{		case COMPID:		    funct->AppendChild(parse_compid());		    break;   		case CONNECT:		    funct->AppendChild(parse_connect());		    break;		case PORTID:		    port=Match(PORTID);		    if(NextToken()=='(')		    {		        port -> AppendChild(MatchPair('(',')'));		    }		    else		    {		        port -> type = UNKNOWN;		    }		    funct->AppendChild(port);		    break;		default:		    if(token=='{')level++;		    if(token=='}')level--;		    funct->AppendChild(MatchAny());		}	}	funct->AppendChild(Match('}'));	if(table!=NULL)	{	    if(table->Size()!=0&&CTranslation::ErrorCount()==0)	    {	        WriteProgram();            fprintf(m_outfile,"#include \"%s\"\n",m_hfilename.c_str());	        table->GenerateCode(m_outfile,NULL,true);	        table->GenerateCode(m_outfile,NULL,false);	    }	    delete table;	    m_component_table=NULL;	}	return funct;}void Parser::WriteProgram(){    for(unsigned int i=0;i<m_program.size();i++)    {        m_program[i]->Write(m_outfile);    }    m_program.clear();}int main(int argc, char* argv[]){	g_debug = false;	g_lineinfo = true;		const char* filename=NULL;	for(int i=1;i<argc;i++)	{		if(strcmp(argv[i],"-verbose")==0)		{			g_debug=true;		}		else if(strcmp(argv[i],"-noline")==0)		{			g_lineinfo=false;		}		else if(strlen(argv[i])>3&& argv[i][0]=='-' && (argv[i][1]=='d'||argv[i][1]=='D') )		{			char* p = strchr(argv[i],'=');			if(p!=NULL)			{				*p=0;				string id1 = argv[i]+2;				string id2 = p+1;				//printf("#define %s %s \n",id1.c_str(), id2.c_str());				g_macros[id1]=id2;			}		}		else if(filename==NULL)filename=argv[i];	}  	if(filename==NULL)    {        printf("error: no input file specfied\n");        return 1;    }  	Parser parser;  	return parser.parse(filename);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -