📄 prepare_analyze_words.cpp
字号:
#include <string>
#include "myhead.h"
using namespace std;
word_chain* sample::add_word(word_chain*head) //往单词链中添加单词,不包括注释
{
if (text==NULL)
{
text=new word_chain;
text=head;
}
else
{
word_chain *s=text;
while (s->next!=NULL)
s=s->next;
s->next=head;
}
return head;
}
int sample::point_word(string word) // 判断并返回标志符单词在文中出现的位置,返回0代表该标志符
//在前面没有出现过,否则,返回该标志符在前面出现的位置
{
int point=1;
symbol_chain *s=symbol;
int work=0;
while (s!=NULL)
{
if (s->symbol_word->word!=word)
{
point++;
s=s->next;
}
else
work=1;
if (work)
break;
}
if (work)
return point;
else
return 0;
}
void sample::add_symbol(word_chain*word)//往标志符链中添加元素,返回0代表该标志符在前面没有出现过,
//否则,返回该标志符在前面出现的位置
{
if (symbol==NULL)
{
symbol=new symbol_chain;
symbol->symbol_word=word;
symbol->next=NULL;
}
else
{
symbol_chain *s=symbol;
while (s->next!=NULL)
s=s->next;
s->next=new symbol_chain;
s->next->symbol_word=word;
s->next->next=NULL;
}
}
string sample::separate_word(string words_line,int&point) //分离单词函数,为词法分析函数做准备
{
int length=words_line.length(); //计算字符串的长度
int work=1; //控制循环
while (words_line[point]==32&&point<length)
point++;
string word="";
word+=words_line[point];//取出首个非空格的字母
char ch=words_line[point]; //为了避免每次都用数组下标找值,提高效率
point++; //指针前移
if (point<length)
{
if (ch>=97&&ch<=122||ch>=65&&ch<=90||ch>=48&&ch<=57) //首字母是字母或者数字的处理
{
while (work&&point<length)
{
ch=words_line[point];
if (ch>=97&&ch<=122||ch>=65&&ch<=90||ch>=48&&ch<=57)
{
word+=ch;
point++;
}
else
work=0;
}
}
else
if (ch=='\'') //首字母是'的处理
{
while (work&&point<length)
{
ch=words_line[point];
if (ch!='\'')
{
point++;
word+=ch;
}
else
{
word+=ch;
work=0;
point++;
}
}
}
else
if (ch=='/'&&words_line[point]=='*') //前两个字母是/*的处理
{
while (work&&point<length)
{
ch=words_line[point];
if (ch!='*'||words_line[point+1]!='/')
{
word+=ch;
point++;
}
else
{
word+=ch;
word+=words_line[point+1];
point+=2;
work=0;
}
}
}
else
if (ch==':'&&words_line[point]=='='
||ch=='.'&&words_line[point]=='.'
||ch=='>'&&words_line[point]=='='
||ch=='<'&&words_line[point]=='='
||ch=='<'&&words_line[point]=='>') //双界符处理:= .. >= <= <>的处理
{
word+=words_line[point];
point++;
}
}
return word;
}
int sample::analyze_word(string word) // 分析单个单词,返回一个代表分析结果的数
// 0 代表合法的注释,-1 代表非法字符
// -2 代表字符常数缺右边的单引号,-3 代表注释部分缺右边的界符*/
// 1~60代表合法并且返回其单词编码
{
char ch=word[0]; //计算字符串的长度
int length=word.length();
int point=0;
int work=1;
if (ch>=97&&ch<=122||ch>=65&&ch<=90) //首字母是字母的处理
{
for (int i=0;i<36;i++)
{
if (word==keywords[i])
work=0;
if (!work)
break;
}
if (!work)
return (i+1);
else
return 36;
}
else
if (ch>=48&&ch<=57) //首字母是数字的处理
{
if(ch==48) //数字以0开头
work=0;
else
point++;
while (work&&point<length)
{
ch=word[point];
if(ch>57||ch<48)
work=0;
else
point++;
}
if (!work)
return -1;
else
return 37;
}
else
if (ch=='\'') //首字母是'的处理
{
if(length>1)
{
if (word[length-1]!='\'')
return -2;
else
{
if (length==2)
return -1;
for (int i=1;i<length-1;i++)
{
if (word[i]<40||word[i]==63||word[i]==64
||word[i]==92||word[i]>93&&word[i]<97||word[i]>122)
return -1;
}
return 38;
}
}
else
return -2;
}
else
if (ch=='/'&&length>2&&word[1]=='*') //前两个字母是/*的处理
{
if (word[length-2]!='*'||word[length-1]!='/')
return -3;
else
return 0;
}
else
{
for (int i=38;i<60;i++)
{
if (word==keywords[i])
work=0;
if (!work)
break;
}
if (!work)
return (i+1);
else
return -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -