📄 lexical_analyzer.h
字号:
#ifndef LEXICAL_H
#define LEXICAL_H
#include "global.h"
#include "error.h"
#include "symbol.h"
long tokenval=NONE;
int lineno=1; //line num
int position=1; //position
char lexbuf[BSIZE]; //buf ,
to_parser &lexan(FILE *file);
char * get_reserved(int p); //get reserved_word string
to_parser &lexan(FILE *file)
{
int t; //used to receive char
struct to_parser q;
while(1)
{
t=getc(file); //读一个字符,文件读
//t=getchar();std
if(t==' ' || t=='\t') //white space or tab
;
else if(t=='\n') //return
{lineno++;position=1;}
////////////////////////////////////////////////////////////////////
else if(isdigit(t)) //digit
{
ungetc(t,file); //倒车
//ungetc(t,stdin); std
fscanf(file, "%ld",&tokenval); //读文件
//for debug
q.token=NUM;
q.line_no=lineno;
q.position=position;
q.attr.long_attr=tokenval;
position++;
return q;
}
//////////////////////////////////////////////////////////////////////
else if(isalpha(t)) //id or keywords
{
int p; //used as return value
int b=0; //the position of buf
while(isalnum(t))
{
lexbuf[b]=t;
t=getc(file);
b++;
if(b>=BSIZE)
error("compiler error");
}
lexbuf[b]=EOS;
if(t!=EOF)
ungetc(t,file);
p=lookup(lexbuf);
if(p==0)
{
p=insert(lexbuf,ID);
q.token=ID;
}
else if(p<7)
{
q.token=p;
}
else
{
q.token=ID;
}
tokenval=p;
q.line_no=lineno;
q.position=position;
q.attr.str_attr=lexbuf;
position++;
return q;
}
////////////////////////////////////////////////////////////////////////////
else if(t==EOF) //end of file
{
q.token=DONE;
return q;
}
////////////////////////////////////////////////////////////////////////////
else if(t=='+' || t=='-' || t=='*' ) // operator
{
q.token=OPERATOR;
q.line_no=lineno;
q.position=position;
sprintf(lexbuf,"%c",t);
q.attr.str_attr=lexbuf;
position++;
return q;
}
else if(t=='/')
{
char next_t;
next_t=getc(file);
if(next_t=='/') //不考虑 注释内容,
{ fgets(lexbuf,1000,file); position=1; lineno++;}
else
{
ungetc(next_t,file);
q.token=OPERATOR;
q.line_no=lineno;
q.position=position;
sprintf(lexbuf,"%c",t);
q.attr.str_attr=lexbuf;
position++;
}
}
////////////////////////////////////////////////////////////////////////////
else if(t=='=' ||t=='>' || t=='<' ||t=='!') //operator
{
char next_t;
next_t=getc(file);
if(next_t!='=')
{
ungetc(next_t,file);
sprintf(lexbuf,"%c",t);
q.attr.str_attr=lexbuf;
}
else
{
sprintf(lexbuf,"%c%c",t,next_t);
q.attr.str_attr=lexbuf;
}
q.token=OPERATOR;
q.line_no=lineno;
q.position=position;
position++;
return q;
}
//////////////////////////////////////////////////////////////////////////////////
else if(t=='(' || t==')' || t==';' ||t=='{' || t=='}')
{
q.token=DELIMITER;
q.line_no=lineno;
q.position=position;
q.attr.char_attr=t;
position++;
return q;
}
//////////////////////////////////////////////////////////////////////////////
else //other invalid character
{
tokenval=NONE;
char next_t;
next_t=getc(file);
if(next_t==' ' ||next_t=='/t')
position++;
else
ungetc(next_t,file);
}
}
}
char * get_reserved(int p)
{
switch(p)
{
case 1: return "IF";
case 2: return "THEN";
case 3: return "WHILE";
case 4: return "REAL";
case 5: return "INT";
case 6: return "ELSE";
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -