[英]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.