cenotelie / hime

Apache License 2.0
27 stars 4 forks source link

Probably undefined variable `root` in net-kickstart's example? #68

Closed aleksmelnikov closed 4 years ago

aleksmelnikov commented 4 years ago

https://cenotelie.fr/projects/hime/tutorials/net-kickstart.html Step 7 Set the minimal Program.cs:

using System;
using Hime.Redist; // the namespace for the Hime Runtime
using MathExp; // default namespace for the parser is the grammar's name

namespace TestHime
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Creates the lexer and parser
            MathExpLexer lexer = new MathExpLexer("2 + 3");
            MathExpParser parser = new MathExpParser(lexer);
            // Executes the parsing
            ParseResult result = parser.Parse();
            // Prints the produced syntax tree
            Print(root, new bool[] {});      <---- root is called but not defined ?
        }

        private static void Print(ASTNode node, bool[] crossings)
        {
            for (int i = 0; i < crossings.Length - 1; i++)
                Console.Write(crossings[i] ? "|   " : "    ");
            if (crossings.Length > 0)
                Console.Write("+-> ");
            Console.WriteLine(node.ToString());
            for (int i = 0; i != node.Children.Count; i++)
            {
                bool[] childCrossings = new bool[crossings.Length + 1];
                Array.Copy(crossings, childCrossings, crossings.Length);
                childCrossings[childCrossings.Length - 1] = (i < node.Children.Count - 1);
                Print(node.Children[i], childCrossings);
            }
        }
    }
}

Step 8 Build the test project:

$ dotnet restore
$ dotnet build
Program.cs(17,19): error CS0103: The name 'root' does not exist in the current context [/at/hime/test/test.csproj]
aleksmelnikov commented 4 years ago

Maybe the Program.cs should be:

using System;
using Hime.Redist; // the namespace for the Hime Runtime
using MathExp; // default namespace for the parser is the grammar's name

namespace TestHime
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Creates the lexer and parser
            MathExpLexer lexer = new MathExpLexer("2 + 3");
            MathExpParser parser = new MathExpParser(lexer);
            // Executes the parsing
            ParseResult result = parser.Parse();
            // Prints the produced syntax tree
            ASTNode root = result.Root;                  <-------------------
            Print(root, new bool[] {});
        }

        private static void Print(ASTNode node, bool[] crossings)
        {
            for (int i = 0; i < crossings.Length - 1; i++)
                Console.Write(crossings[i] ? "|   " : "    ");
            if (crossings.Length > 0)
                Console.Write("+-> ");
            Console.WriteLine(node.ToString());
            for (int i = 0; i != node.Children.Count; i++)
            {
                bool[] childCrossings = new bool[crossings.Length + 1];
                Array.Copy(crossings, childCrossings, crossings.Length);
                childCrossings[childCrossings.Length - 1] = (i < node.Children.Count - 1);
                Print(node.Children[i], childCrossings);
            }
        }
    }
}
woutersl commented 4 years ago

Good catch, thanks for reporting this. Interestingly, the longer tutorial looks fine. This should be fixed now.

aleksmelnikov commented 4 years ago

Yes, with this patch it works fine:

$ dotnet bin/Debug/netcoreapp3.0/TestHime.dll
exp
+-> exp_term
    +-> exp_term
    |   +-> exp_factor
    |       +-> exp_atom
    |           +-> NUMBER = 2
    +-> + = +
    +-> exp_factor
        +-> exp_atom
            +-> NUMBER = 3