Solved

Unhandled exception: System.IndexOutOfRangeException:

Posted on 2012-03-18
2
564 Views
Last Modified: 2012-03-19
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

0
Comment
Question by:crazy4s
2 Comments
 
LVL 20

Accepted Solution

by:
a_b earned 500 total points
ID: 37734985
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
 

Author Comment

by:crazy4s
ID: 37737248
oops i'm supposed to pass in the test filename in the command line arguments...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now