四川大学Cminus语法分析器纯代码Word格式文档下载.docx
《四川大学Cminus语法分析器纯代码Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《四川大学Cminus语法分析器纯代码Word格式文档下载.docx(43页珍藏版)》请在冰点文库上搜索。
![四川大学Cminus语法分析器纯代码Word格式文档下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/4/28e59ae4-2b5e-40c2-98c8-6a957fa82585/28e59ae4-2b5e-40c2-98c8-6a957fa825851.gif)
char*
str;
TokenType
tok;
}ReserverWords[6]
{
{"
if"
IF},{"
else"
ELSE},{"
int"
INT},{"
return"
RETURN},{"
void"
VOID},{"
while"
WHILE}
void
UnGetNextChar()
if
(!
EOF_FLAG)
linepos--;
}
GetNextChar()
if(!
(linepos<
bufsize))
{
lineno++;
if(fgets(lineBuf,BUFLEN-1,source))
{
bufsize=strlen(lineBuf);
linepos=0;
return
lineBuf[linepos++];
}
else
EOF_FLAG=true;
EOF;
}
else
return
TokenType
ReservedLookUp(char
s)
int
i;
for
(i
i
<
6;
i++)
if(!
strcmp(s,ReserverWords[i].str))
ReserverWords[i].tok;
return
ID;
GetToken()
StateType
state
START;
//初始状态为start
bool
save;
CurrentToken;
tokenStringIndex=0;
string
assign="
"
;
while(state!
=DONE)
int
c=GetNextChar();
save
true;
switch
(state)
case
START:
if
(isdigit(c))
{
state
INNUM;
}
else
if
(isalpha(c))
INID;
((c
==
'
)||(c=='
>
='
!
))
{
INASSIGN;
assign+=char(c);
)
||
(c
\t'
\n'
save
/'
)
PRECOMMENT;
else
DONE;
switch
(c)
{
case
EOF:
save
CurrentToken
ENDFILE;
break;
+'
:
PLUS;
-'
MINUS;
*'
TIMES;
('
LPAREN;
)'
RPAREN;
SEMI;
['
LBRACKET;
]'
RBRACKET;
{'
LBRACE;
}'
RBRACE;
'
COMMA;
default:
ERROR;
}
break;
INCOMMENT:
save
EOF)
CurrentToken
AFTERCOMMENT;
state=INCOMMENT;
break;
INASSIGN:
EQ;
state=DONE;
if(assign=="
UnGetNextChar();
="
=ASSIGN
=LT
=GT
INNUM:
isdigit(c))
NUM;
INID:
isalpha(c))
PRECOMMENT:
if(c=='
save=false;
CurrentToken=OVER;
AFTERCOMMENT:
save=false;
state=START;
if(c=='
state=AFTERCOMMENT;
DONE:
default:
state
CurrentToken
if((save)&
&
(tokenStringIndex<
=MAXTOKENLEN))
tokenString[tokenStringIndex++]=(char)c;
if(state==DONE)
tokenString[tokenStringIndex]='
\0'
if(CurrentToken==ID)
CurrentToken=ReservedLookUp(tokenString);
NodeKind//节点类型
FuncK,IntK,IdK,ParamsK,ParamK,CompK,ConstK,CallK,ArgsK,VoidK,Var_DeclK,Arry_DeclK,Arry_ElemK,AssignK/*,WhileK*/,OpK,
Selection_StmtK,Iteration_StmtK,Return_StmtK
struct//节点类型和字符串关系
NodeKind
nk;
}nodekind[18]
Funck"
FuncK},{"
IntK"
IntK},{"
IdK"
IdK},{"
ParamsK"
ParamsK},{"
ParamK"
ParamK},{"
CompK"
CompK},{"
ConstK"
ConstK},{"
CallK"
CallK},
ArgsK"
ArgsK},{"
VoidK"
VoidK},{"
Var_DeclK"
Var_DeclK},{"
Arry_DeclK"
Arry_DeclK},{"
Arry_ElemK"
Arry_ElemK},{"
AssignK"
AssignK},
/*{"
WhileK"
WhileK},*/{"
OpK"
OpK},{"
Selection_StmtK"
Selection_StmtK},{"
Iteration_StmtK"
Iteration_StmtK},{"
Return_StmtK"
Return_StmtK}};
struct//符号与字符串关系
tk;
}Ope[11]
ASSIGN},{"
=="
EQ},{"
LT},{"
+"
PLUS},{"
-"
MINUS},{"
*"
TIMES},{"
/"
OVER},
GT},{"
GEQ},{"
NEQ},{"
LEQ}};
OpeLookUp(TokenType
tk)//操作符转换为字符串
for(i=0;
i<
11;
if(tk==Ope[i].tk)
Ope[i].str;
Change(NodeKind
nk)//节点类型转换为字符串
19;
if(nk==nodekind[i].nk)
nodekind[i].str;
token;
struct
TreeNode
struct
TreeNode
child[4];
sibling;
NodeKind
nodekind;
union
op;
val;
const
name;
}
attr;
declaration_list(void);
declaration(void);
params(void);
param_list(TreeNode
p);
param(TreeNode
compound_stmt(void);
local_declaration(void);
statement_list(void);
statement(void);
expression_stmt(void);
selection_stmt(void);
iteration_stmt(void);
return_stmt(void);
expression(void);
var(void);
simple_expression(TreeNode
additive_expression(TreeNode
term(TreeNode
factor(TreeNode
call(TreeNode
args(void);
copyString(char
*s)
n;
char
t;
(s==NULL)
NULL;
n=strlen(s)+1;
t=(char*)malloc(n);
(t==NULL){
strcpy(t,s);
match(TokenType
expected)
if(token==expected)
token=GetToken();
cout<
unexpected
token"
endl;
newNode(NodeKind
kind)
TreeNode
t
(TreeNode
*)
malloc(sizeof(TreeNode));
(t==NULL)
;
else
(i=0;
4;
i++)
t->
child[i]
t->
sibling
nodekind
kind;
lineno
(kind==OpK||kind==IntK||kind==IdK)
if(kind==IdK)
attr.name
if(kind==ConstK)
attr.val
declaration_list(void)//declaration_list->
declaration_list
declaration
|
declaration
declaration();
p
=t;
while((token!
=INT)&
(token!
=VOID)&
=ENDFILE))
{
token
GetToken();
if(token==ENDFILE)
while(token==INT||token==VOID)
TreeNode
q;
q
if
(q!
=NULL)
t=p=q;
p->
sibling=q;
p=q;
match(ENDFILE);
declaration(void)
q
s
a
(token==INT)
p=newNode(IntK);
match(INT);
else//(token==VOID)
p=newNode(VoidK);
match(VOID);
if(p!
=NULL&
token==ID)
newNode(IdK);
q->
copyString(tokenString);
match(ID);
(token==LPAREN)
t
newNode(FuncK);
t->
child[0]
p;
//p是t子节点
child[1]
match(LPAREN);
child[2]
params();
match(RPAREN);
child[3]
compound_stmt();
else
(token==LBRACKET)
newNode(Var_DeclK);
a
newNode(Arry_DeclK);
a;
match(LBRACKET);
s
newNode(ConstK);
s->
atoi(tokenString);
match(NUM);
a->
child[0]=q;
child[1]=s;
match(RBRACKET);
match(SEMI);
(token==SEMI)
;
params(void)//params_list
void
newNode(ParamsK);
(token==VOID)
p
newNode(VoidK);
(token==RPAREN)
if(t!
else//参数列表为(void
id,[……])
param_list(p);
else//(token==INT)
k)//params_list->
params_list,param
param
param(k);
k
//没有要传给param的VoidK,所以将k设为NULL
while
(token==COMMA)
match(COMMA);
p
k)
Tr