简体   繁体   中英

Identifying C 11 keywords in Strings (string.at() issues)

I have been successful in searching for c++11 keywords in code snippets provided by the professor in the main function. Although when printed keywords are printed if they are included within other words like do is printed when donuts exist when the string snippet. So I created two if statements which identify the next char in a string and whether or not it is a space character and if it is it proceeds to print it. But i am having an issue in the x.at() statement which compiles but does not run. Anyone know what i am doing wrong?

// C++11 Keyword Search Program

#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <vector>
using std::vector;
typedef vector<string> string_vector;

// return the C++11 keywords that appear in the code snippet
string_vector find_keywords(const string snippet)
{
  const string x=snippet;
  int len=x.length();
  int back=x.back();
  int last_char, first_char;
  vector<string> answer;
  string keywords[]= {"alignas", "alignof", "and", "and_eq", "asm", "auto", "bitand", 
      "bitor", "bool", "break", "case", "catch", "char", "char16_t", "char32_t", "class", "compl", "const", "constexpr",
      "const_cast", "continue", "decltype", "default", "delete", "do", "double", "dynamic_cast", "else", "enum", 
      "explicit", "export", "extern", "false", "float", "for","friend", "goto", "if", "inline", "int", "long", "mutable",
      "namespace", "new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected public", 
      "register", "reinterpret_cast","return","short","signed","sizeof","static","static_assert","static_cast","struct",
      "switch", "template", "this","thread_local","throw","true","try","typedef","typeid","typename","union","unsigned",
      "using","virtual","void", "volatile","wchar_t","while","xor","xor_eq"};



     for(int i=0;i<83;++i)
  {
    string test=keywords[i];
    size_t found = x.find(test);
    unsigned int testsize=test.size();
    int test1=0,test2=0;

    if(found!=string::npos)
    {
        char a=x.at(found-1);
        char b=x.at(found+testsize);
        int c=isalpha(a);
        int d=isalpha(b);
        if (c>=1)
            test1=1;
        if (d>=1)
            test2=1;
    }   
    if(found!=string::npos && test1==0 &&test2==0)
        cout<<test<<" ";        
  }
return answer;
}

int main (int argc, char* const argv[])
{
  const string snips[] =
  {
    "int i = 0;",
    "i += 1;",
    "return (double) x / y;",
    "char foo(char bar);",
    "double bar(void) const",
    "garbage = fuzz + lint;"
  };

  const string_vector snippets(snips,snips + 6);

  for (auto snippet : snippets)
  {
    cout << "**Snippet**  " << snippet << endl << "**Keywords** ";
    for (auto keyword : find_keywords(snippet))
    {
      cout << ' ' << keyword;
    }
    cout << endl << endl;
  }

  return EXIT_SUCCESS;
}
 last_char=found+len-1; char2=x.at(last_char); 

As soon as found > 0 , then last_char >= x.length() and your at() call attempts to read out of bounds.

Note also that a space is not the only possible token delimiter. One routinely writes things like break; , or return; , or while(condition) , or f(int[]) , or this->member . All of these contain a keyword that is not surrounded by spaces.

And, a string that looks like a keyword may appear in a string literal, "like this" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM