VS2010环境在一个程序中实现语法分析和词法分析,求代码
发布网友
发布时间:2022-05-13 19:40
我来回答
共1个回答
热心网友
时间:2023-10-23 13:33
//一个词法分析程序,输入源程序,输出 二元组(词法记号 , 属性值 / 其在符号表中的位置)构成的序列
#include<iostream>
#include<cstring>
using namespace std;
//char * keywords[6] = {"for","if","then","else","while","do" };
int zimu(char a)//分析是否为字母
{
if ((a>='a')&&(a<='z'))
return 1;
else
return 0;
}
int shuzi(char a)
{
if ((a>='0')&&(a<='9'))
return 1;
else
return 0;
}
int biaodian(char a)
{
if(!zimu(a) && !shuzi(a))
return 1;
else
return 0;
}
void showstring(char * a)
{
int count = 0;
while( a[count]!='\0')
cout<<a[count++];
}
void fenxi()
{
char * sentence;
sentence = new char[50];//储存输入的句子
int i = 0;
char input;
while(cin>>input && input!='#' && i<50)
{
sentence[i] = input;
i++;
}
char ** id;
id = new char*[20];
for(int m = 0;m<20;m++)
{
id[m] = new char[10];
}
for(int x = 0;x<20;x++)
{
for(int y = 0;y<10;y++)
id[x][y] = '\0';
}
char ** num;
num = new char*[20];
for( m = 0;m<20;m++)
{
num[m] = new char[10];
}
for( x = 0;x<20;x++)
{
for(int y = 0;y<10;y++)
num[x][y] = '\0';
}
int * token;
token = new int[30];
int t = 0;//标记token中的词法记号个数
int danci = -1;//记录字母串的单词个数
int shuzichuan = -1;
int dancilong = 0;//记录单词中字母个数
int k = 0;//记录sentence 中的遍历数位
for(k = 0;k<i;)
{
if(zimu(sentence[k]))
{
danci ++;
id[danci][dancilong] = sentence[k];
k++;
while(!biaodian(sentence[k]) && k<i)
{
dancilong++;
id[danci][dancilong] = sentence[k];
k++;
}
dancilong = 0;
if(strcmp("for",id[danci]) ==0)
{
token[t] = 1;
}
if(strcmp("if",id[danci]) ==0)
{
token[t] = 2;
}
if(strcmp("then",id[danci]) ==0)
{
token[t] = 3;
}
if(strcmp("else",id[danci]) ==0)
{
token[t] = 4;
}
if(strcmp("while",id[danci]) ==0)
{
token[t] = 5;
}
if(strcmp("do",id[danci]) ==0)
{
token[t] = 6;
}
else
token[t] = 10;
t++;
}
if(biaodian(sentence[k]))
{
danci ++;
id[danci][dancilong] = sentence[k];
k++;
while(biaodian(sentence[k]) && k<i)
{
dancilong++;
id[danci][dancilong] = sentence[k];
k++;
}
dancilong = 0;
if(strcmp("+",id[danci]) ==0)
{
token[t] = 13;
}
if(strcmp("-",id[danci]) ==0)
{
token[t] = 14;
}
if(strcmp("*",id[danci]) ==0)
{
token[t] = 15;
}
if(strcmp("/",id[danci]) ==0)
{
token[t] = 16;
}
if(strcmp(":",id[danci]) ==0)
{
token[t] = 17;
}
if(strcmp(":=",id[danci]) ==0)
{
token[t] = 18;
}
if(strcmp("<",id[danci]) ==0)
{
token[t] = 20;
}
if(strcmp("<>",id[danci]) ==0)
{
token[t] = 21;
}
if(strcmp("<=",id[danci]) ==0)
{
token[t] = 22;
}
if(strcmp(">",id[danci]) ==0)
{
token[t] = 23;
}
if(strcmp(">=",id[danci]) ==0)
{
token[t] = 24;
}
if(strcmp("=",id[danci]) ==0)
{
token[t] = 25;
}
if(strcmp(";",id[danci]) ==0)
{
token[t] = 26;
}
if(strcmp("(",id[danci]) ==0)
{
token[t] = 27;
}
if(strcmp(")",id[danci]) ==0)
{
token[t] = 28;
}
if(strcmp("#",id[danci]) ==0)
{
token[t] = 0;
}
t++;
}
if(shuzi(sentence[k]))
{
shuzichuan++;
num[shuzichuan][dancilong] = sentence[k];
k++;
while(shuzi(sentence[k]) && k<i)
{
dancilong++;
num[shuzichuan][dancilong] = sentence[k];
k++;
}
dancilong = 0;
token[t++] = 11;
}
}
int n = 0;
x = 0;//id计数
int y = 0;//num计数
while(n < t)
{
if(token[n] == 11)
{
cout<<"("<<token[n]<<",";
showstring(num[y++]);
cout<<")";
//y++;
}
else
{
cout<<"("<<token[n]<<",";
showstring(id[x++]);
cout<<")";
}
n++;
}
}
void main()
{
fenxi();
}