none
Parse negative number RRS feed

  • Question

  • So I've been working on an assignment for my study AI, and as a final project I need to build a SAT solver.. My question is how you can make a parser that recognizes negations. It is writing in a consoleapplication:

                

    private IFormule ParseFactor()
            {

                SkipSpaces();
                if (cursor<lengte && inhoud[cursor] == '(')
                {
                    cursor++; // passeer het openingshaakje
                    IFormule resultaat = ParseExpressie(); 
                    SkipSpaces();
                    if (inhoud[cursor] != ')') throw new Exception("sluithaakje ontbreekt op positie " + cursor);
                    cursor++; 
                    return resultaat;
                }
                else if (cursor < lengte && (inhoud[cursor] == '-' || inhoud[cursor] == '!' || inhoud[cursor]=='~'))
                {
                   //TODO: make sure the parser also recognizes negations
                }

    //the rest of my code

    It would be amazing if some of you knows how.. (it might be easy idk). 

    Kind regards.


    Sunday, January 14, 2018 3:07 PM

All replies

  • Maybe instead of ‘//TODO’ you should write something like this:

       IFormule expr = ParseFactor();
       IFormule resultaat = make negation IFormule on ‘expr’…
       return resultaat;

    Sunday, January 14, 2018 3:17 PM
  • Hello,

     First you want to examine the input syntax of the data, like comma or spaces. If you know

    the exact format of the input and the required element then a parser, regardless of object

    type, should be fairly easy to implement.  This example "-1,1,0" is a string and uses commas.

    Knowing that spaces are usually useless, recommend remove. However, tabs are also used

    to split data elements.

     

     Example Code/Output

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SimpleParser
    {
        class Program
        {
            static void Main( string[] args )
            {
                // Preparse items
                //
                char[] trim = { ' ' };
                char[] separators = { ',' };
    
                // Sample object
                //
                string input = "-1,1,0";
    
                // Display input data
                //
                Console.WriteLine( "Input Data: {0}", input );
    
                // Remove unwanted objects
                //
                input = input.Trim( trim );
    
                // Parse finalize
                //
                string[] data = input.Split( separators );
    
                // Display results
                //
                // To get object type int instead of
                // string use Convert.ToInt32( s )
                //
                foreach ( string s in data )
                {
                    Console.WriteLine( s );
                }
    
                // Wait user input
                //
                Console.ReadLine();
            }
        }
    }

     

     If this is not the desired solution then please provide more details with expected

    input and output examples.

     

     Hope this helps :)

    • Proposed as answer by Fei HuModerator Wednesday, January 31, 2018 2:02 AM
    • Unproposed as answer by Fei HuModerator Wednesday, January 31, 2018 2:02 AM
    Sunday, January 14, 2018 9:23 PM
  • Thanks for replying, though this is not actually what I'm looking for:

    I have to make a parser that recognizes formulas from the propositional logic (e.g. ( (p\/q) /\ -p )). It's manditory that it recognizes negations and variables...

    :)

    Tuesday, January 16, 2018 2:27 PM
  • We do not provide homework solutions in the forums. This benefits no one here or you.  The best we can do is provide you guidance on how to fix your own code and/or point you in the direction of a solution.

    I recommend that read up on how to build a simple scanner. The very first example that pretty much every scanner algorithm gives is for reading numeric values. You can then convert that code to C#.

    As for the parens that you're starting with you have moved into grammars. In this case I'd recommend that you read up on how a recursive descent parser works (the easiest form of a parser). It can quite easily solve this type of problem for you.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 16, 2018 2:43 PM
    Moderator
  • Hello Julian,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 1:12 PM
    Moderator