Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to create my own SIMPLE Language for my C# application with commands such as:

MAKE_LETTER
MESSAGE
UPDATE
CREATECASE
IF ELSE END

However i am having issues parsing the IF Statement. I need it so that if the condition with the if statement is true then it goes to the Next END in the textfile. However if the condition is not met then Go to the NExt ELSE. I have included my code below so that you can see where i am going wrong.

Any help would be much appreicated!

What I have tried:

C#
<pre>string strLine = string.Empty;

            string strFile = @"d:\test.txt";

            ArrayList arr = new ArrayList();

            StreamReader sr = new StreamReader(strFile);

            while (sr.Peek() >= 0)

            {

                strFile = sr.ReadLine();

                arr.Add(strFile);

            }

            for (int i = 0; i < arr.Count; i++)

            {

                if (i > 0)

                {

                    string s1 = arr[i].ToString();
                   // MessageBox.Show(s1);
                    if (s1.Contains("IF"))
                    {
                        string field = "";
                        string message = "";
                        bool con_met = false;

                        if (s1.Contains("[") || s1.Contains("]"))
                        {
                            field = s1.Split('[', ']')[1];
                            string table = field.Split('.')[0];
                            string field_value = field.Substring(field.LastIndexOf('.') + 1);
                            string value = Run_Code.Get_db_val123(table, field_value);
                            Global_Variables.case_ref = "TEST";
                            //MessageBox.Show(value);
                            message = s1.Split('"', '"')[1];
                           // MessageBox.Show("IF " + value + "== " + message);
                            if (value == message)
                            {

                                MessageBox.Show("Conditon Met");
                                //MessageBox.Show(s1)
                                //i = i + 1;
                                i = End_pos;



                            }
                            else
                            {
                                i = Else_pos;
                                //MessageBox.Show("ELSE");
                                
                            }


The Code in the Text Files Looks like this:
//MESSAGE("Testing123")

IF "Patrick" = "Patrick" THEN
MAKE_LETTER(test150)
ELSE
//DO Something here
END
Posted
Updated 7-Apr-20 0:24am
Comments
BillWoodruff 7-Apr-20 6:22am    
are you the author of the format of the file you read ?
patrickb123 7-Apr-20 6:33am    
Yes.
BillWoodruff 7-Apr-20 6:41am    
why did you decide to use this format ?

The logic is quite simple, you just need to separate the parts of the statement and check the condition operator:
IF verb EQUALS "IF"
BEGIN
    Parse the statement into operand1, operator, operand2
    IF operator EQUALS "="
    BEGIN
        compare the two operands and if equal go to the next statement
        if not equal then skip the next statement
    END
END

While that is over simplified it should give you something to work on. You may find a better way is to use a ready made language parser. This lady: honey the codewitch - Professional Profile[^] (@code-witch) has written some excellent articles on the subject.
 
Share this answer
 
Comments
patrickb123 7-Apr-20 6:36am    
Hi Richard,

Thanks for your reply, if i was to use this method, how would i parse the textfile and skip to the next ELSE/END?
Richard MacCutchan 7-Apr-20 6:47am    
The same way basically. Parsing is a matter of splitting statements into tokens and matching the tokens to your rule set. But once you move from simple one liners into compound statements it gets a bit more complicated. Follow the link I gave you and take a look at some of the articles.
Don't do it like that: it's far, far too easy to have "false positives":
C#
MAKE_LETTER("QUIFF")
for example will match your:
C#
if (s1.Contains("IF"))
and that'll throw everything out.
Instead, consider writing a tokenizer[^] and processing the user code as a stream of tokens. There are a fair number of lexical processors out there: Google[^] will show you a couple of hundred on this site alone. I'd have a look at some of those articles (though they can get pretty heavy for a beginner) and see what they are doing, why they are doing it, and maybe how they are doing it before going any further.
A simplistic approach like you current one will just end up with more, and more special cases as you process more and more complex code - and will quickly become unreliable and unmaintainable! It's worth thinking about "doing it properly" right from the start, as it'll save you a lot of time and heartache later - the more time and effort you invest in the simplistic approach, the harder it is so say "Sod it! this won't work" and throw it away. That's still one of the things I find hardest to do: bin code I've worked hard on because it clearly isn't going to do what I want without major faffing about. And I've been doing this for decades! :laugh:
 
Share this answer
 
Comments
patrickb123 7-Apr-20 7:39am    
Hi, i have taken a look at the link you provided at a lexical parsers. However i was wondering whether you were free for a screenshare so that i could get it working. As im not sure how to run it?

The link is: https://codereview.stackexchange.com/questions/172026/simple-language-tokenizer-in-c

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900