?? wordanalyse.c
字號:
#include "global.h"
#include "keyword.h"
int main(){
char buffer[20];/*將每次讀取的字符串存到一個緩沖區(qū)*/
int length=0;/*記錄讀取字符串的長度*/
struct record temp;/*定義詞法分析器輸出的中間結(jié)果的格式*/
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){/*創(chuàng)建中間文件,保存詞法分析器分析出的二元式文件*/
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);//輸出詞法分析器的錯誤,并指出錯誤發(fā)生的行號
}
else{
temp.kind=look(buffer);//查關(guān)鍵字表,返回關(guān)鍵字的編碼
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);//輸出詞法分析器的錯誤,并指出錯誤發(fā)生的行號
}
fclose(fpread);
fclose(fpwrite);
return 1;
}
void flashBuffer(char buffer[],struct record *temp)//清空函數(shù)
{
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')){ //常數(shù)
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'))){ //標(biāo)識符
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);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -