检查程序的语法......开始和结束.... [英] checking syntax of a program ... starting and ending....

查看:111
本文介绍了检查程序的语法......开始和结束....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的任务编写解析器阶段的编译器....

i我正在检查程序是否已经正确并且正确结束...当我读取包含正确语法的文件时它显示消息程序开始缺失.....

任何人都可以帮助我...我会非常感谢...

这里是我的代码.. ....





  public   bool  pgStrtEnd( string  str)
{
bool p = true ;
if (str.Contains( empty [)&& str.Contains( ]))
// if(str [0] .Equals(empty [)&& str [str。长度-1] .Equals(]))
{
// MessageBox.Show(有效程序开始和结束);
p = true ;
return p;
}
// 其他//程序开始和结束
// {
// else if(str [str.Length-1] .Equals(]))
else if (!str.Contains( ] )) // str.Contains(empty [)&&
{
MessageBox.Show( 程序结束缺失);
p = false ;
return p;
}
// 否则if(str [0] .Equals(empty [) )
else if (!str.Contains( empty [)) // && str.Contains(]))
{
MessageBox.Show( 程序开始缺失);
p = false ;
return p;
}
// 否则if(str.Contains(empty [)&& ; str.Contains(])&& str.Any(char.IsLetterOrDigit))
// {
// MessageBox.Show(程序有一些额外的结束后);
// p = true;
// return p;
// }
// return p;

else
{
p = false ;
return p;
}
// }
// return p;
}

解决方案

从一开始:这个解析器是错误的;所以我甚至不想深入了解你的问题的细节。



你还没有接近启动解析器项目。这不是解析器的工作方式:它们应该完全从任何UI中抽象出来。您可以收集某些数据集合中的所有问题,直到继续解析为止。如果输入代码并停止,您甚至可以停止第一个不可恢复的问题。但看看你在做什么:显示每个问题的 MessageBox 。想象一下自己是这种解析器的用户。更重要的是,托管解析器的大型系统很可能甚至不会意识到 MessageBox 的概念或任何UI元素。



此外,您接近解析是完全临时的,不使用任何常规方法。我可以想象有人从头开始做一个解析器(因为我这样做了,还有很多其他人),但你还没有足够的理解,所以你的尝试已经导致你无处可去。例如,您可以在代码中形式化BNF语法定义的概念,并将BNF转换为解析代码。相反,你需要从一些关于解析器的书中阅读解析器的设计(它们有不同的众所周知的类型)。如果这是你的学校作业,你可以安全地假设你有一些讲座,研讨会或学习课程涉及这个主题吗? :-)



另外,你可以使用可用的 compier-compilers ,但我不确定它是否兼容你的任务: http://en.wikipedia.org/wiki/Compiler-compiler [ ^ ]。



另请参阅:

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

http://en.wikipedia.org/wiki/Comparison_of_parser_generators [ ^ ]。



-SA

要测试字符串是否以给定字符串开头并以另一个字符串结尾,您可以使用se String.StartsWith Method [ ^ ]和 String.EndsWith方法 [ ^ ]

,然后再拨打上述其中一个方法,你最好 trim 输入字符串。 (开头和结尾的空格仍然有效)

  if (str.Trim()。StartsWith(   [
{
// 您的程序以[
}开头

if (str.Trim()。StartsWith( ]
{
// 您的程序以]开头
}





我不打算对您的解析器发表评论,因为 SA [ ^ ]已经给出了全面的答案。


_After_你学到了abou解析器我建议你研究一个非常直接的实现: A .NET的计算引擎 [ ^ ]


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......


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;
       }

解决方案

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


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)

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.


_After_ you learned about parser I suggest you to study a very straight Forward implementation: A Calculation Engine for .NET[^]


这篇关于检查程序的语法......开始和结束....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆