Results 1 to 9 of 9

Thread: Parser

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Parser

    Hi
    Im taking a Language and Compiler course as a part of my software engineering degree. In that part, i tought it could be fun developing my own mini calculator to make something practical. I have learnt the needed tools such as Automatons, Lexers,Scanners,EBNF and the like. I understand the theory perfectly well, the problem is that I have problems translating this theory to actual code. My fellow students didnt have any troubles with making their own parser, took them a weekend and they where done. But I dont know where to start...very frustrating.

    I have made a small grammar of the calculator, so I have that

    Command ::= Expression =
    Expresstion ::= Numeral((+ | - | *) Numeral)*
    Numeral ::= Digit Digit*
    Digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

    I would really hope that you guys could help me do the actual coding part, some hints to get started, maybe some examples.

    Thanks in advance

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Parser

    I have made this small code snippet, but there is some problems in the Eqauls(operator)...

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace minicalc
    {
        class Program
        {
            public static String input, tmpexp;       // Expression
            public static int length;         // Lenght of input
            public static int curPos;         // Current token
            public static int value, temp = 0;    // value holders
            public static String Op;
            static void Main(string[] args)
            {
               
                
    
                System.Console.WriteLine("Please enter expression to calculate: ");
                input = System.Console.ReadLine();
                
    
                length = input.Length;
                curPos = 0;
    
                while (curPos < length)
                {
                    if ((!input[curPos].Equals("+")) | (!input[curPos].Equals( "-")) | (!input[curPos].Equals("*")))
                    {
                        tmpexp += input[curPos];
                        System.Console.WriteLine(input[curPos]);
    
                    }
                    temp += Convert.ToInt32(tmpexp);
                    //System.Console.WriteLine(tmpexp);
                    Op = Convert.ToString( input[curPos + 1]);
    
                    // Determine which operator we have
                    switch (Op)
                    {
                        case "+": value += temp;
                            break;
                        case "-": value -= temp;
                            break;
                        case "*": value *= temp;
                            break;
                    }
                    
                    curPos++;
                    
    
    
                }
    
                System.Console.WriteLine(value);
                System.Console.ReadLine();
    
            }
        }
    }
    It reads the operators in the value string even tough it shouldn't
    Last edited by carstenht; Mar 7th, 2007 at 09:59 AM.

  3. #3
    Lively Member
    Join Date
    Jun 2003
    Posts
    124

    Re: Parser

    have you thought about using regular expressions?

    here is a good starting point

    i have a degree in computer engineering and we did a decent sized study of regex when we had to write our own compiler.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Parser

    Sounds interesting ill take a look

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Parser

    but would really like this simple thing to work first

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Parser

    converted it to this:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace minicalc
    {
        class Program
        {
            public static String input, tmpexp;       // Expression
            public static int length;         // Lenght of input
            public static int curPos;         // Current token
            public static int value, temp = 0;    // value holders
            public static String Op;
            
            static void Main(string[] args)
    
            {
               
                
    
                System.Console.WriteLine("Please enter expression to calculate: ");
                input = System.Console.ReadLine();
                
    
                length = input.Length;
                curPos = 0;
                
                while (curPos < length)
                {
                    if (input[curPos] == 0 |input[curPos] == 1 |input[curPos] == 2 |input[curPos] == 3 | input[curPos] == 4 |input[curPos] == 5 |input[curPos] == 6 |input[curPos] == 7 |input[curPos] == 8 |input[curPos] == 9 )
                    {
                        tmpexp += input[curPos];
                        System.Console.WriteLine(input[curPos]);
    
                    }
                   
                    temp += Convert.ToInt32(tmpexp);
                   
                    
                    Op = Convert.ToString( input[curPos + 1]);
    
                    // Determine which operator we have and use it on the result
                    switch (Op)
                    {
                        case "+": value += temp;
                            break;
                        case "-": value -= temp;
                            break;
                        case "*": value *= temp;
                            break;
                    }
                    
                    curPos++;
                    
    
    
                }
    
                System.Console.WriteLine("Result of calculation: " + value);
                System.Console.ReadLine();
    
            }
        }
    }
    But now I get and array index out of bounds at:

    Code:
     Op = Convert.ToString( input[curPos + 1]);

  7. #7
    Lively Member
    Join Date
    Jun 2003
    Posts
    124

    Re: Parser

    try something like this:

    i made this using txtInput as my input text box, and txtOutput as my output text box
    Code:
                string strInput = txtInput.Text;
    
                //define your operands
                string strOps = "[+]|[*]|[-]";
                
                string[] myNumerals = new string[2];
                
                //determines which operand is present
                Match myMatch = Regex.Match(strInput, strOps);
    
                //gets the two numerals on each side of the operand
                myNumerals = Regex.Split(strInput, strOps);
                
                //myMatch.ToString() will return which operand was in the expression
                switch (myMatch.ToString())
                {
                    case "+":
                        txtOutput.Text = (Convert.ToDouble(myNumerals[0]) + Convert.ToDouble(myNumerals[1])).ToString();
                        break;
                    case "*":
                        txtOutput.Text = (Convert.ToDouble(myNumerals[0]) * Convert.ToDouble(myNumerals[1])).ToString();
                        break;
                    case "-":
                        txtOutput.Text = (Convert.ToDouble(myNumerals[0]) - Convert.ToDouble(myNumerals[1])).ToString();
                        break;
                }

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Parser

    hi
    i just tried your suggestion, but i get 3 errors in the Switch:
    Error 1 Cannot implicitly convert type 'string' to 'int'
    Error 2 Cannot implicitly convert type 'string' to 'int'
    Error 3 Cannot implicitly convert type 'string' to 'int'

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    162

    Re: Parser

    Im still quite determined to get my own code working, so i still get the something educational from it, and it appears that i never enter the if statement:

    Code:
    if (input[curPos].Equals("0") || input[curPos].Equals("1") || input[curPos].Equals("2") || input[curPos].Equals("3") || input[curPos].Equals("4") || input[curPos].Equals("5") || input[curPos].Equals("6") || input[curPos].Equals("7") || input[curPos].Equals("8") || input[curPos].Equals("9"))
                    {
                        System.Console.WriteLine("in if");
                        tmpexp += input[curPos];
                        System.Console.WriteLine(input[curPos]);
    
                    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width