コンパイルの原則の字句解析のソースコード

 
 
#include <iostream>
#include <string>
#include <fstream>

#include <vector>

// C ++のデータ構造、まさにクラス。これは動的配列と同等です。プログラマーが必要な配列のサイズを認識できない場合、それを使用して問題を解決することで、最大のスペース節約を実現できます。目的。

using namespace std;


vector <string> Char; // char型識別子
格納するためのコンテナ ベクトル<string> Double; // double型識別子
格納するためのコンテナ ベクトル<string> Float; //型識別子
格納するためのコンテナ ベクトル<string> Int; // int型識別子を格納するためのコンテナ
<string> String; //文字列型識別子を格納するためのコンテナ




bool pChar = 0; //文字列がchar型識別子であるかどうかを判断する
bool pDouble = 0 ; //文字列が二重識別子であるかどうかを判別
bool pFloat = 0; //文字列が浮動小数点識別子であるかどうかを判別
bool pInt = 0; //文字列がint識別子であるかどうかを判別
bool pString = 0; //文字列が文字列型識別子かどうかを判断します


void fhb()//シンボルテーブルの表示
{
string filename2;
int i;
int add = 0; //アドレス
/ * cout << "シンボルテーブルを入力してください名前: ";
cin >> filename2; * /
filename2 =" fb.txt ";
ofstream f2(filename2、 ios :: out);
f2 << "类型" << '\ t' << "变量" << '\ t' << "地址" << endl;


cout << endl;
cout << "类型" << '\ t' << "变量" << '\ t' << "地址" << endl;
for(i = 0; i <Char.size(); i ++)
{
cout << "char" << '\ t' << Char [i] << '\ t' << add << endl;
f2 << "char" << '\ t' << Char [i] << '\ t' << add << endl;
add ++;
}
for(i = 0; i <Double.size(); i ++)
{
cout << "double" << '\ t' << Double [i] << '\ t' << << endl;を追加
f2 << "double" << '\ t' << Double [i] << '\ t' << add << endl;
add ++;
}
for(i = 0; i <Float.size(); i ++)
{
cout << "float" << '\ t' << Float [i] << '\ t' << add << endl;
f2 << "float" << '\ t' << Float [i] << '\ t' << add << endl;
add ++;
}
for(i = 0; i <Int.size(); i ++)
{
cout << "int" << '\ t' << Int [i] << '\ t' << add << endl;
f2 << "int" << '\ t' << Int [i] << '\ t' << add << endl;
add ++;
}
for(i = 0; i <String.size(); i ++)
{
cout << "string" << '\ t' << String [i] << '\ t' << add << endl;
f2 << "string" << '\ t' << String [i] << '\ t' << add << endl;















= 'z')|| (c> = 'A' && c <= 'Z')) {



























trueを返します。
}
それ以外の
リターンはfalse;
}


bool isDigit(char c)
{//判断是否が数字である
場合(c> = '0' && c <= '9')
{
return true;
}
それ以外の
リターンはfalse;
}


bool isKey(char * string)
{//判断是否は保留字
if(!strcmp (string、 "void")||!strcmp(string、 "if")||!strcmp(string、 "for")| |!strcmp(string、 "while")||!strcmp(string、 "do")
||!strcmp(string、 "return")||!strcmp(string、 "break")||!strcmp(string、 "main")||!strcmp(string、 "int")
||!strcmp(string、 "float")||!strcmp(string、 "char")||!strcmp(string、 「ダブル」)||
!strcmp(string、 "String")) {
trueを返す;
}
それ以外の場合は
falseを返します。
}


bool isTODKey(char * string)
{//判读是否是データ类型保留字
if(!strcmp (string、 "int")||!strcmp(string、 "float")||!strcmp(string、 "char" )||!strcmp(string、 "double")||!strcmp(string、 "String"))
{
return true;
}
それ以外のリターンはfalse;
}


int judgTODKey(char * string)
{//判断保留字类型
if(!strcmp(string、 "char"))return 1;
if(!strcmp(string、 "double"))は2を返します。
if(!strcmp(string、 "float"))は3を返します。
if(!strcmp(string、 "int"))は4を返します。
if(!strcmp(string、 "String"))は5を返します。





if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=')
{
return true;
}
else
return false;
}


bool isSeparator(char ch)
{//
if(ch == ';' || ch == '{' || ch == '}' || ch == '(' || ch == ')' || ch == '、' || ch == '"')
{
return true;
}
else
return false;
}


//ファイル内の文字列を読み取る
void outFile(char buff []、string filename1)
{
int i = 0;
ofstream f1(filename1、ios :: app);
while(buff [i])
{
f1 << buff [i];
i ++;
}
}


//最初の文字が文字である分析
if(judgTODKey(buff)== 5)pString = 1; } } その他

























{
cout << "(2、";
表示(buff);
cout << ")";
cout << "identifier" << endl;
outFile(buff、filename1);
f1 << '\ t';
f1 < <"2";
f1 << '\ t';
f1 << "Identifier" << endl;
if(pChar)
{// pChar開くときに、この文字列をpChar
Char.push_back(buff)に入れ ます;
}
if(pDouble)
{// pDoubleはこの文字列をpDouble
Double.push_back(buff);に挿入します//文字列の後に文字を挿入します
}
if(pFloat)
{// pFloat whenこの文字列をpFloat
Float.push_back(buff);に入れ ます。 //文字列の後に文字を挿入
}
if(pInt)
{// pIntが開いたら、この文字列をpIntに
挿入しますInt.push_back(buff); //文字列の後に文字を挿入します
}
if(pString)
filename1); f1 << '\ t';


































sizeof(buff)); //清空buff int i = 0; while(!isspace(ch [j])&&!isOperator(ch [j])&&!isSeparator(ch [j])) {





























buff [i] = ch [j];
j ++;
i ++;
}
//分析buff []数组
if(isLetter(buff [0]))
{
Aletter(buff、filename1);
}
if(isDigit(buff [0]))
{
Adigit(buff、filename1);
}
if(isOperator(ch [j]))
{
cout << "(4、" << ch [j] << ")" << "运算符" << endl;
ofstream f1(filename1、ios :: app);
f1 << ch [j] << '\ t' << "4" << '\ t' << "运算符" << endl;
f1.close();
}
if(isSeparator(ch [j]))
{
if(ch [j] == ';' && pChar == 1)
pChar = 0;


if(ch [j] == ';' && pFloat == 1)
pFloat = 0; //セミコロンが検出され
たら、スイッチをオフにして識別子の受信を終了します if(ch [j] == ';' && pInt = = 1)
pInt = 0; //セミコロンが検出されると、スイッチがオフになり、識別子の受信は終了します
if(ch [j] == ';' && pString == 1)
pString = 0; //セミコロンが検出されますその時点で、スイッチを閉じ、識別子
cout << "(5、" << ch [j] << ")" << "Delimiter" << endl;
ofstream f1(filename1、ios :: app)の受信を終了し ます。 ;
f1 << ch [j] << '\ t' << "5" << '\ t' << "separator" << endl;
f1.close(); 
}
j ++;
}
fhb();
}
36件の元の記事を公開 19件の賞賛 20,000回以上の閲覧

おすすめ

転載: blog.csdn.net/GJ_007/article/details/79587657