简体   繁体   English

将EBNF转换为C#以进行编译

[英]Convert EBNF to C# for compiler

I'm trying to learn to convert EBNF to C# code. 我正在尝试学习将EBNF转换为C#代码。

Sample: int <ident> = <expr> 示例: int <ident> = <expr>

I understand its saying "The variable (ident) of this data type (int) takes in (=) a whole number (expr), but what I don't understand is how to convert it to this: 我明白这句话:“此数据类型(int)的变量(ident)接受(=)一个整数(expr),但我不明白如何将其转换为:

From Ast class 从Ast班

public class Int : Stmt
{
    public string Ident;
    public Expr Expr;
}

From Parser class 来自解析器类

    #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

From CodeGen class 从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

Can someone point me in the right direction as to how to properly understand how to convert this? 有人可以向我指出正确的方向,如何正确地理解如何转换吗?

Why not use a compiler compiler like AntLR ? 为什么不使用像AntLR这样的编译器呢? It does it automatically, and faster :) 它会自动执行,并且速度更快:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM