Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am working on parser phase of compiler for my assignment....
i am checking if the program has strated and ended correctly or not... when i read a file containing the correct syntax it display the message program starting missing.....
can anyone please help me... i will be very thankful...
here is my code......


C#
public bool pgStrtEnd(string str)
       {
           bool p = true;
           if (str.Contains("empty [") && str.Contains("]"))
        // if (str[0].Equals("empty [") && str[str.Length-1].Equals("]"))
           {
              // MessageBox.Show("Valid Program Start and End");
               p = true;
               return p;
           }
          // else // program starting and ending
         //  {
         //  else if (str[str.Length-1].Equals("]"))
           else  if (!str.Contains("]")) //str.Contains("empty [") &&
               {
                   MessageBox.Show("Program Ending Missing");
                   p =false;
                   return p;
               }
          // else if (str[0].Equals("empty [") )
           else if (!str.Contains("empty [")) // && str.Contains("]"))
               {
                   MessageBox.Show("Program Starting Missing");
                   p = false;
                   return p;
               }
           //else if (str.Contains("empty [") && str.Contains("]") && str.Any(char.IsLetterOrDigit))
           //{
           //    MessageBox.Show("Program has some extra after ending");
           //    p = true;
           //    return p;
           //}
               // return p;

           else
           {
               p = false;
               return p;
           }
          // }
        //   return p;
       }
Posted
Comments
ZurdoDev 12-May-14 18:10pm    
What is your question?
BillWoodruff 12-May-14 19:40pm    
When are you going to learn to set break-points in your code, then single-step until you find an error, and determine exactly when errors, or unexpected results, occur ? If you take the trouble to learn/do this ... it's not that difficult ... you can start to post questions here that we can help you with.

From the very beginning: this "parser" is wrong; so I don't want even to get into the detail of your problem.

You are not even close to starting a parser project yet. This is not how parsers work: they should be totally abstracted from any UI. You could collect all the problems in some data collection until it makes sense to continue parsing. You can even stop on the first unrecoverable problem if the input code and stop. But look what you are doing: showing MessageBox of every problem. Just imagine yourself to be a user of such "parser". More importantly, the bigger system hosting your parser, most likely, won't be even aware of the concept of MessageBox or any UI elements at all.

Also, you approach to a parse is totally ad-hoc, not using any regular approach. I can imagine someone doing a parser from scratch (because I did so, as well as many other people), but you don't have sufficient understanding yet, so your attempt already leads you nowhere. For example, you could formalize the notion of BNF syntax definition in your code and translate BNF into the parsing code. Instead, you need to read about the design of parsers (there are different well-known types of them) from some book on parsers. If this is your school assignment, could it be safe to assume that you had some lectures, seminars or learning courses covering this topic? :-)

Also, you could use on of available compier-compilers, but I'm not sure if it can be compatible with your assignment: http://en.wikipedia.org/wiki/Compiler-compiler[^].

See also:
http://en.wikipedia.org/wiki/Parsing[^],
http://en.wikipedia.org/wiki/Comparison_of_parser_generators[^].

—SA
 
Share this answer
 
v3
Comments
DamithSL 12-May-14 23:13pm    
nice! 5d+
Sergey Alexandrovich Kryukov 13-May-14 0:12am    
Thank you very much.
—SA
Maciej Los 24-May-14 15:42pm    
I don't want even to get into the detail of your problem. - You're a Liar, Sergey ;)
This is not how parsers work: they should be totally abstracted from any UI. It denied above sentence.
To test whether a string start with given string and end with another string you could use String.StartsWith Method [^] and String.EndsWith Method[^]
before you call one of above method, you better trim the input string. (spaces at the start and end still valid)
C#
if(str.Trim().StartsWith("[")
{
   // your program start with "["
}

if(str.Trim().StartsWith("]")
{
   // your program start with "]"
}


I'm not going to comment on your parser because SA[^] already gave comprehensive answer.
 
Share this answer
 
v2
_After_ you learned about parser I suggest you to study a very straight Forward implementation: A Calculation Engine for .NET[^]
 
Share this answer
 
Comments
Maciej Los 24-May-14 14:01pm    
Good advice, Bruno!
+5!
[no name] 24-May-14 15:33pm    
Thank you Maciej, Regards, Bruno

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