Parser

Specifications


CMakeLists.txt 	

Grammar.cpp:Grammar check	
Grammar.h 
	
Parser.cpp:The main part of Parser
Parser.h 		

1.Use Parser.cpp read token from Scanner. 
2.Use Grammar.cpp to check the expands of nonterminal and build the rule log
3.If nonterminal expand correct go next step

Source Listing


Some example of parser code

//this is only used to move ahead to the next token
bool Grammar::match(){
	return _currentTokens->moveAhead();
}

//save a rule number in _ruleLog
void Grammar::logRule(int rule){
	_ruleLog.push_back(rule);
}

/*  Rule 94, 95, 96, 97, 98
Parses strings in the language generated by
 -> "*"|"/"|"div"|"mod"|"and"  */
bool Grammar::multiplyingOperator(SemanticRecord& record)
{
	switch (nextTokenType())
	{
	case MP_TIMES:{
		logRule(94);
		string command = record.showNextOperand().type() == FloatData ? "MULSF" : "MULS";
		record.addOperand(CommandOperand(command));
		match();
		return true;}
	case MP_FLOAT_DIVIDE:
		logRule(95);
		record.addOperand(CommandOperand("DIVSF", FloatData));
		match();
		return true;
	case MP_DIV:
		logRule(96);
		record.addOperand(CommandOperand("DIVS"));
		match();
		return true;
	case MP_MOD:
		logRule(97);
		record.addOperand(CommandOperand("MODS"));
		match();
		return true;
	case MP_AND:{
		logRule(98);
		string command = record.showNextOperand().type() == FloatData ? "ANDSF" : "ANDS";
		record.addOperand(CommandOperand(command));
		match();
		return true; }
	default:
		error( TypeList() << MP_TIMES << MP_FLOAT_DIVIDE << MP_DIV << MP_MOD << MP_AND );
	}
	return false;
}