开发者

How to write simple parser for if and while statements? [closed]

开发者 https://www.devze.com 2023-04-02 09:45 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I need to write a simple parser that will convert the tokens to parser tree. I've already wrote LexicalAnalyzer that returns the tokens. Now, I want to write rules for "if and while" statements(for the beginning), so I could pass this rules to parser and it will create a tree. So i need to write the parser in the way, so I could write new rules.

Can you advise me开发者_运维百科 how I can implement it in C#? Can you give me some example?


In a recursive descent parser it's easy to implement these statements if you have the normal block and expression parsers. In pseudo-code, they are basically:

void ParseIf()
{
  Match("if");
  Match("(");
  ParseExpression();
  Match(")");
  ParseBlock();
}

and

void ParseWhile()
{
  Parse("while");
  Parse("(");
  ParseExpression();
  Parse(")");
  ParseBlock();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消