[英]Convert EBNF to C# for compiler
我正在尝试学习将EBNF转换为C#代码。
示例: int <ident> = <expr>
我明白这句话:“此数据类型(int)的变量(ident)接受(=)一个整数(expr),但我不明白如何将其转换为:
从Ast班
public class Int : Stmt
{
public string Ident;
public Expr Expr;
}
来自解析器类
#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
有人可以向我指出正确的方向,如何正确地理解如何转换吗?
为什么不使用像AntLR这样的编译器呢? 它会自动执行,并且速度更快:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.