Principio de compilación código fuente de análisis léxico

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

#include <vector>

// Una estructura de datos en C ++, exactamente una clase. Es equivalente a una matriz dinámica. Cuando el programador no puede saber el tamaño de la matriz que necesita, usarla para resolver el problema puede lograr el máximo ahorro de espacio. Propósito

usando el espacio de nombres std;


vector <string> Char; //
Vector contenedor para almacenar identificadores de tipo char <string> Double; //
Vector contenedor para almacenar identificadores de tipo doble <string> Float; //
Vector contenedor para almacenar identificadores de tipo <string> Int; // El contenedor para almacenar el
vector de identificador de tipo int <string> String; // El contenedor para almacenar el identificador de tipo de cadena




bool pChar = 0; // Para juzgar si una cadena es un identificador de tipo char
bool pDouble = 0 ; // Determine si una cadena es un identificador doble
bool pFloat = 0; // Determine si una cadena es un identificador flotante
bool pInt = 0; // Determine si una cadena es un identificador int
bool pString = 0; // Juzgue si una cadena es un identificador de tipo de cadena


void fhb () // Visualización de la tabla de símbolos
{
string filename2;
int i;
int add = 0; // Dirección
/ * cout << "Ingrese la tabla de símbolos Nombre: ";
cin >> filename2; * /
filename2 =" fb.txt ";
ofstream f2 (filename2, ios :: fuera);
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;
agregar ++;
}
for (i = 0; i <Double.size (); i ++)
{
cout << "double" << '\ t' << Double [i] << '\ t' << agregar << endl;
f2 << "double" << '\ t' << Double [i] << '\ t' << add << endl;
agregar ++;
}
for (i = 0; i <Float.size (); i ++)
{
cout << "float" << '\ t' << Float [i] << '\ t' << add << endl;
f2 << "float" << '\ t' << Float [i] << '\ t' << add << endl;
agregar ++;
}
for (i = 0; i <Int.size (); i ++)
{
cout << "int" << '\ t' << Int [i] << '\ t' << add << endl;
f2 << "int" << '\ t' << Int [i] << '\ t' << add << endl;
agregar ++;
}
para (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')) {



























volver verdadero;
}
else
return false;
}


bool isDigit (char c)
{// 判断 是否 为 数字
if (c> = '0' && c <= '9')
{
return true;
}
else
return 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, "doble") ||
! strcmp (string, "String")) {
return true;
}
de lo contrario
devuelve falso;
}


bool isTODKey (char * string)
{// 判读 是否 为 数据 类型 保留 字
if (! strcmp (string, "int") ||! strcmp (string, "float") ||! strcmp (string, "char" ) ||! strcmp (string, "double") ||! strcmp (string, "String"))
{
return true;
}
else return false;
}


int judgTODKey (char * string)
{// 判断 保留 字 类型
if (! strcmp (string, "char")) devuelve 1;
if (! strcmp (string, "double")) devuelve 2;
if (! strcmp (string, "float")) devuelve 3;
if (! strcmp (string, "int")) devuelve 4;
if (! strcmp (string, "String")) devuelve 5;





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


bool isSeparator (char ch)
{// Determine si es un separador
si (ch == ';' || ch == '{' || ch == '}' || ch == '(' || ch == ')' || ch == ',' || ch == '"')
{
return true;
}
else
return false;
}


// Lee la cadena en el archivo
void outFile (char buff [], string filename1)
{
int i = 0;
ofstream f1 (filename1, ios :: app);
while (buff [i])
{
f1 << buff [i];
i ++;
}
}


// Análisis donde el primer carácter es una letra
if (judgTODKey (buff) == 5) pString = 1; } } más

























{
cout << "(2,";
Show (buff);
cout << ")";
cout << "identificador" << endl;
outFile (buff, filename1);
f1 << '\ t';
f1 < <"2";
f1 << '\ t';
f1 << "Identificador" << endl;
if (pChar)
{// pChar Al abrir, coloque esta cadena en pChar
Char.push_back (buff) ;
}
if (pDouble)
{// pDouble pondrá esta cadena en pDouble
Double.push_back (buff); // inserta un carácter después de la cadena
}
if (pFloat)
{// pFloat cuando Pon esta cadena en pFloat
Float.push_back (buff); // Insertar un carácter después de la cadena
}
if (pInt)
{// pInt se abre, poner esta cadena en pInt
Int.push_back (buff); // Insertar un carácter después de la cadena
}
if (pString)
nombre de archivo1); 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 (nombre de archivo1, ios :: aplicación);
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; // Cuando se encuentra un punto y coma, apague el interruptor y finalice la recepción del identificador
if (ch [j] == ';' && pInt = = 1)
pInt = 0; // Cuando se encuentra el punto y coma, el interruptor se apaga y la recepción del identificador finaliza
si (ch [j] == ';' && pString == 1)
pString = 0; // Se encuentra el punto y coma En ese momento, cierre el interruptor y finalice la recepción del identificador
cout << "(5," << ch [j] << ")" << "Delimitador" << endl;
ofstream f1 (filename1, ios :: app) ;
f1 << ch [j] << '\ t' << "5" << '\ t' << "separador" << endl;
f1.close (); 
}
j ++;
}
fhb ();
}
Publicado 36 artículos originales · 19 alabanzas · 20,000+ visitas

Supongo que te gusta

Origin blog.csdn.net/GJ_007/article/details/79587657
Recomendado
Clasificación