Exemplo n.º 1
0
        /*
         * 语法解析:乘法表达式
         * **/
        private SimpleASTNode multiplicative(ITokenReader tokens)
        {
            SimpleASTNode child1 = primary(tokens);
            SimpleASTNode node   = child1;

            IToken token = tokens.peek();

            if (child1 != null && token != null)
            {
                if (token.getType() == TokenType.Star || token.getType() == TokenType.Slash)
                {
                    token = tokens.read();
                    SimpleASTNode child2 = primary(tokens);
                    if (child2 != null)
                    {
                        node = new SimpleASTNode(ASTNodeType.Multiplicative, token.getText());
                        node.addChild(child1);
                        node.addChild(child2);
                    }
                    else
                    {
                        throw (new FormatException("无效的乘法表达式!"));
                    }
                }
            }
            return(node);
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            SimpleCalculator calculator = new SimpleCalculator();

            String script = "int a = b + 3;";

            Console.WriteLine("解析变量声明语句:" + script);
            SimpleLexer  lexer  = new SimpleLexer();
            ITokenReader tokens = lexer.tokenize(script);

            try
            {
                SimpleASTNode node = calculator.intDeclare(tokens);
                calculator.dumpAST(node, "");
            }catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            script = "2+3*5";
            Console.WriteLine("\n计算:{0}", script);
            calculator.evaluate(script);

            script = "2+";
            Console.WriteLine("\n计算:{0}", script);
            calculator.evaluate(script);

            script = "2+3+4*2+10+10/5";
            Console.WriteLine("\n计算:{0}", script);
            calculator.evaluate(script);

            Console.ReadKey();
        }
Exemplo n.º 3
0
        /*
         * 语法解析:根节点
         * **/
        private SimpleASTNode prog(ITokenReader tokens)
        {
            SimpleASTNode node  = new SimpleASTNode(ASTNodeType.Programm, "Calculator");
            SimpleASTNode child = additive(tokens);

            if (child != null)
            {
                node.addChild(child);
            }
            return(node);
        }
Exemplo n.º 4
0
        /*
         * 整型变量声明语句,如:int a; int b = 2*3;
         * **/
        private SimpleASTNode intDeclare(ITokenReader tokens)
        {
            SimpleASTNode node  = null;
            IToken        token = tokens.peek();

            if (token.getType() == TokenType.Int)
            {
                tokens.read();
                token = tokens.peek();
                if (token.getType() == TokenType.Identifier)
                {
                    token = tokens.read();
                    node  = new SimpleASTNode(ASTNodeType.IntDeclaration, token.getText());
                    token = tokens.peek();
                    if (token.getType() == TokenType.Assignment)
                    {
                        tokens.read();
                        SimpleASTNode child = additive(tokens);
                        if (child != null)
                        {
                            node.addChild(child);
                        }
                        else
                        {
                            throw (new FormatException("'='赋值表达式错误"));
                        }
                    }
                }
                else
                {
                    throw (new FormatException("需要一个变量名"));
                }

                if (node != null)
                {
                    token = tokens.peek();
                    if (token != null && token.getType() == TokenType.SemiColon)
                    {
                        tokens.read();
                    }
                    else
                    {
                        throw (new FormatException("表达式缺少分号"));
                    }
                }
            }
            return(node);
        }
Exemplo n.º 5
0
        /*
         * 语法解析:基础表达式
         * **/
        private SimpleASTNode primary(ITokenReader tokens)
        {
            SimpleASTNode node  = null;
            IToken        token = tokens.peek();

            if (token != null)
            {
                if (token.getType() == TokenType.IntLiteral)
                {
                    token = tokens.read();
                    node  = new SimpleASTNode(ASTNodeType.IntLiteral, token.getText());
                }
                else if (token.getType() == TokenType.Identifier)
                {
                    token = tokens.read();
                    node  = new SimpleASTNode(ASTNodeType.Identifier, token.getText());
                }
                else if (token.getType() == TokenType.LeftParen)
                {
                    token = tokens.read();
                    node  = additive(tokens);
                    if (node != null)
                    {
                        token = tokens.peek();
                        if (token != null && token.getType() == TokenType.RightParen)
                        {
                            tokens.read();
                        }
                        else
                        {
                            throw (new FormatException("缺少右括号"));
                        }
                    }
                    else
                    {
                        throw (new FormatException("无效的表达式"));
                    }
                }
            }
            return(node);
        }
Exemplo n.º 6
0
 public void addChild(SimpleASTNode child)
 {
     children.Add(child);
     child.parent = this;
 }