Пример #1
0
        public AST expr()
        {
            var token  = this.currentToken;
            var tokenT = this.currentToken.getType();

            //expr ::= [<unary_op>]<opnd>
            if (tokenT == TokenType.NOT)
            {
                this.eatToken(TokenType.NOT);
                return(this.checkStatus(new unaryOpNode(this.opnd(), token)));
            }

            //expr ::= <opnd> <op> <opnd>
            AST node = this.opnd();

            token  = this.currentToken;
            tokenT = this.currentToken.getType();

            if (tokenT == TokenType.PLUS)
            {
                this.eatToken(TokenType.PLUS);
            }
            else if (tokenT == TokenType.MINUS)
            {
                this.eatToken(TokenType.MINUS);
            }
            else if (tokenT == TokenType.DIV)
            {
                this.eatToken(TokenType.DIV);
            }
            else if (tokenT == TokenType.MULT)
            {
                this.eatToken(TokenType.MULT);
            }
            else if (tokenT == TokenType.EQUALS)
            {
                this.eatToken(TokenType.EQUALS);
            }
            else if (tokenT == TokenType.LESSTHAN)
            {
                this.eatToken(TokenType.LESSTHAN);
            }
            else if (tokenT == TokenType.AND)
            {
                this.eatToken(TokenType.AND);
            }
            else
            {
                return(node);
            }
            node = new opNode(node, this.opnd(), token);
            return(this.checkStatus(node));
        }
Пример #2
0
        private static Node MakeTree(string s, char op)
        {
            int pcounter = 0;                       // parenthesis counter

            for (int i = s.Length - 1; i >= 0; i--) // 0 based indexing, start from right of the string
            {
                // keep count of total parentesis open and closed
                if (')' == s[i]) // check if closing parenthesis
                {
                    pcounter++;
                }
                else if ('(' == s[i]) // check for open parenthesis
                {
                    pcounter--;
                }

                // no mismatching parenthesis set op to a valid op, do not want to set parenthesis to opnode
                if (0 == pcounter && op == s[i])
                {
                    opNode on = new opNode(s[i], MakeTree(s.Substring(0, i)), MakeTree(s.Substring(i + 1))); // make a new opnode
                    return(on);
                }
            }

            if (pcounter != 0) // no matching parenthesis
            {
                if (pcounter < 0)
                {
                    throw new Exception("Missing 1 or more closing parenthesis");
                }
                else
                {
                    throw new Exception("missing 1 or more opening parenthesis");
                }
            }

            return(null); // no node created
        }