///////////////////////////////////////////////////////////////////// // ScopeAnalysisDemo.cpp - demo recursive descent scope analysis // // ver 1.0 // // // // Language: Visual C++ 2008, SP1 // // Platform: Dell Precision T7400, Vista Ultimate SP1 // // Application: Prototype for CSE687 Pr1, Sp09 // // Author: Jim Fawcett, CST 4-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // //////////////////////////////////////////////////////////////////// #include #include "ScopeAnalysisDemo.h" void scoper::showStack() { std::stack temp; std::cout << "\n scope stack: "; while(scopeStk.size() > 0) { temp.push(scopeStk.top()); scopeStk.pop(); } while(temp.size() > 0) { scopeStk.push(temp.top()); std::cout << temp.top() << " "; temp.pop(); } } void scoper::walkScopes() { // show the current contents of scope stack showStack(); // start scope analysis std::string tok = "anonymous"; while(se.get()) { std::string lastTok = se[se.length()-1]; if(lastTok == "}") // leaving current scope { scopeStk.pop(); showStack(); if(scopeStk.size() == 0) std::cout << "\n"; // puts space between global scope outputs return; } // The code below tries to capture the scope identifier. // Several special cases are not handled effectively, e.g.: // operator[](...), for(int i=0; i 0) tok = se[len-1]; else tok = "anonymous"; std::cout << "\n" << se.show(); scopeStk.push(tok); walkScopes(); continue; } if(se.length() > 2) // class X {, enum T {, etc. tok = se[se.length()-3]; else if(se.length() > 1) // do {, catch {, etc. tok = se[se.length()-2]; std::cout << "\n" << se.show(); scopeStk.push(tok); walkScopes(); } } } int main(int argc, char* argv[]) { std::cout << "\n Code Scope Analysis Demo\n " << std::string(26,'=') << std::endl; if(argc < 2) { std::cout << "\n please enter name of file to process on command line\n\n"; return 1; } for(int i=1; i