编译原理课程设计报告c-语言词法与语法分析器的实现(编辑修改稿)内容摘要:

p = newStmtNode(AssignK)。 p = t。 match(token)。 pchild[0] = expression()。 t = p。 } if (token == LPAREN ) { TreeNode * p = newStmtNode(CallK)。 p = t。 t=p。 match(token)。 pchild[0] = args()。 match(RPAREN)。 } break。 case LPAREN : match(LPAREN)。 t = expression()。 match(RPAREN)。 break。 default: syntaxError(unexpected token )。 printToken(token,tokenString)。 token = getToken()。 break。 } return t。 } TreeNode * args(void) { TreeNode * t = newStmtNode(ArgList)。 if(token != RPAREN) { tchild[0] = arg_list()。 return t。 }else return NULL。 } TreeNode * arg_list(void) { TreeNode * t = newStmtNode(ArgK)。 int i = 1。 if(token != RPAREN) tchild[0] = expression()。 while(token!=RPAREN) { match(DOT)。 tchild[i] = expression()。 i++。 } return t。 } TreeNode * parse(void) { TreeNode * t。 token = getToken()。 t =declaration_list()。 if (token!=ENDFILE) syntaxError(Code ends before file\n)。 return t。 } include include include /*对扫描的字符进行匹配判断 */ TokenType getToken(void) { /* index for storing into tokenString */ int tokenStringIndex = 0。 /* holds current token to be returned */ TokenType currentToken。 /* current state always begins at START */ StateType state = START。 /* flag to indicate save to tokenString */ int save。 while (state != DONE) { int c = getNextChar()。 save = TRUE。 switch (state) { case START: if (isdigit(c)) state = INNUM。 else if (isalpha(c)) state = INID。 else if (c == 39。 =39。 ) state = INEQUAL。 else if (c == 39。 39。 ) state = INLE。 else if (c == 39。 39。 ) state = INME。 else if ((c == 39。 39。 ) || (c == 39。 \t39。 ) || (c == 39。 \n39。 )) save = FALSE。 else if (c== 39。 !39。 ) state = INNEQ。 else if (c == 39。 /39。 ) { if(getNextChar()!=39。 *39。 ) { ungetNextChar()。 state = DONE。 currentToken = OVER。 break。 } else { save = FALSE。 state = INCOMMENT。 } } else { state = DONE。 switch (c) { case EOF: save = FALSE。 currentToken = ENDFILE。 break。 case 39。 +39。 : currentToken = PLUS。 break。 case 39。 39。 : currentToken = MINUS。 break。 case 39。 *39。 : currentToken = TIMES。 break。 case 39。 (39。 : currentToken = LPAREN。 break。 case 39。 )39。 : currentToken = RPAREN。 break。 case 39。 39。 : currentToken = SEMI。 break。 case 39。 [39。 : currentToken=LZKH。 break。 case 39。 ]39。 : currentToken=RZKH。 break。 case 39。 {39。 : currentToken=LDKH。 break。 case 39。 }39。 : currentToken=RDKH。 break。 case 39。 ,39。 : currentToken=DOT。 break。 default: currentToken = ERROR。 break。 } } break。 case INCOMMENT: save = FALSE。 if (c == EOF) { state = DONE。 currentToken = ERROR。 } else if(c==39。 *39。 ) { if(getNextChar()==39。 /39。 ) { state = START。 } else { ungetNextChar()。 } } break。 case INNEQ: state=DONE。 if(c==39。 =39。 ) currentToken=NEQ。 else { ungetNextChar()。 save=FALSE。 currentToken=ERROR。 } break。 case INEQUAL: state = DONE。 if (c == 39。 =39。 ) currentToken = EQ。 else { /* backup in the input */ ungetNextChar()。 currentToken = ASSIGN。 } break。 case INNUM: if (!isdigit(c)) { /* backup in the input */ ungetNextChar()。 save = FALSE。 state = DONE。 currentToken = NUM。 } break。 case INID: if (!isalpha(c)) { /* backup in the input */ ungetNextChar()。 save = FALSE。 state = DONE。 currentToken = ID。 } break。 case INLE: state = DONE。 if(c== 39。 =39。 ) currentTok。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。