C Program fragment concurrency test and the automatic generation of pre-trend graphs

Source: Internet
Author: User

VX2012 platform. C + + Complete the main program, Java implementation interface.

The so-called program concurrency refers to the existence of multiple programs in the computer system at the same time, macro-view, these programs are moving forward at the same time. In a single-CPU environment, these concurrently executing programs run alternately on the CPU. Analysis of the concurrency between the program, and the use of the Bernstein condition to determine whether the concurrent execution between the programs, and through lexical parsing automatically generated between the program of the pre-graph (dag graph), it is very useful for our analysis program.


by Hfut Yzk
#include "stdafx.h" #include <iostream> #include <string> #include <vector> #include <map># include<cstdio> #include <cstring> #include <cstdlib> #include <fstream>using namespace std;/* struct definition ***********************************************   /#pragma region struct defines struct kind{string na;   word int num;    Inner code string type; type int l,r; Number of rows};struct var_que//Each variable corresponds to two programs read/write queue {vector<int>r, W;}; #pragma endregion</span>/* *********************************************************** variable definition **************** */#pragma region variable defines int line=1,row=1;//row number//char C;const int num_pro= 10;//Number of Programs map<char,int>ma;//operator table vector<kind>res;                                                                                View chart int tree[10000][30]; Dictionary Tree int Numv=0;int w[10000];  Dictionary tree variable value int key=0;    int Flag=0;int flag1=0;    Key statistic keyword, flag statistic identifier char *pro[100];    program file path name Ifstream in;     File Object Map<string,int>getid;  Identifier number Var_que vq[100];                                                         Variable read and write to each program void Test_for_berstein (); Determination of Berstein condition #pragma endregion</span>/* *********************************************************** function declaration * /#pragma the region function declares void start (int id);//start function void Testit (int id,char c,string s);//identifier whether Lvalue test 1void testitf (int id,string s);//identifier whether Lvalue Test 2void init ();//initialization function void Insert ( string s);//dictionary tree Insert Function void reads ();//read into data bool Test (int u);//whether to keep word prefix void F1 (char &c,int ID);//keyword identifier parser void F2 (char &  AMP;C); Digital analyzer void F3 (char &c); Delimiter, operator, and other parser bool Test_other (char x);                                         Tests whether the operator is void Test_for_same_w (string s,int ID); Prevents variables of the same program from recording void Get_gra multiple timesph (); Generating a pre-trend graph #pragma endregion</span>/************************************************************ function implementation area ******** /#pragma region function implementation/***************************************                                                                                         initialization function ************************************************************/void init ()    {//res.clear (); Flag=0;return;} /******************************************************* Dictionary tree data insertion function **********************************************    /void Insert (string s) {int u=0;        for (int i=0;i<s.size (); i++) {if (tree[u][s[i]-' a ']==0) tree[u][s[i]-' a ']=++numv;    U=tree[u][s[i]-' a ']; } W[u]=key++;return;} /*********************************************************** read-in data function ********************************************* /void reads () {ma[' [']=1;ma['] ']=2;ma[', ']=3;ma['; =4;ma[' (']=5;ma[') ']=6;ma[' {']=7;ma['} ']=8; ma[' + ']=9;ma['-']=10;ma[' * ']=11;ma['/']=12;ma['% ']=13;ma[' & ']=14;ma[' | =15;ma['. '   = 16;   string S;   Ifstream Inkey ("H:/course/os/os_program/key.txt");       Reserved words read in while (s!= "end") {inkey>>s;   Insert (s);    }//program read into pro[1]= "H:/course/os/os_program/program1.txt";    Pro[2]= "H:/course/os/os_program/program2.txt"; pro[3]= "H:/course/os/os_program/program3.txt";p ro[4]= "H:/course/os/os_program/program4.txt";p ro[5]= "H:/course/ Os/os_program/program5.txt ";p ro[6]=" H:/course/os/os_program/program6.txt ";p ro[7]=" h:/course/os/os_program/ Program7.txt ";p ro[8]=" H:/course/os/os_program/program8.txt ";p ro[9]=" H:/course/os/os_program/program9.txt ";p ro[    10]= "H:/course/os/os_program/program10.txt";         for (int i=1;i<=num_pro;i++) {init ();         In.open (Pro[i]);         Start (i);     In.close (); }return;} /******************************************************* whether the word prefix tester is reserved ********************************************* /bool Test (int u) {for (int i=0;i<26;i++) {if (tree[u][i]!=0) return 1; } return 0;} /****************************************************** keyword Identifier parser ***********************************************    /void F1 (char &c,int ID) {int mark=1;    string S;    S+=c;    int u=0;    if (tree[u][c-' a ']!=0)//Whether the keyword u=tree[u][c-' a '];    else mark=0; while (C=in.get ())//keyword Test {if (! ( C>= ' a ' &&c<= ' Z ')) if (c!= ' _ ') if (! (        c>= ' 0 ' &&c<= ' 9 ')) break;              if (mark==1) {if (tree[u][c-' a ']!=0) u=tree[u][c-' a '];        else mark=0;    } s+=c;    } if (Test (U))//appears with reserved word prefix condition as identifier {mark=0;}                                                 Kind T;    Input t.na=s;    T.l=line;    T.r=row;      if (mark==0) {t.num=++flag;  if (Getid.find (s) ==getid.end ())//prevent duplication {GETID[S]=++FL         AG1;    } int dex= (Vq[getid[s]].r.size ())-1;        Prevent the same program variable multiple records if (DEX&GT;=0&AMP;&AMP;VQ[GETID[S]].R[DEX]==ID);        else Vq[getid[s]].r.push_back (ID);      T.type= "identifier";                                                                                 Testit (id,c,s);        Lvalue Test 1 TESTITF (id,s);//Left Value Test 2} else {t.num=w[u];    t.type= "keywords";    } res.push_back (t); return;} /*************************************************************** Digital Analyzer ******************************************    /void F2 (char &c) {string S;    S+=c;    C=in.get ();        while (c>= ' 0 ' &&c<= ' 9 ') {s+=c;    C=in.get ();    } int mark=1;    while (c>= ' a ' &&c<= ' Z ') {s+=c;mark=0;c=in.get ();}    Kind T;    T.na=s;    T.l=line;    T.r=row; if (mark==1) {//chaR *p;    Strcpy_ (P,s.c_str ());    T.num=0;//t.num=atoi (P);    t.type= "Number";        } else {t.type= "error";    t.num=0;    } res.push_back (t);                                 return;} /********************************************** delimiters, operators, and other analyzers **************************************************    /void f3 (char &c) {string S;    Kind T;    T.l=line;t.r=row;        if (Ma.find (c)!=ma.end ()) {t.na+=c;        if (ma[c]<=8) t.type= "delimiter";        else t.type= "operator";        T.NUM=MA[C];    C=in.get ();        } else {t.type= "relational operator";             if (c== ' > ') {c=in.get ();                 if (c== ' = ') {t.na= ">=";                 T.num=1;             C=in.get ();                } else {t.na= ">";             t.num=2;              }} else if (c== ' < ') {c=in.get ();  if (c== ' = ')            {t.na= "<=";                 t.num=3;              C=in.get ();                  } else if (c== ' > ') {t.na= "<>";                  t.num=4;              C=in.get ();                  } else {t.na= "<";              t.num=5;            }} else if (c== ' = ') {t.na= "=";            t.num=6;        C=in.get ();    }} res.push_back (t); return;} /********************************************** test whether the operator ********************************************************* /bool Test_other (char x) {if (x== ' + ' | | x== '-' | | x== ' * ' | | x== '/' | | x== '% ' | | x== ' & ' | | x== ' ^ ' | |      x== ' | ')    return 1; else return 0;} /********************************************* the variable record of the same program *******************************************************    /void Test_for_same_w (string s,int ID) {int dex= (vq[getid[s]].w.size ())-1;       Prevents multiple records of the same program's variables           if (dex>=0&&vq[getid[s]].w[dex]==id); else Vq[getid[s]].w.push_back (ID);} /********************************************* Left value Tester 1***********************************************************                                                                                          /void TESTITF (int id,string s)    Test ++a/--a{int SS=IN.TELLG ();//Position the current file pointer position int go=s.size () +2;//to the previous position of the variable in.seekg (-go,ios::cur); for (;;        go++) {char c=in.get ();        if (c== ") {in.seekg ( -2,ios::cur); continue;}  else if (c== ' + ') {in.seekg ( -2,ios::cur);  C=in.get ();  if (c== ' + ') {Test_for_same_w (s, id);  }}else if (c== '-') {IN.SEEKG ( -2,ios::cur);  C=in.get ();  if (c== '-') {Test_for_same_w (s, id);    }} break;    } go=ss-in.tellg (); IN.SEEKG (go,ios::cur);} /********************************************* Left value Tester 2*****************************************/void testit (int id,char c,string s) {int go=0;    int Ss=in.tellg (); for (;;        go++) {if (c== ')//Ignore space {c=in.get (); continue;}        else if (c== ' = ')//exclude = = disturbance {if (In.peek () = ' = ') Test_for_same_w (s, id);        } else if (Test_other (c)) {if (In.peek () = = ' = ') Test_for_same_w (s, id);        } if (c== ' + ') {if (In.peek () = = ' + ') Test_for_same_w (s, id);        } else if (c== '-') {if (In.peek () = = '-') Test_for_same_w (s, id);    } break;    } go=ss-in.tellg (); IN.SEEKG (go,ios::cur);} /**************************************************** General Control Program ******************************************************    /void start (int id)//start {char C;     C=in.get (); while (1) {if (c== ' @ ') break;/*cout<<in.tellg ();cout<< "C:" <<c<< ": C"<<endl;*/if (int (IN.TELLG ()) ==-1) break; while (c== ' | |           c== ' \ t ') {c=in.get (); row++;}              if (c== ' \ n ') {line++;              Row=1;              C=in.get ();          Continue        } if (c>= ' a ' &&c<= ' Z ') F1 (C,id);        else if (c>= ' 0 ' &&c<= ' 9 ') F2 (c);        Else F3 (c);        row++;              if (c== ' \ n ') {line++;              Row=1;          C=in.get (); }} return; /******************************************************** generating a pre-trend map ************************************************* /void get_graph () {ofstream out (' h:/course/os/os_program/graph.txt ');out<< num_pro<<endl; for    (int i=1;i<=flag1;i++)                                              {for (int w=0;w<vq[i].w.size (), w++) {for (int r=0;r<vq[i].r.size (); r++)                                            {if (Vq[i].w[w]<vq[i].r[r])                             Write after read out<<vq[i].w[w]<< "<<vq[i].r[r]<<" "<<" 1 "<<endl;else if (Vq[i].w[w]>vq[i].r[r])//Read after write out<<vq[i].r[r]<<" "<<vq[i].w[w ]<< "" << "2" &LT;&LT;ENDL;} for (int ww=w+1;ww<vq[i].w.size (); ww++)//write after {out<<vq[i].w[w]<< "" <<vq[i].w[ww]<< "<               < "3" &LT;&LT;ENDL;} }} out<<-1<<endl; return;} The concurrency ***************************************** of/******************************************************** Bernstein conditional judgment program /void Test_for_berstein () {cout<< "according to the conditions of Berstein, the following program s can not being executed concurrently:\n "; for (int i=1;i<=flag1;i++) {A (int w=0;w<vq[i].w.size (); w++) {for (int r=0;r&l                                       T;vq[i].r.size (); r++) {if (Vq[i].w[w]<vq[i].r[r])                                  Write after read cout<< "program" <<vq[i].w[w]<< "<&" lt; " Program "<<vq[i].r[r]<<" << "for the Conflict of raw.\n" <<endl;else if (vq[i].w[w]>vq[i].r[ R])//Read and write cout<< "program" <<vq[i].r[r]<< "" << "program" <<vq[i].w[w]<< "" << "For the Conflict of war.\n" &LT;&LT;ENDL;} for (int ww=w+1;ww<vq[i].w.size (); ww++)//write-write {cout<< "program" <<vq[i].w[w]<< "<<"               Program "<<vq[i].w[ww]<<" << "for the Conflict of waw.\n" &LT;&LT;ENDL;} }} return; #pragma endregion</span>/************************************************** main function *************************** /int _tmain () {reads (); Test_for_berstein (); Get_graph ();//cout    << "graph get successfully!\n";   Ofstream out ("H:/course/os/os_program/res.txt"); out<< "word" << ' \ t ' << ' \ t ' << ' two-dollar sequence '<< ' \ t ' << ' \ t ' << ' type ' << ' \ t ' << ' \ t ' << ' position (row, column) "<<endl; for (int i=0;i<res.size (); i++) {/* out<<res[i].na<< ' \ t ' << ' \ t ' << ' ("<<res[i].num<<", "<<res[i].na<<") "<< ' \ t ' << ' \ t ' <<res[i].type<< ' \ t ' << ' \ t ' << ("<<res[i].l<<", "<<res[i].r<<") "<<endl;*/out<      <res[i].na<< ' \ t ' << ' \ t ' <<res[i].type<<endl; } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C Program fragment concurrency test and the automatic generation of pre-trend graphs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.