Hi,

Basically im looking to create a basic C# compiler or parser if you like that will allow users to create simple programs that can declare variables, assign variable values etc.

Ive began with the main functionality within the application, this being the parser which shall pass in user code as a string and delimit this into a series of tokens to determine the command the user wishes to execute. I am trying to use substring to add these string tokens to a String array list but keep getting an out of bounds exception when running this. It appears to work fine for lines of code such as 'int x' but becomes problematic on statements such as 'int x;'. Here is the code for the parser:

using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Compiler
{
    class Parser
    {
        public String inputCode;
        public Interface itf;

        //read in lines of code
        public StringReader sr;

        //array lists to store each line/delimited string tokens of each line
        ArrayList pLines;
        ArrayList tkns;

        int c = 0;

        public Parser(Interface inf) 
        {
            itf = inf;
            pLines = new ArrayList();
            //tkns = new ArrayList();
        }

        public void parseCode(String parseType)
        {
            if (parseType.Equals("Run"))
            {
                //start point to read parsed string
                int start = 0;
                tkns = new ArrayList();
                //get user code
                inputCode = itf.UserCode.Text;
                //create new string reader - read in lines of code
                sr = new StringReader(inputCode);
                
                inputCode = itf.UserCode.Text;
                //loop while there is a line to read
                while ((inputCode = sr.ReadLine()) != null)
                {
                    for (int i = 0; i < inputCode.Length; i++)
                    {
                        if (inputCode[i] == ' ' || inputCode[i] == ';')
                        {
                           tkns.Add(inputCode.Substring(start,i));
                           start = i + 1;
                        }
                    }

                    pLines.Add(tkns);
                }
           }
        }
    }
}




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Compiler
{
    public partial class Interface : Form
    {
        public String pType;


        public Interface()
        {
            InitializeComponent();
        }

        private void ShowMem_Click(object sender, EventArgs e)
        {
            Memory mem = new Memory();
            mem.Show();
        }

        private void RunProg_Click(object sender, EventArgs e)
        {
            pType = "Run";
            Parser p = new Parser(this);
            p.parseCode(pType);
        }
    }
}

Just wondering if you could identify why I am getting this exception and a possible solution to this? If this cannot be done using this method, is there a possible alternative that could be implemented to emulate the parser I wish to create?

Thanks for all your help, much appreciated.

Dan

To give you the full exception, here is the message:

ArgumentOutOfRange Exception was unhandled

Index and length must refer to a location within the string.
Parameter name: length

Think you can remove line 45, it is already declared on line 41
You define an ArrayList on line 39 This ArrayList will happily vanish into oblivion once your PaeseCode method ends. I don't think it is meant to be so.
On line 53 start is a position in a string, i is the lenght of the substring.
When iterating throug the string, start+i get bigger then the length of your inputCode string, hence the error.
See http://msdn.microsoft.com/en-us/library/aka44szs(v=vs.90).aspx for reference.
In my code snippets you will find 3 codes how I did something similar what you are trying to do.
Here is how I did the parser.
http://www.daniweb.com/software-development/csharp/code/217186
Find the other two in my snippets and perhaps they can serve as a base for you to extend it or just as a reference to proceed with your code.
Success :)

Edited 4 Years Ago by ddanbe: n/a

thanks for the response, ive cleaned up the redundant code as you suggested. Ive ran a debug again using the 'int x;' and within the loop just before the second string token is added to the arraylist (delimiting character being the semi colon), the value of start is 4 and i is 5. The total length of the string read in is 6 meaning im a little confused as to why the exception is occurring.

Thanks again for any help

Dan

Well, you said it yourself!
The total lenght of your string is 6, meaning it has positions 0 1 2 3 4 5
Your variable start has a value of 4, meaning it is referring to position 4 in your string 0 1 2 3 4 5
If you want to take a substring at position4 with a lenght of 5 (i=5) you want to take a part of your string that does not exist.
0 1 2 3 4 5 6 7 8 so you get an error.
Hope this removes the clouds around your head and that all is cristalclear :)

Edited 4 Years Ago by ddanbe: n/a

Ahh right I see now :) that really has cleared up the length parameter using substrings! I can see how i am encountering this exception. I shall have a look at the code you suggested to see if it is possible to code an alternative solution to this.

Thanks again

This article has been dead for over six months. Start a new discussion instead.