Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Unhandled exception: System.IndexOutOfRangeException:

Posted on 2012-03-18
2
Medium Priority
?
598 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 1500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

885 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