コード例 #1
0
ファイル: Test.cpp プロジェクト: Belbesy/JCompiler
int main() {
	cout << "Enter file name for lexical rules" << endl;
	string file = "test";
	cin >> file;
	FileReader *f = new FileReader();
	f->readTheFile((char*) file.c_str());
	f->initializeForNFA();
	NFA *n = new NFA(f->regularExpressions);
	n->defs = f->defs;
	n->matchedExps = f->expressionsID;
	n->createAll();
	cout << "  Start Conversion NFA to DFA " << endl;
	vector<char> all_inputs;
	for (set<char>::iterator i = n->input.begin(); i != n->input.end(); i++) {
		all_inputs.push_back(*i);
	}
	DFA_Builder* DFA = new DFA_Builder(n->NFATable, n->matchedExps, all_inputs);
	cout << " Conversion Function " << endl;
	DFA->NFA_to_DFA();

	cout << "Enter file name for syntax rules" << endl;
	string syntax_file = "syntax_test.txt";
	cin >> syntax_file;
	// read syntax rules file and generate productions
	SyntaxRulesParser* syntaxParser = new SyntaxRulesParser();
	syntaxParser->parse(syntax_file.c_str());
	vector<Production> prods = syntaxParser->productions;
	printf("Print Productions...\n");
		for (size_t i = 0; i < prods.size(); i++) {
			Production prod = prods[i];
			printf("%s -> ", prod.LHS.c_str());
			for (size_t j = 0; j < prod.RHS.size(); j++) {
				for (size_t k = 0; k < prod.RHS[j].size(); k++)
					printf("%s ", prod.RHS[j][k].name.c_str());
				puts("");
				if (j < prod.RHS.size() - 1)
					printf("     |");
			}
			puts("");
		}
		cout << endl;
	// Generate parser
	ParserGenerator* parserGenerator  = new ParserGenerator(syntaxParser->productions , syntaxParser->LHS_index);
	bool isLL1 = parserGenerator->generateParser();
	if(!isLL1)
	{
		cout << "Not LL(1) grammar" << endl;
		return 0;
	}
	// read program source file
	cout << "Enter file name for source code" << endl;
	string src_file = "src_file";
	cin >> src_file;
	Simulator* sim = new Simulator(DFA->DFA, DFA->patterns);
	sim->open_file(src_file.c_str());

	// start reading src file tokens and parse it using LL(1) parser
//	freopen("Parser_output.txt", "w", stdout);
	parserGenerator->SimuParser(sim);
//		string lex;
//		lex = sim->next_token().first;
//		while (!lex.empty())
//		{
//			cout << lex << endl;
//			lex = sim->next_token().first;
//		}

	fclose(stdout);

	//	test_parser();
	//	test_parser_parseFile();
	return 0;
}