Скачиваний:
20
Добавлен:
01.05.2014
Размер:
2.35 Кб
Скачать
#ifndef SYNTACTICPARSER_HPP_
#define SYNTACTICPARSER_HPP_

#include <istream>

#include "Poliz.hpp"
#include "TokenParser.hpp"

namespace Parser_impl {
	

class SyntaticParser {
	
private:	
	
	PolizProgram* prog;
	TokenParser* tokens;
	
	Function* currfunc;
	LookupTable<Variable>* currvarttable;
	
	bool tokenIs(TokenType type, long val)
	{
		return 	token()->type == type && 
				token()->getLong() == val;
	}
	
	void checkProgram()
	{
		int size = prog->functions.getSize();
	
		int main_count = 0;
		
		for (int i=0; i<size; ++i){
			Function& f = prog->functions.get(i);
			if (!f.declared)
				error("Function '" + f.name + "' is not defined");
			if (f.name == "main" && f.return_type == INT){
				prog->main = i;
				++main_count;
			}
		}
		if (main_count > 1)
			throw SyntacticParserException("There are more than one 'int main(...)' function");
		
		if (main_count == 0)
			throw SyntacticParserException("There is no 'int main(...)' function");
	}
	
	bool containsId(const std::string id)
	{		
		return currfunc->local.contains(id) || prog->global.contains(id);
	}
	
	PolizInstruction makeVarPoliz(const std::string id)
	{
		PolizInstruction v(TVariable);		
		if (currfunc->local.contains(id)){
			// Local variable;			
			v.local 		= true;
			v.val			= currfunc->local.getPos(id);
		} else {
			// Global variable			
			v.local 		= false;
			v.val			= prog->global.getPos(id);
		}
		return v;
	}
	
    void error(const char* message)
    {
        throw SyntacticParserException(message, tokens->getLineNumber());
    }
    
    void error(const std::string str)
    {
    	error(str.c_str());
    }
    
    Token* token()
    {
        return tokens->currentToken();
    }
    
    void assert_token(TokenType _type, long _val)
    {
    	if (token()->type != _type || (_val != 0 && token()->getLong() != _val))
    		throw SyntacticParserException(_val, tokens->getLineNumber() );
    }
    
    // Non terminals
    void Program();
    void Vardef();
    void Func();
    void BL();
    void ST();
    void E();
    void E1();
    void E2();
    void E3();
    void E4();
    void E5();
    void E6();
    void E7();
    
public:
	
	PolizProgram* parse(std::istream& is);	
	
};
	
	
}


#endif /*SYNTACTICPARSER_HPP_*/
Соседние файлы в папке MyLanguage