예제 #1
0
파일: main.cpp 프로젝트: audduk/lex_idl
int main(int argc, char* args[]) {
  extern FILE *yyin;  // файл module_lex.cpp
  extern QString resultH, resultCpp; // файл generator.cpp
  yyin = 0; //по умолчанию будет заполнено в функции yylex() в значение stdin

  const char *tsmName = args[0]; // имя запускаемого файла
  //обрабатываем входные параметры
  QString resname, filename, prefix;
  for ( int i=1; i<argc; ++i ) {
    if ( args[i][0] == '-' ) { // настроечный параметр
      switch (args[i][1]) {
      case 't': // установка типа формируемого выхода (клиент или сервер)
        ++i;
        if (strcmp(args[i], "client" )==0)
          tType = ttClient;
        else if (strcmp(args[i], "server" )==0)
          tType = ttServer;
        break;
      case 'r': // установка наименования результирующих файлов
        ++i;
        resname = args[i];
        break;
      case 'p':   // установка префикса выходного файла
        ++i;
        prefix = args[i];
        break;
      //case 's': // задаем имя файлов шаблонов
      // case 'x':
        // ...
        //break;
      case 'h': // запрос справки по использованию
      default:  // неизвестный параметр
        badUsage(tsmName);
        return 0;
      }
    } else { // последний параметр - имя файла для обработки
      filename = args[i];
#ifdef TSMDEBUG
      qDebug()<<"File to process "<<args[i];
      qDebug()<<"make "<< ( (tType==ttClient) ? "client" : "server" );
#endif
      break;
    }
  }
  if ( resname=="" ) {
    QRegExp rx("(\\w+)\\.\\w+");
    if ( rx.indexIn(filename)>=0 ) {
      resname = rx.cap(1);
    } else {
      resname = filename;
    }
  }
  if (prefix=="") prefix = "tsm_";
  QRegExp rx("^" + prefix + "\\w+");
  if ( !rx.exactMatch(resname) )  // если начинается не с $prefix
    resname = prefix + resname;
  qDebug() << "RES NAME = " << resname;

  if ( tType == ttUndefined || filename == "" ) {
    badUsage(tsmName);
    return -1;
  }

  yyin = fopen(filename.toLocal8Bit().data(), "r");
  if (yyin == 0) {
    qCritical() << "Unable to open file " << filename;
    return -1;
  }

  preDefined();
  int result = yyparse();

  if (yyin) fclose(yyin);

#ifdef TSMDEBUG
  //qDebug() << resultH << "\n";
  //qDebug() << resultCpp << "\n";
#endif

  QString hGuardian = "_" + resname.toUpper() + "_H_\n";

  QFile fH(resname+".h"), fCpp(resname+".cpp");

  fH.open(QIODevice::WriteOnly);
  QTextStream outH(&fH);
  outH << "#ifndef " << hGuardian;
  outH << "#define " << hGuardian << "\n";
  outH << "#include <QList>\n";
  outH << "#include <tsm/tsminterface.h>\n\n";
  //outH << "class Command;\n" ;    // -> см. файл <tsm/commander.h>
  //outH << "namespace TSM { class Commander; }\n\n" ;
  outH << resultH;
  outH << "\n#endif // " << hGuardian;

  fCpp.open(QIODevice::WriteOnly);
  QTextStream outCpp(&fCpp);
  outCpp << "#include <cmdparser.h>\n";
  outCpp << "#include <tsm/commander.h>\n\n";
  outCpp << "#include \"" + resname + ".h\"\n\n";
  outCpp << resultCpp << "\n";

  return result;
}
예제 #2
0
파일: main.cpp 프로젝트: TrygveS/sweet.hpp
int main(int argc, char** argv) {
	std::string inputFile;
	std::string outAstH("ast.hpp");
	std::string outAstS("ast.cpp");
	std::string outPrsH("parser.hpp");
	std::string outPrsS("parser.cpp");
	std::string outErrH("error.hpp");
	std::string outErrS("error.cpp");
	std::string outVH("visitor.hpp");
	std::string outVS("visitor.cpp");
	std::string outovH("outvisitor.hpp");
	std::string outovS("outvisitor.cpp");
	std::string outdvH("dotvisitor.hpp");
	std::string outdvS("dotvisitor.cpp");
	std::string outMvH("multivisitor.hpp");
	std::string outMvS("multivisitor.cpp");
	std::string outLamH("lambdavisitor.hpp");
	std::string outLamS("lambdavisitor.cpp");
	std::string outH("visitorinclude");
	std::string outPrefix;
	sweet::Options opts(argc, argv);
	opts.get("-i", "--inputFile", "The grammar file to parse", inputFile);
	opts.get("-ah", "--astoutputheader", "Output file for ast header", outAstH);
	opts.get("-as", "--astoutputsource", "Output file for ast source", outAstS);
	opts.get("-ph", "--parseroutputheader", "Output file for parser header", 
		outPrsH);
	opts.get("-ps", "--parseroutputsource", "Output file for parser source", 
		outPrsS);
	opts.get("-eh", "--erroroutputheader", "Output file for error header", 
		outErrH);
	opts.get("-es", "--erroroutputsource", "Output file for error source", 
		outErrS);
	opts.get("-vh", "--visitoroutputheader", 
		"Output file for tree visitor header", outVH);
	opts.get("-vs", "--visitoroutputsource", 
		"Output file for tree visitor source", outVS);
	opts.get("-oh", "--outvisitoroutputheader", 
		"Output file for std::cout visitor header", outovH);
	opts.get("-os", "--outvisitoroutputsource", 
		"Output file for std::cout visitor source", outovS);
	opts.get("-dh", "--dotvisitoroutputheader", 
		"Output file for Dot file visitor header", outdvH);
	opts.get("-ds", "--dotvisitoroutputsource", 
		"Output file for Dot file visitor source", outdvS);
	opts.get("-mh", "--multivisitoroutputheader", 
		"Output file for Multi visitor header", outMvH);
	opts.get("-ms", "--multivisitoroutputsource", 
		"Output file for Multi visitor source", outMvS);
	opts.get("-lh", "--lambdavisitorheader", 
		"Output file for Lambda visitor header", outLamH);
	opts.get("-ls", "--lambdavisitorsource", 
		"Output file for Lambda visitor source", outLamS);
	opts.get("-op", "--outputprefix", 
		"All output files will be prefixed with this string", outPrefix);
	opts.get("-in", "--visitorinclude", 
		"Visitor method declarations", outH);
	opts.finalize();

	if(!outPrefix.empty()) {
		outAstH = outPrefix + outAstH;
		outAstS = outPrefix + outAstS;
		outPrsH = outPrefix + outPrsH;
		outPrsS = outPrefix + outPrsS;
		outErrH = outPrefix + outErrH;
		outErrS = outPrefix + outErrS;
		outVH = outPrefix + outVH;
		outVS = outPrefix + outVS;
		outovH = outPrefix + outovH;
		outovS = outPrefix + outovS;
		outdvH = outPrefix + outdvH;
		outdvS = outPrefix + outdvS;
		outMvH = outPrefix + outMvH;
		outMvS = outPrefix + outMvS;
		outLamS = outPrefix + outLamS;
		outLamH = outPrefix + outLamH;
		outH = outPrefix + outH;
	}

	if(inputFile.empty()) {
		return 0;
	}

	RuleStore store;
	RuleParser parser(inputFile, store);
	parser.parse();
	std::ofstream astH(outAstH);
	std::ofstream astS(outAstS);
	std::ofstream prsH(outPrsH);
	std::ofstream prsS(outPrsS);
	std::ofstream errH(outErrH);
	std::ofstream errS(outErrS);
	std::ofstream visS(outVS);
	std::ofstream visH(outVH);
	std::ofstream ovisH(outovH);
	std::ofstream ovisS(outovS);
	std::ofstream dvisH(outdvH);
	std::ofstream dvisS(outdvS);
	std::ofstream mvisH(outMvH);
	std::ofstream mvisS(outMvS);
	std::ofstream lamH(outLamH);
	std::ofstream lamS(outLamS);
	std::ofstream inH(outH);
	Output out(prsS,prsH,astS,astH,errS,errH,visS,visH,ovisH,ovisS,dvisH,dvisS,
		mvisH,mvisS,lamS,lamH,inH
	);
	RecurDec rd(store, out);
	rd.computeFirstSet();
	//std::cout<<store.first<<std::endl;

	for(auto& it : store.token) {
		LOG("%s\n", it.second);
	}
	for(auto& it : store.rules) {
		LOG("%s : %s\n", it.first, it.second);
	}
	std::cout<<std::endl;
	sweet::enableLogger(64);
	rd.gen();
	LOG("%s\n%s\n", outPrsH, outPrsS);
	return 0;
}