Solved

Unhandled exception: System.IndexOutOfRangeException:

Posted on 2012-03-18
2
569 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 83
Counting connections to SQL Server through C# 3 33
send messages to whatsapp programatically 2 49
.NET 2008 VB and C# 6 28
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…

821 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