Пример #1
0
        /// <summary>
        /// Parse given input string in the concrete grammar.
        /// </summary>
        /// <param name="str">Input string to be parsed.</param>
        /// <param name="cat">Category (Type) to parse.</param>
        /// <param name="heuristics">Heuristic (see the GF C runtime docs).</param>
        /// <param name="Callback1">Callback function.</param>
        /// <param name="Callback2">Callback function.</param>
        /// <returns>Enumerates pairs of (abstract grammar) expressions and corresponding probability.</returns>
        public IEnumerable <Tuple <Expression, float> > Parse(string str, Type cat = null, double?heuristics = null,
                                                              Action Callback1     = null, Action Callback2  = null)
        {
            var parse_pool = new NativeGU.NativeMemoryPool();
            var exn        = new NativeGU.NativeExceptionContext(parse_pool);

            cat = cat ?? Grammar.StartCat;

            using (var nativeStr = new Native.NativeString(str))
            {
                var result_pool = new NativeGU.NativeMemoryPool();
                var callbackMap = Native.pgf_new_callbacks_map(this.Ptr, parse_pool.Ptr);

                var iterator = Native.pgf_parse_with_heuristics(this.Ptr, cat.Ptr,
                                                                nativeStr.Ptr, heuristics ?? -1, callbackMap,
                                                                exn.Ptr, parse_pool.Ptr, result_pool.Ptr);

                if (iterator == IntPtr.Zero || exn.IsRaised)
                {
                    throw new PGF.Exceptions.ParseErrorException();
                }
                else
                {
                    foreach (var ptr in NativeGU.IteratorToIEnumerable(iterator, parse_pool.Ptr))
                    {
                        var exprProb = (Native.PgfExprProb)Marshal.PtrToStructure(ptr, typeof(Native.PgfExprProb));
                        yield return(Tuple.Create(Expression.FromPtr(exprProb.expr, result_pool),
                                                  exprProb.prob));
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Get the bracketed (structured) linearization of an expression.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public Bracket BracketedLinearize(Expression e)
        {
            var tmp_pool = new NativeGU.NativeMemoryPool();
            var exn      = new NativeGU.NativeExceptionContext(tmp_pool);

            var cts = Native.pgf_lzr_concretize(Ptr, e.Ptr, exn.Ptr, tmp_pool.Ptr);

            var ctree = IntPtr.Zero;

            NativeGU.gu_enum_next(cts, ref ctree, tmp_pool.Ptr);

            if (ctree == IntPtr.Zero)
            {
                return(null);
            }

            ctree = Native.pgf_lzr_wrap_linref(ctree, tmp_pool.Ptr);

            var builder = new Bracket.BracketBuilder();

            var mem = Marshal.AllocHGlobal(Marshal.SizeOf <Native.PgfLinFuncs>());

            Marshal.StructureToPtr <Native.PgfLinFuncs>(builder.LinFuncs, mem, false);

            Native.pgf_lzr_linearize(Ptr, ctree, 0, ref mem, tmp_pool.Ptr);

            var b = builder.Build();

            Marshal.FreeHGlobal(mem);

            return(b);
        }
Пример #3
0
        /// <summary>
        /// Read grammar from PGF file.
        /// </summary>
        /// <param name="fn">filename</param>
        /// <returns></returns>
        public static Grammar FromFile(string fn)
        {
            var obj = new Grammar();
            var exn = new NativeGU.NativeExceptionContext(new NativeGU.NativeMemoryPool());

            obj.pool = new NativeGU.NativeMemoryPool();
            obj._ptr = Native.pgf_read(fn, obj.pool.Ptr, exn.Ptr);
            if (exn.IsRaised)
            {
                throw new PGF.Exceptions.PGFException($"Could not read PGF from file {fn}. ({System.IO.Directory.GetCurrentDirectory()})");
            }
            return(obj);
        }
Пример #4
0
        /// <summary>
        /// Get all possible linearization for an expression (see <see cref="Linearize(Expression)"/>).
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public IEnumerable <string> LinearizeAll(Expression e)
        {
            var tmp_pool = new NativeGU.NativeMemoryPool();
            var exn      = new NativeGU.NativeExceptionContext(tmp_pool);

            var cts = Native.pgf_lzr_concretize(Ptr, e.Ptr, exn.Ptr, tmp_pool.Ptr);

            if (exn.IsRaised || cts == IntPtr.Zero)
            {
                throw new PGF.Exceptions.PGFException("Could not linearize the abstract tree.");
            }

            return(NativeGU.IteratorToIEnumerable(cts, tmp_pool.Ptr).Select(LinearizeCnc));
        }
Пример #5
0
        /// <summary>
        /// Enumerate all expressions in the given category.
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        public IEnumerable <Expression> GenerateAll(Type cat = null)
        {
            cat = cat ?? StartCat;
            var    tmp_pool    = new NativeGU.NativeMemoryPool();
            var    exn         = new NativeGU.NativeExceptionContext(tmp_pool);
            var    result_pool = new NativeGU.NativeMemoryPool();
            IntPtr ptr         = IntPtr.Zero;
            var    iterator    = Native.pgf_generate_all(this._ptr, cat.Ptr, exn.Ptr, tmp_pool.Ptr, result_pool.Ptr);

            return(NativeGU.IteratorToIEnumerable(iterator, tmp_pool.Ptr).Select(p =>
            {
                var exprProb = Marshal.PtrToStructure <Native.PgfExprProb>(ptr);
                return Expression.FromPtr(exprProb.expr, result_pool);
            }));
        }
Пример #6
0
        /// <summary>
        /// Reduce expression.
        /// </summary>
        /// <param name="expr"></param>
        /// <returns></returns>
        public Expression Compute(Expression expr)
        {
            var tmp_pool    = new NativeGU.NativeMemoryPool();
            var exn         = new NativeGU.NativeExceptionContext(tmp_pool);
            var result_pool = new NativeGU.NativeMemoryPool();
            var newExpr     = Native.pgf_compute(_ptr, expr.Ptr, exn.Ptr, pool.Ptr, result_pool.Ptr);

            if (exn.IsRaised || newExpr == IntPtr.Zero)
            {
                throw new PGF.Exceptions.PGFException("Could not reduce expression.");
            }
            else
            {
                return(Expression.FromPtr(newExpr, result_pool));
            }
        }
Пример #7
0
        /*
         * [StructLayout(LayoutKind.Sequential)]
         * public struct PGFClosure
         * {
         *  public GuMapItor fn;
         *  public IntPtr grammar;
         *  public IntPtr obj;
         * }*/

        public static string ReadString(Action <IntPtr, NativeGU.NativeExceptionContext> f)
        {
            var pool   = new NativeGU.NativeMemoryPool();
            var exn    = new NativeGU.NativeExceptionContext(pool);
            var sbuf   = NativeGU.gu_new_string_buf(pool.Ptr);
            var output = NativeGU.gu_string_buf_out(sbuf);

            f(output, exn);
            if (exn.IsRaised)
            {
                throw new Exception();
            }
            var strPtr = NativeGU.gu_string_buf_freeze(sbuf, pool.Ptr);
            var str    = Native.NativeString.StringFromNativeUtf8(strPtr);

            return(str);
        }
Пример #8
0
        public static void MapIter(MapIterFunc iter, IntPtr _pgf, Action <string, IntPtr> action)
        {
            var pool = new NativeGU.NativeMemoryPool();
            var exn  = new NativeGU.NativeExceptionContext(pool);
            var f    = new GuMapItor()
            {
                fn = (self, key, value, _err) =>
                {
                    action(Native.NativeString.StringFromNativeUtf8(key), value);
                    if (exn.IsRaised)
                    {
                        throw new Exception();
                    }
                }
            };

            iter(_pgf, ref f, exn.Ptr);
        }
Пример #9
0
        /// <summary>
        /// Linearize expression, i.e. produce a string in the concrete grammar from an expression in the abstract grammar.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public string Linearize(Expression e)
        {
            var tmp_pool = new NativeGU.NativeMemoryPool();
            var exn      = new NativeGU.NativeExceptionContext(tmp_pool);

            var buf  = NativeGU.gu_new_string_buf(tmp_pool.Ptr);
            var out_ = NativeGU.gu_string_buf_out(buf);

            Native.pgf_linearize(Ptr, e.Ptr, out_, exn.Ptr);
            if (exn.IsRaised)
            {
                throw new PGF.Exceptions.PGFException();
            }
            else
            {
                var cstr = NativeGU.gu_string_buf_freeze(buf, tmp_pool.Ptr);
                return(Native.NativeString.StringFromNativeUtf8(cstr));
            }
        }
Пример #10
0
        private T Read <T>(Func <IntPtr, IntPtr, IntPtr, IntPtr> reader, Func <IntPtr, NativeGU.NativeMemoryPool, T> factory, string str)
        {
            var tmp_pool    = new NativeGU.NativeMemoryPool();
            var exn         = new NativeGU.NativeExceptionContext(tmp_pool);
            var result_pool = new NativeGU.NativeMemoryPool();

            using (var strNative = new Native.NativeString(str))
            {
                var in_  = NativeGU.gu_data_in(strNative.Ptr, strNative.Size, tmp_pool.Ptr);
                var expr = reader(in_, result_pool.Ptr, exn.Ptr);
                if (exn.IsRaised || expr == IntPtr.Zero)
                {
                    throw new PGF.Exceptions.ParseErrorException();
                }
                else
                {
                    return(factory(expr, result_pool));
                }
            }
        }
Пример #11
0
        private string LinearizeCnc(IntPtr cnc)
        {
            var tmp_pool = new NativeGU.NativeMemoryPool();
            var exn      = new NativeGU.NativeExceptionContext(tmp_pool);

            var sbuf = NativeGU.gu_new_string_buf(tmp_pool.Ptr);
            var out_ = NativeGU.gu_string_buf_out(sbuf);

            var wrapped = Native.pgf_lzr_wrap_linref(cnc, tmp_pool.Ptr);

            Native.pgf_lzr_linearize_simple(Ptr, wrapped, 0, out_, exn.Ptr, tmp_pool.Ptr);

            if (exn.IsRaised)
            {
                throw new PGF.Exceptions.PGFException("Could not linearize abstract tree.");
            }

            var cstr = NativeGU.gu_string_buf_freeze(sbuf, tmp_pool.Ptr);

            return(Native.NativeString.StringFromNativeUtf8(cstr));
        }