2

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?

1 Answers1

1

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

Dave Hillier
  • 18,105
  • 9
  • 43
  • 87
LueTm
  • 2,366
  • 21
  • 31
  • I just checked out the libraries and it looks like its only up to 2.0, is that ok? –  Oct 21 '12 at 09:56
  • Might be: Here someone asked the same question: http://stackoverflow.com/questions/1194584/what-is-a-good-c-sharp-compiler-compiler-parser-generator – LueTm Oct 21 '12 at 09:58
  • Im sticking with Gold. Thx for the input! :) –  Oct 21 '12 at 11:00