#ifndef GRAMMAR_H #define GRAMMAR_H /////////////////////////////////////////////////////////////// // grammar.h - detect and format semi-expressions with // // ver 2.3 selected syntactic forms // // // // Language: Visual C++, ver 6.0 // // Platform: Micron Dual Pentium Pro 200, Win NT 4.0 // // Application: CSE784 Project #1, Fall 1999 // // Author: Jim Fawcett, CST 2-187, Syracuse Univ. // // (315) 443-3948, fawcett@ecs.syr.edu // /////////////////////////////////////////////////////////////// /* Module Operations ================= Objects of the grammar class detect some specific sytax forms, e.g., function definitions, global data declarations, static data declarations, and class and struct declarations. When a semi-expression is found to have a specific form, it can be displayed in a nicely formatted form using format function. Public Interface: ================= functionDef fDef; // function detector object globalDeclar gDec; // global declar. detector staticDeclar sDec; // static declar. detector classDeclar cDec; // class/struct declar. detector. localDataDeclar dDec; // local data declaration pointerDeclar pDec; // declaration of a non-const ptr if(fDef(se)) ... // apply function detector string fmt = fDef.format(se); // format se for output as code fDef.doStrip(); // reduce full signature to name detector::showSummary(outStream); // show summary statistics int nfl = detector::getTotalFuncLines();// return total lines processed int ncc = detector::getTotalCyclo(); // return total cyclo processed grammar gram; // grammar object gram.analyze(fDef); // register det with grammar object gram.doAnalyze(se); // analyze semi-expression for det's // registered with analyze(...) gram.processFile(inStream); // run doAnalyze() on all semiExp int tl = grammar::getTotalLines(); // get number of lines read so far int fl = grammar:: */ // /////////////////////////////////////////////////////////////// // build process // /////////////////////////////////////////////////////////////// // Required Files: // // grammar.h, grammar.cpp, cmdline.h, cmdline.cpp // // Compile Command: // // cl -GX -DTEST_GRAMMAR grammar.cpp cmdline.cpp // // semi.cpp tok.cpp // /////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 2.3 : 16 Jun 2006 - modified funcdef::operator() to avoid declaring a .Net attribute as a function - added collectSig() function ver 2.2 : 18 Sep 2000 - fixed stripSignature to collect all of operator() ver 2.1 : 21 Apr 2000 - fixed bug which falsely reports returned pointers for operator* - added static member void nestedComments(bool pred) to pass option to semiExp and token ver 2.0 : 29 Feb 2000 - fixed bug in class analysis that skipped all inline function definitions see classDeclar::lookForErrors(semiExp&) ver 1.1 : 26 Feb 2000 - cosmetic modifications to this page ver 1.0 : 14 Dec 99 - first release Planned Modifications ===================== - fix bug due to nested classes or structs - confuses grammar::lookForErrors 4 May 2000 */ // #include #include #include #include "semi.h" /////////////////////////////////////////////////////////////// // declaration of base syntax detector class // /////////////////////////////////////////////////////////////// class detector { public: detector(); virtual ~detector(); virtual bool operator()(semiExp &se)=0; virtual void doStrip(); virtual string format(semiExp &se); static void showSummary(std::ostream &out); static int getTotalFuncLines(); static int getTotalCyclo(); protected: static bool strip; static std::vector limitsExceeded; static std::vector globals; static std::vector publicData; static std::vector pointersReturned; static int totalFuncLines; static int totalCyclo; static int maxFuncLines; static int maxCyclo; }; inline detector::detector() { } inline detector::~detector() { } inline void detector::doStrip() { strip = true; } inline int detector::getTotalFuncLines() { return totalFuncLines; } inline int detector::getTotalCyclo() { return totalCyclo; } // /////////////////////////////////////////////////////////////// // declaration of class to detect function definitions // /////////////////////////////////////////////////////////////// class functionDef : public detector { public: functionDef(); virtual bool operator()(semiExp &se); virtual std::string format(semiExp &se); protected: bool isTemplate(semiExp& se); string collectSig(semiExp& se); bool key(std::string &s); int cyclo(semiExp &se); std::string collectMetrics(semiExp &se, int &cc, int &lines); std::string stripSignature(semiExp &se); static std::string funcName; }; /////////////////////////////////////////////////////////////// // declaration of class to detect global data declarations // /////////////////////////////////////////////////////////////// class globalDeclar : public detector { public: virtual bool operator()(semiExp &se); virtual std::string format(semiExp &se); protected: std::string stripDeclar(semiExp &se); }; class classDeclar : public detector { public: virtual bool operator()(semiExp &se); virtual std::string format(semiExp &se); protected: string lookForErrors(semiExp &se); }; // /////////////////////////////////////////////////////////////// // declaration of class to detect static data declarations // /////////////////////////////////////////////////////////////// class staticDeclar : public detector { public: virtual bool operator()(semiExp &se); virtual std::string format(semiExp &se); protected: std::string stripDeclar(semiExp &se); }; /////////////////////////////////////////////////////////////// // declaration of class to detect data declarations // /////////////////////////////////////////////////////////////// class localDataDeclar : public detector { public: virtual bool operator()(semiExp &se); virtual std::string format(semiExp &se); protected: std::string stripDeclar(semiExp &se); }; /////////////////////////////////////////////////////////////// // declaration of class to detect pointer declarations // /////////////////////////////////////////////////////////////// class pointerDeclar : public detector { public: virtual bool operator()(semiExp &se); virtual std::string format(semiExp &se); protected: std::string stripDeclar(semiExp &se); }; // /////////////////////////////////////////////////////////////// // declaration of class to manage the detection process // /////////////////////////////////////////////////////////////// class grammar { public: void analyze(detector &d); void processFile(std::istream &in); void doAnalyze(semiExp &s); static int getTotalLines(); static void nestedComments(bool pred); private: std::vector detect; static int totalLines; static bool _nestedComments; }; inline int grammar::getTotalLines() { return totalLines; } inline void grammar::nestedComments(bool pred) { _nestedComments = pred; } class moduleName { public: static string get() { return moduleName_; } static void set(std::string name) { moduleName_ = name; } private: static std::string moduleName_; }; #endif