Solved

Unhandled exception: System.IndexOutOfRangeException:

Posted on 2012-03-18
2
579 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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