📄 wordanalyse.c
字号:
#include "global.h"
#include "keyword.h"
int main(){
char buffer[20];/*将每次读取的字符串存到一个缓冲区*/
int length=0;/*记录读取字符串的长度*/
struct record temp;/*定义词法分析器输出的中间结果的格式*/
FILE *fpread;/*读源程序文件的指针*/
FILE *fpwrite;/*写源程序文件的指针*/
if((fpread=fopen("in.pas","rb"))==NULL){/*打开已知源文件,用mini-Pascal编写的原文件*/
printf("\nERROR:源程序不存在.\n");
exit(0);
}
if((fpwrite=fopen("out.pbj","wb"))==NULL){/*创建中间文件,保存词法分析器分析出的二元式文件*/
printf("\nERROR.\n");
exit(0);
}
flashBuffer(buffer,&temp);/*清空缓存*/
if(!feof(fpread)){
length=scan(buffer,fpread);/*读取字符串*/
}
while(!feof(fpread)){
if(length==0){
printf("\nERROR: Line %d 非法字符\n",line);//输出词法分析器的错误,并指出错误发生的行号
}
else{
temp.kind=look(buffer);//查关键字表,返回关键字的编码
if(temp.kind==35){//是换行符则行号加一
strcpy(temp.strName,"newline");
}else{
strcpy(temp.strName,buffer);
}
if(temp.kind==35)line++;
printf("{ %s",temp.strName);
printf(" %d }",temp.kind);
printf("\n");
outWord(temp,fpwrite);
flashBuffer(buffer,&temp);
}
length=scan(buffer,fpread);
}
if(length){
temp.kind=look(buffer);
if(temp.kind==35){
strcpy(temp.strName,"newline");
}else{
strcpy(temp.strName,buffer);
}
if(temp.kind==35)line++;
printf("{ %s",temp.strName);
printf(" %d }",temp.kind);
printf("\n");
outWord(temp,fpwrite);
flashBuffer(buffer,&temp);
}
else{
printf("\nERROR: Line %d 非法字符\n",line);//输出词法分析器的错误,并指出错误发生的行号
}
fclose(fpread);
fclose(fpwrite);
return 1;
}
void flashBuffer(char buffer[],struct record *temp)//清空函数
{
int i=0;
for(i=0;i<20;i++){
buffer[i]='\0';
temp->strName[i]='\0';
}
}
int scan(char buffer[],FILE *fpread){//读字符模块
char word;
int ptr=0;
word=getc(fpread);
while((word==' ')||(word=='\t')||(word=='\r')){
word=getc(fpread); //保留字
}
if((word>='0')&&(word<='9')){ //常数
while((word>='0')&&(word<='9')){
buffer[ptr]=word;
if(ptr<20){
ptr++;
}
if(feof(fpread))
break;
else
word=getc(fpread);
}
}
else if(((word>='a')&&(word<='z'))||((word>='A')&&(word<='Z'))){ //标识符
while(((word>='A')&&(word<='Z'))||((word>='a')&&(word<='z'))||((word>='0')&&(word<='9'))){
buffer[ptr]=word;
if(ptr<20){
ptr++;
}
if(feof(fpread))
break;
else
word=getc(fpread);
}
}
else if(word=='+'){
buffer[ptr]=word;
ptr++;
}
else if(word=='-'){
buffer[ptr]=word;
ptr++;
}
else if(word=='*'){
buffer[ptr]=word;
ptr++;
}
else if(word=='/'){
buffer[ptr]=word;
ptr++;
}
else if(word==':'){
buffer[ptr]=word;
ptr++;
if(!feof(fpread))
{
word=getc(fpread);
if(word=='='){
buffer[ptr]=word;
ptr++;
}
else {
ptr=0;
fseek(fpread,-1L,SEEK_CUR);/*回退一个字符*/
}
}
}
else if(word=='='){
buffer[ptr]=word;
ptr++;
}
else if(word=='<'){
buffer[ptr]=word;
ptr++;
if(!feof(fpread))
{
word=getc(fpread);
if(word=='>'){
buffer[ptr]=word;
ptr++;
}
else {
ptr=0;
fseek(fpread,-1L,SEEK_CUR);/*回退一个字符*/
}
}
}
else if(word=='>'){
buffer[ptr]=word;
ptr++;
if(!feof(fpread))
{
word=getc(fpread);
if(word=='='){
buffer[ptr]=word;
ptr++;
}
else {
ptr=0;
fseek(fpread,-1L,SEEK_CUR);/*回退一个字符*/
}
}
}
else if(word=='<'){
buffer[ptr]=word;
ptr++;
if(!feof(fpread))
{
word=getc(fpread);
if(word=='='){
buffer[ptr]=word;
ptr++;
}
else {
ptr=0;
fseek(fpread,-1L,SEEK_CUR);/*回退一个字符*/
}
}
}
else if(word=='('){
buffer[ptr]=word;
ptr++;
}
else if(word==')'){
buffer[ptr]=word;
ptr++;
}
else if(word==','){
buffer[ptr]=word;
ptr++;
}
else if(word==';'){
buffer[ptr]=word;
ptr++;
}
else if(word=='.'){
buffer[ptr]=word;
ptr++;
}
else if(word=='\n'){
buffer[ptr]=word;
ptr++;
}
else
{
ptr=0;
}
return ptr;
}
int look(char word[]){//查保留字表模块
int i;
int location=0;
for(i=0;i<KeyWordLength;i++){
if(strcmp(word,keyWord[i])==0){
location=i;
return location;
}
}
if((word[0]>='0')&&(word[0]<='9')){
location=18;
}
else if(((word[0]>='a')&&(word[0]<='z'))||((word[0]>='A')&&(word[0]<='Z'))){
location=17;
}
return location;
}
void outWord(struct record out,FILE *fpwrite){//输出单词模块
fwrite(&out,sizeof(struct record),1,fpwrite);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -