列ベクトルC++で保存したテキストファイルを形成し、このファイルがあるチェックは、私が解析されたC以下のように++プログラムのテキストファイルを持っている
element
unit
element
cpp:include
pcdata
#
element
cpp:directive
pcdata
include
element
cpp:file
pcdata
<iostream>
element
using
pcdata
using
element
namespace
pcdata
namespace
element
name
pcdata
std
pcdata
;
element
class
pcdata
class
element
name
pcdata
Person
element
block
pcdata
{
element
private
element
public
pcdata
public:
element
decl_stmt
element
decl
element
type
element
name
pcdata
string
element
name
pcdata
profession
pcdata
;
element
decl_stmt
element
decl
element
type
element
name
pcdata
int
element
name
pcdata
age
pcdata
;
element
constructor
element
name
pcdata
Person
element
parameter_list
pcdata
()
element
member_init_list
pcdata
:
element
call
element
name
pcdata
profession
element
argument_list
pcdata
(
element
argument
element
expr
element
literal
pcdata
......
......
(.txtファイルの内容として、すべての行を考えます) srcmlツールを使用して解析され、xmlタグが削除されました。
1行ごとにファイルを読み込む必要があります(単語でも単語でも問題ありません)。これを自分のコードで定義された文字列値と比較する必要があります。ファイルから読み込んだ任意の単語が「要素」である場合たとえば、次の単語が「クラスである場合は、次の単語が再び「PCDATA」であるかどうかを確認、次の単語が再び「クラス」であるかどうかを確認し、その後のようなワードパターンをチェック「再び次の単語がであれば、 『PCDATA人『(この 『、この場合にはをファイルすなわちから読み取る次の単語を保存する』 『次の単語がある場合は、再度』名前』次の単語がある場合は、再度』要素29〜36行目)をベクトル/配列に変換します。
2-は、そのブロックのためにそれを保存し続ける生じる「{」ポイント1で述べたようなファイルから次いでまで、パターンを探し、「}」、すなわち読み取り値がある場合、ブロック内のステップ1を実行してください。
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
class xmlVector
{
public:
void browseFile(); // Read file and load it to vector
void printVector(); // Print loaded vector
typedef vector <string> vector_str; // Define vector to store the File.
vector<string>::iterator vec_it; // used in traversal
public:
string line;
string class_name;
string &temp;
};
class classList : public xmlVector
{
public:
string list_classes();
};
私xmlVector.cppが
#include "xmlVector.h"
using namespace std;
void xmlVector::browseFile() // Create vector from parsed data
{
vector_str vect;
ifstream code_File ("Test.txt", ios::in);
while (getline(code_File, line))
{
vect.push_back(line); // add the line to the vector
}
}
void xmlVector::printVector() // Print values in vector_str
{
for (string s : vect)
{
cout << s << endl;
}
}
string classList::list_classes()
{
for (str_it = vector_str.begin(); str_it !=vector_str.end(); str_it++)
{
temp = &str_it;
string element1 = "element";
if(*temp.compare(element1) == 0){
str_it = begin()+1;
temp = &str_it;
string element2 = "class";
if(*temp.compare(element2) == 0){
str_it = begin()+2;
temp = &str_it;
string element3 = "pcdata";
if(*temp.compare(element3) == 0){
str_it = begin()+3;
temp = &str_it;
string element4 = "class";
if(*temp.compare(element4) == 0){
str_it = begin()+4;
temp = &str_it;
string element5 = "element";
if(*temp.compare(element5) == 0){
str_it = begin()+5;
temp = &str_it;
string element6 = "name";
if(*temp.compare(element6) == 0){
str_it = begin()+6;
temp = &str_it;
string element7 = "pcdata";
if(*temp.compare(element7) == 0){
class_name = *temp;
cout << "class name = " << class_name;
}
}
}
}
}
}
}
}
}
私のmain.cppには、私はの名前を保存する必要が
include <iostream> // Std Library
#include "xmlVector.h"
#include <string>
using namespace std;
int main()
{
xmlVector readXmlVector;
readXmlVector.browseFile();
readXmlVector.printVector();
//cout << "Printed Successfully in the file " << endl;
classList classes;
classes.list_classes();
}
であるように私headderファイルがxmlVector.hですすべてのクラスとそのメンバー関数は入力ファイル "Test.txt"にあります。私はベクトルで言及されたパターンを検索することができないので、私は立ち往生しています。
ベクトルを繰り返し処理し、各繰り返しでベクトルに存在する文字列リテラルを比較すると、パターンが一致した後に次の値がどこかに保存される可能性があります。
*はありません:それは、「トークン」、それは特別なシーケンスを一致させることができるかどうかを確認するために1つずつ読み込みます。 [*コンピュータ言語の解析*](https://en.wikipedia.org/wiki/Parsing#Computer_languages)についてさらに調査してください。 –
私が知る必要があるのは、いくつかの文字列値を後続位置のベクトル文字列にある項目と比較する方法です。 'ベクトル :: iteratorはvec.begin()を指し、次に' vec.begin() 'の値をいくつかの文字列' some1 'と比較し、' vec.begin() 'に移動する方法と一致する場合、 +1 'し、別の文字列' 'another' 'に値を与えます。 –