Question

I'm trying to learn to convert EBNF to C# code.

Sample: 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:

From Ast class

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

    #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?

Was it helpful?

Solution

Why not use a compiler compiler like AntLR? It does it automatically, and faster :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top