我正在尝试学习将EBNF转换为C# 代码。
样本:int <ident> = <expr>
我理解它的说法“这种数据类型(int)的变量(ident)接受(=)一个整数(expr),但我不明白如何将其转换为:
来自 Ast 班
public class Int : Stmt
{
public string Ident;
public Expr Expr;
}
来自 Parser 类
#region INTEGER VARIABLE
else if (this.tokens[this.index].Equals("int"))
{
this.index++;
Int Integer = new Int();
if (this.index < this.tokens.Count &&
this.tokens[this.index] is string)
{
Integer.Ident = (string)this.tokens[this.index];
}
else
{
throw new System.Exception("expected variable name after 'int'");
}
this.index++;
if (this.index == this.tokens.Count ||
this.tokens[this.index] != Scanner.EqualSign)
{
throw new System.Exception("expected = after 'int ident'");
}
this.index++;
Integer.Expr = this.ParseExpr();
result = Integer;
}
#endregion
来自 CodeGen 类
#region INTEGER
else if (stmt is Int)
{
// declare a local
Int integer = (Int)stmt;
this.symbolTable[integer.Ident] = this.il.DeclareLocal(this.TypeOfExpr(integer.Expr));
// set the initial value
Assign assign = new Assign();
assign.Ident = integer.Ident;
assign.Expr = integer.Expr;
this.GenStmt(assign);
}
#endregion
有人可以为我指出正确的方向如何正确理解如何转换吗?