Unhandled exception: System.IndexOutOfRangeException:

Hi all,
I'm getting this error while trying to run the below program,

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
     at AbstractSyntax.Program.Main(String[] args) ... : line 13

Can anyone figure out what is the error meant?
Thanks in advance.

Below is part of the program:

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractSyntax;

namespace AbstractSyntax
{
    class Program
    {
        static void Main(string[] args)
        {
            Parser myParser = new Parser(args[0]);
            myParser.program();
        }
    }
}

Open in new window


Parser.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractSyntax
{
    class Parser
    {
        Lexer mylexer;
        Token current_token;
        bool error_flag = false;

        public Parser(String fileName)
        {
            mylexer = new Lexer(fileName);
            current_token = mylexer.next();
        }

        public void program()
        {
            match(Token.TokenType.Int);
            match(Token.TokenType.Main);
            match(Token.TokenType.LeftParen);
            match(Token.TokenType.RightParen);
            match(Token.TokenType.LeftBrace);
            List<Declaration> decls = declarations();
            List<Statement> stmts = statements();
            match(Token.TokenType.RightBrace);

            if (error_flag == false)
                Console.WriteLine("No Syntax Error. Comgratulation!");

            CProgram p = new CProgram(decls,stmts);
        }

        private List<Declaration> declarations()
        {
            List<Declaration> ds = new List<Declaration>();
            List<Declaration> dsl = new List<Declaration>();
            while (current_token.getType() == Token.TokenType.Int ||
                current_token.getType() == Token.TokenType.Float ||
                current_token.getType() == Token.TokenType.Char ||
                current_token.getType() == Token.TokenType.Bool)
            {
                dsl = declaration();
                ds.AddRange(dsl);
            }
            return ds; 
        }

        private List<Declaration> declaration()
        {
            List<Declaration> result = new List<Declaration>();
            Type t = type();
            Variable v = new Variable(match(Token.TokenType.Identifier));

            if (current_token.getType() == Token.TokenType.LeftBracket)
            {
                match(Token.TokenType.LeftBracket);
                int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
                match(Token.TokenType.RightBracket);
                ArrayDecl ad = new ArrayDecl(v, t, i);
                result.Add(ad);
            }
            else
            {
                VariableDecl vd = new VariableDecl(v, t);
                result.Add(vd);
            }

            while (current_token.getType() == Token.TokenType.Comma)
            {
                match(Token.TokenType.Comma);
                Variable v1 = new Variable(match(Token.TokenType.Identifier));
                if (current_token.getType() == Token.TokenType.LeftBracket)
                {
                    match(Token.TokenType.LeftBracket);
                    int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
                    match(Token.TokenType.RightBracket);
                    ArrayDecl ad = new ArrayDecl(v1, t, i);
                    result.Add(ad);
                }
                else
                {
                    VariableDecl vd2 = new VariableDecl(v1, t);
                    result.Add(vd2);
                }
            }

            match(Token.TokenType.Semicolon);

            return result;
        }

        private Type type()
        {
            Type t = null;
            switch (current_token.getType())
            {
                case Token.TokenType.Int:
                case Token.TokenType.Char:
                case Token.TokenType.Float:
                case Token.TokenType.Bool:
                    t = new Type(current_token.toString());
                    current_token = mylexer.next();
                    return t;
                default:
                    Console.WriteLine("Syntax error: line " + mylexer.getCurrentLine() + " expecting: int, char, bool, or float" + "; saw: " + current_token.getType() + "value is " + current_token.toString());
                    current_token = mylexer.next();
                    error_flag = true;
                    break;
            }

            return t;
        }

        private List<Statement> statements()
        {
            List<Statement> st = new List<Statement>();
            Statement stl;
            while (current_token.getType() == Token.TokenType.Semicolon ||
                current_token.getType() == Token.TokenType.LeftBrace ||
                current_token.getType() == Token.TokenType.Identifier ||
                current_token.getType() == Token.TokenType.If ||
                current_token.getType() == Token.TokenType.While)
            {
                stl = statement();
                st.Add(stl);
            }
            return st;
        }

        private Statement statement()
        {
            Statement s = null;
            if (current_token.getType() == Token.TokenType.Semicolon)
            {
                s = new Skip();
                current_token = mylexer.next();
            }
            else if (current_token.getType() == Token.TokenType.LeftBrace)
            {
                match(Token.TokenType.LeftBrace);
                s = statement();
                match(Token.TokenType.RightBrace);
            }
            else if (current_token.getType() == Token.TokenType.Identifier)
            {
                s = assignment();
            }
            else if (current_token.getType() == Token.TokenType.If)
            {
                s = ifstatement();
            }
            else if (current_token.getType() == Token.TokenType.While)
            {
                s = whilestatement();
            }
            return s;
        }

        private Statement block()
        {
            Statement sb = null;
            match(Token.TokenType.LeftBrace);
            sb = statement();
            match(Token.TokenType.RightBrace);
            return sb;
        }

        private Assignment assignment()
        {
            Variable target = new Variable(match(Token.TokenType.Identifier));
            match(Token.TokenType.Assign);
            Expression source = expression();
            match(Token.TokenType.Semicolon);
            Assignment a = new Assignment(target, source);
            return a;
        }

        private Conditional ifstatement()
        {
            match(Token.TokenType.If);
            match(Token.TokenType.LeftParen);
            Expression e = expression();
            match(Token.TokenType.RightParen);
            Statement tb = statement();
            Conditional c = new Conditional(e, tb);
            if (current_token.getType() == Token.TokenType.Else)
            {
                match(Token.TokenType.Else);
                Statement eb = statement();
                c = new Conditional(e, tb, eb);
            }

            return c;
        }

        private Loop whilestatement()
        {
            match(Token.TokenType.While);
            match(Token.TokenType.LeftParen);
            Expression t = expression();
            match(Token.TokenType.RightParen);
            Statement b = statement();
            Loop l = new Loop(t, b);

            return l;
        }

        private Expression expression()
        {
            Expression e = conjunction();
            while (current_token.getType() == Token.TokenType.Or)
            {
                Operator op = new Operator(match(current_token.getType()));
                Expression term2 = conjunction();
                e = new Binary(op, e, term2);
            }

            return e;
        }

        private Expression conjunction()
        {
            Expression e = equality();
            while (current_token.getType() == Token.TokenType.And)
            {
                Operator op = new Operator(match(current_token.getType()));
                Expression term2 = equality();
                e = new Binary(op, e, term2);
            }
            return e;
        }

        private Expression equality()
        {
            Expression e = relation();
            if (isEquOp())
            {
                Operator op = new Operator(match(current_token.getType()));
                Expression term2 = relation();
                e = new Binary(op, e, term2);
            }
            return e;
        }

        private Expression relation()
        {
            Expression e = addition();
            if (isRelOp())
            {
                Operator op = new Operator(match(current_token.getType()));
                Expression term2 = addition();
                e = new Binary(op, e, term2);
            }
            return e;
        }

        private Expression addition()
        {
            Expression e = term();
            while (isAddOp())
            {
                Operator op = new Operator(match(current_token.getType()));
                Expression term2 = term();
                e = new Binary(op, e, term2);
            }
            return e;
        }

        private Expression term()
        {
            Expression e = factor();
            while (isMulOp())
            {
                Operator op = new Operator(match(current_token.getType()));
                Expression term2 = factor();
                e = new Binary(op, e, term2);
            }

            return e;
        }

        private Expression factor()
        {
            if (isUnaryOp())
            {
                UnaryOp op = new UnaryOp(match(current_token.getType()));
                Expression term = primary();
                return new Unary(op, term);

            }
            else
                return primary();
        }

        private Expression primary()
        {
            Expression e = null;
            if (current_token.getType() == Token.TokenType.Identifier)
            {
                e = new Variable(match(Token.TokenType.Identifier));
            }
            else if (isLiteral())
            {
                e = literal();
            }
            else if (current_token.getType() == Token.TokenType.LeftParen)
            {
                current_token = mylexer.next();
                e = expression();
                match(Token.TokenType.RightParen);
            }
            else if (isType())
            {
                UnaryOp op = new UnaryOp(match(current_token.getType()));
                match(Token.TokenType.LeftParen);
                Expression term = expression();
                match(Token.TokenType.RightParen);
                e = new Unary(op, term);
            }

            return e;
        }

        private Value literal()
        {
            return null;
        }

        private bool isAddOp()
        {
            return current_token.getType() == Token.TokenType.Plus ||
                current_token.getType() == Token.TokenType.Minus;
        }

        private bool isMulOp()
        {
            return current_token.getType() == Token.TokenType.Multiply ||
                current_token.getType() == Token.TokenType.Divide;
        }

        private bool isUnaryOp()
        {
            return current_token.getType() == Token.TokenType.Minus ||
                current_token.getType() == Token.TokenType.Not;
        }

        private bool isEquOp()
        {
            return current_token.getType() == Token.TokenType.Equals ||
                current_token.getType() == Token.TokenType.NotEqual;
        }

        private bool isRelOp()
        {
            return current_token.getType() == Token.TokenType.Less ||
                current_token.getType() == Token.TokenType.LessEqual ||
                current_token.getType() == Token.TokenType.Greater ||
                current_token.getType() == Token.TokenType.GreaterEqual;
        }

        private bool isType()
        {
            return current_token.getType() == Token.TokenType.Int ||
                current_token.getType() == Token.TokenType.Bool ||
                current_token.getType() == Token.TokenType.FloatLiteral ||
                current_token.getType() == Token.TokenType.CharLiteral;
        }

        private bool isLiteral()
        {
            return current_token.getType() == Token.TokenType.IntLiteral ||
                isBooleanLiteral() ||
                current_token.getType() == Token.TokenType.FloatLiteral ||
                current_token.getType() == Token.TokenType.CharLiteral;
        }

        private bool isBooleanLiteral()
        {
            return current_token.getType() == Token.TokenType.True ||
                current_token.getType() == Token.TokenType.False;
        }

        private String match(Token.TokenType t)
        {
            String value = current_token.getValue();
            if (current_token.getType() == t)
                current_token = mylexer.next();
            else
                error(t);
            return value;
        }

        private void error(Token.TokenType tok)
        {
            error_flag = true;
            Console.WriteLine("Syntax error: line " + mylexer.getCurrentLine() + " expecting: " + tok + "; saw: " + current_token.getType() + "value is " + current_token.toString());
            Environment.Exit(0);
        }
    }
}

Open in new window

crazy4sAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

a_bCommented:
Are you passing in the command line argument correctly to the program?

Parser myParser = new Parser(args[0]) => What is the length of args ?

Modify the program as follows -

 static void Main(string[] args)
        {
           if(args.length > 0 )
           {
               Parser myParser = new Parser(args[0]);
                myParser.program();
            }
            else
           {
              Console.Writeline("No command line args passed");
           }
        }


Run the program and post what the error message is.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crazy4sAuthor Commented:
oops i'm supposed to pass in the test filename in the command line arguments...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.