Exemplo n.º 1
0
        /// <summary>
        /// 2. declaration-list → declaration-list declaration | declaration
        /// </summary>
        /// <returns></returns>
        TreeNode DeclarationList()
        {
            TreeNode program = null;
            TreeNode last    = null;
            TreeNode temp    = null;

            currentToken = GetNextToken();
            while (!CheckCurrentToken(TokenType.EOF))
            {
                if (!CheckCurrentToken(TokenType.INT) &&
                    !CheckCurrentToken(TokenType.VOID) && !CheckCurrentToken(';'))
                {
                    ErrorRecord(ParseErrorType.InvalidType);
                    ConsumeUntil(";", "}");
                    //错误处理
                    //scan->add_err();
                    //sprintf(msg_temp, "Syntax error: invalid type \'%s\'",
                    //    cToken.str.c_str());
                    //outputMsg(scan->lineno(), msg_temp);
                    //consumeUntil(SEMI, RBRACE); // ';', '}'
                }
                else if ((temp = Declaration()) != null)
                {
                    if (program == null)
                    {
                        program = temp;
                        last    = temp.LastSibling();
                    }
                    else
                    {
                        last.Sibling = temp;
                        last         = temp.LastSibling();
                    }
                }
                currentToken = GetNextToken();
            }
            return(program);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 10. compound-stmt → { local-declarations statement-list }
        /// </summary>
        /// <returns></returns>
        TreeNode CompoundStmt()
        {
            TreeNode first    = null;
            TreeNode last     = null;
            TreeNode temp     = null;
            bool     noBraces = false;

            if (!Match(Convert.ToInt32('{')))
            {// match "{"
                noBraces = true;
                --currentTokenIndex;
            }
            // local-declarations
            while (true)
            {
                typeToken = currentToken = GetNextToken();
                if (CheckCurrentToken(TokenType.INT) ||
                    CheckCurrentToken(TokenType.VOID))
                {
                    temp = LocalDeclarations();
                }
                else
                {
                    --currentTokenIndex;
                    break;
                }
                if (noBraces)
                {
                    return(temp);
                }
                if (temp != null)
                {
                    if (first == null)
                    {
                        first = temp;
                        last  = temp.LastSibling();
                    }
                    else
                    {
                        last.Sibling = temp;
                        last         = temp.LastSibling();
                    }
                }
            }

            //currentToken = GetNextToken();
            currentToken = GetNextToken();
            while (true)
            {
                temp = null;
                if (CheckCurrentToken('}'))
                {
                    if (noBraces)
                    {
                        //错误处理
                        ErrorRecord(ParseErrorType.UnpairedRBrace);
                    }
                    break;
                }
                if (CheckCurrentToken(TokenType.EOF))
                {
                    //错误处理
                    ErrorRecord(ParseErrorType.MissingRBraceBeforeEOF);
                    --currentTokenIndex;
                    break;
                }
                switch (currentToken.TokenTypes)
                {
                case (Int32)TokenType.READ:
                    temp = ReadStmt();
                    break;

                case (Int32)TokenType.WRITE:
                    temp = WriteStmt();
                    break;

                case (Int32)(';'):
                case (Int32)TokenType.NUMBER:
                case (Int32)('('):
                    temp = ExpressionStmt();
                    break;

                case (Int32)TokenType.ID:
                    temp = SubcompoundStmt();
                    break;

                case (Int32)TokenType.IF:
                    temp = IfStmt();
                    //  currentToken = GetNextToken();
                    break;

                case (Int32)TokenType.WHILE:
                    temp = WhileStmt();
                    break;

                case (Int32)TokenType.RETURN:
                    temp = ReturnStmt();
                    break;

                case (Int32)TokenType.ELSE:
                    //错误处理
                    ErrorRecord(ParseErrorType.UnpairedElseStatement);
                    ConsumeUntil(";", "}");
                    //scan->add_err();
                    //outputMsg(scan->lineno(), " unpaired \'else\' statement");
                    //consumeUntil(SEMI, RBRACE);
                    break;

                default:
                    //错误处理
                    ErrorRecord(ParseErrorType.UndefinedSymbol);
                    ConsumeUntil(";", "}");

                    //scan->add_err();
                    //sprintf(msg_temp, "Syntax error: undefined symbol \"%s\"", cToken.str.c_str());
                    //outputMsg(scan->lineno(), msg_temp);
                    //consumeUntil(SEMI, RBRACE);
                    break;
                }

                if (noBraces)
                {
                    return(temp);//// no braces
                }
                if (temp != null)
                {
                    if (first == null)
                    {
                        first = temp;
                        last  = temp.LastSibling();
                    }
                    else
                    {
                        last.Sibling = temp;
                        last         = temp.LastSibling();
                    }
                }

                currentToken = GetNextToken();
            }
            return(first);
        }