?? lex.cpp
字號:
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include<iostream.h>
#include "global.h"
#include "keyword.h"
//*********************************************************************************************//
// 詞法分析部分
//*********************************************************************************************//
void getch();//把下一個輸入字符讀到ch中,把讀入源程序字符的向前指針前移一個字節位置
void getbc();//檢查ch中的字符是否為空白、tab或換行。若是,則調用getch()直至ch進入一個非空白也非
//標記字符為止。
void concatenation();//把ch中的字符連接到token中字符串的后面
void retract();//把讀入源程序字符的向前指針回調一個字節位置,并把ch中的字符置為空
int reserve();//對token中的字符串查找保留字表,若是則送回它的編碼,否則送回非保留字信息
void error(int,int);
int nexttoken(){//取下一個單詞,詞法分析部分
int xh;
for(xh=0;xh<SIZE;xh++)token[xh]=0;//初始化
getch();getbc();//得到有效字符
if(isalpha(ch)){
while(isalpha(ch)||isdigit(ch)){
concatenation();getch();
}
retract();
xh=reserve();
if(xh==0){
return(id);
}
return(xh);
}
if(isdigit(ch)){
while(isdigit(ch)){
concatenation();getch();
}
retract();
return(number);
}
switch(ch){
case EOF:return EOF;
case '(':return(lpar);
case ')':return(rpar);
case '+':return(plus_op);
case '-':return(minus_op);
case '=':return(relop_EQ);
case '<':getch();
if(ch=='='){return(relop_LE);}
else if(ch=='>'){return(relop_NE);}
retract();return(relop_LT);
case '>':getch();
if(ch=='='){return(relop_GE);}
retract();return(relop_GT);
case ':':getch();
if(ch=='='){return(assign_op);}
retract();return(colon);
case ';':return(semicolon);
case '*':return(multi_op);
case '|':return(or_op);
case ',':return(comma);
case '[':return(lbracket);
case ']':return(rbracket);
case '.':getch();
if(ch=='.')return dotdot;
retract();return dot;
default:
error(19,LINE);
return nexttoken();
// return -1;
}//and ".." need to be added!
}
void getch(){
ch=fgetc(testfile);
if(ch=='\n')LINE++;
if(ch=='{'){
while(ch!='}')
{
getch();
if(ch==EOF)
{
error(33,LINE);
cout<<progname<<".obj- "<<errorcount<<" error(s)"<<endl;
exit(0);
}
}
getch();
}
}
void getbc(){
while(ch==' '||ch=='\n'||ch==9)getch();
// if(ch==EOF)cout<<"over..."<<endl;
}
void retract(){
if(ch=='\n')LINE--;
fseek(testfile,-1L,1);
ch=' ';
}
void concatenation()
{
char tmp=tolower(ch);
strncat(token,&tmp,1);
}
int reserve(){
int i;
for(i=0;token[i]!=0;i++);
if(!strcmp(token,"true"))return True;
if(!strcmp(token,"false"))return False;
if(!strcmp(token,"program"))return Program;
if(!strcmp(token,"procedure"))return Procedure;
if(!strcmp(token,"const"))return Const;
if(!strcmp(token,"type")) return Type;
if(!strcmp(token,"array"))return Array;
if(!strcmp(token,"of")) return Of;
if(!strcmp(token,"record"))return Record;
if(!strcmp(token,"begin")) return Begin;
if(!strcmp(token,"end")) return End;
if(!strcmp(token,"var")) return Var;
if(!strcmp(token,"procedure"))return Procedure;
if(!strcmp(token,"if")) return If;
if(!strcmp(token,"then")) return Then;
if(!strcmp(token,"else")) return Else;
if(!strcmp(token,"while"))return While;
if(!strcmp(token,"do")) return Do;
// if(!strcmp(token,"empty"))return Empty;
if(!strcmp(token,"or")) return Or;
if(!strcmp(token,"and"))return And;
if(!strcmp(token,"div"))return Div;
if(!strcmp(token,"mod"))return Mod;
if(!strcmp(token,"not"))return Not;
if(!strcmp(token,"boolean"))return Boolean;
if(!strcmp(token,"integer"))return Integer;
if(!strcmp(token,"read")) return Read;
if(!strcmp(token,"write")) return Write;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -