RE: Generalized parser without generation

Quinn Tyler Jackson <qjackson@shaw.ca>
4 Feb 2004 21:24:48 -0500

          From comp.compilers

Related articles
Generalized parser without generation moughanj@tcd.ie (2004-02-01)
RE: Generalized parser without generation qjackson@shaw.ca (Quinn Tyler Jackson) (2004-02-04)
RE: Generalized parser without generation qjackson@shaw.ca (Quinn Tyler Jackson) (2004-02-04)
RE: Generalized parser without generation qjackson@shaw.ca (Quinn Tyler Jackson) (2004-02-08)
RE: Generalized parser without generation qjackson@shaw.ca (Quinn Tyler Jackson) (2004-02-08)
| List of all articles for this month |
From: Quinn Tyler Jackson <qjackson@shaw.ca>
Newsgroups: comp.compilers
Date: 4 Feb 2004 21:24:48 -0500
Organization: Compilers Central
References: 04-02-031
Keywords: parse
Posted-Date: 04 Feb 2004 21:24:48 EST

James Moughan asked:


> A friend of mine has been given a final year project for which his
> supervisor wants the above; that is, a program which can read in a
> description of a grammar then parse a file using it directly. Users
> must be able to add new grammars at any time without compiling. He
> has been told that this should be easy to find on the net, but this
> doesn't seem to be the case, and I certainly don't believe it. Does
> anyone know if such a tool exists, and if so, can they point me in the
> right direction?


Meta-S grammars can be loaded at run-time, and even their reduction event
code can be interpreted, rather than fired in compiled code.


If all your friend needs is an LL(k) parser, he could simply avoid using
Meta-S's adaptive features and avoid predicates, in which case he would have
a run-time loadable LL(k) parsing engine.


Because it produces C++ parsers that are written in a highly-portable subset
of the full ANSI/ISO C++, it is likely he could compile the engine on any
number of target platforms. If he is allowed to produce his final project in
a .NET language like C#, Meta-S now also has .NET support.


What follows is an example of a C# host using Meta-S at run-time. LuaTest
includes interpreted reduction event code. StandardTest shows how C# code
can respond to Meta-S parse events in C# code.


Have your friend contact me via email at qjackson@shaw.ca if he would like a
complimentary Research version of Meta-S, and I will be happy to provide him
with it.


--
Quinn Tyler Jackson


http://members.shaw.ca/qjackson/


using System;
using METASCOM;
using MetaSLua;
using System.Runtime.InteropServices;
using System.Reflection;


namespace CSharpExample
{
        [Guid("03911914-DCF0-4caf-A654-5897ED3DC060")]
        public interface ITest
        {
                void S_event();
                void b_event();
        }


        [ClassInterface(ClassInterfaceType.AutoDual)]
        public class CSharpEventHost : ITest, IMetaXParserEventHost
        {
                public CSharpEventHost()
                {
                }


                public Guid ClassGUID
                {
                        get
                        {
                                return new Guid("03911914-DCF0-4caf-A654-5897ED3DC060");
                        }
                }


                public void b_event()
                {
                        Console.Out.Write(String.Format("Standard b_event fired on
\"{0}\".\n", EventInfo.Lexeme));
                }


                public void S_event()
                {
                        Console.Out.Write("Standard S_event fired.\n");
                }


                public IMetaXParserEventInfo EventInfo
                {
                        get
                        {
                                return m_pEI;
                        }


                        set
                        {
                                m_pEI = value;
                        }
                }


                IMetaXParserEventInfo m_pEI = null;
        }


        class ApplicationClass
{
[STAThread]
static void Main(string[] args)
{
                        RegistrationServices rs = new RegistrationServices();
                        rs.RegisterAssembly(Assembly.GetExecutingAssembly(), 0);


                        ApplicationClass app = new ApplicationClass();


                        if(app.LuaDemo())
                        {
                                Console.Out.Write("Lua grammar demo passed.\n\n");
                        }
                        else
                        {
                                Console.Out.Write("Lua grammar demo falsed.\n\n");
                        }


                        if(app.StandardDemo())
                        {
                                Console.Out.Write("Standard grammar demo passed.\n\n");
                        }
                        else
                        {
                                Console.Out.Write("Standard grammar demo failed.\n\n");
                        }


                        rs.UnregisterAssembly(Assembly.GetExecutingAssembly());
}


                public bool LuaDemo()
                {
                        MetaXGrammar gmr = new MetaXGrammar();
                        gmr.LuaCallbacks = new LuaEvents(Console.Out, Console.Error);
                        gmr.SetLuaDebug(true, false, false);


                        gmr.Grammar =
                                "grammar LuaTest host Lua {" +
                                " S ::= a b c;" +
                                " a ::= '[0-9]+';" +
                                " b ::= '[a-z]+';" +
                                " c ::= '[0-9]+';" +


                                " @function_impls = :{ " +
                                " function c_event (N)\n" +
                                " if (N:MATCHED()) then\n" +
                                " print(N:LEXEME())\n" +
                                " end\n"+
                                " end" +
                                " }:;" +
                                "};";


                        if(gmr.IsGood)
                        {
                                Console.Write("Grammar \"" + gmr.GrammarName + "\"
compiled!\n\n");


                                IMetaXScanInfo r = gmr.Scan("12345 abc 67890 123abc456");


                                if(r.MatchLength > 0)
                                {
                                        Console.Write("Lexeme = \"" + r.Lexeme + "\"\n");
                                        Console.Write(gmr.Root.get_ChildByPath("S/b").Lexeme +
"\n");


                                        return true;
                                }
                        }
                        else
                        {
                                Console.Write("Error: Grammar could not be compiled!\n");
                        }


                        return false;
                }


                public bool StandardDemo()
                {
                        MetaXGrammar gmr = new MetaXGrammar();
                        gmr.EventHost = new CSharpEventHost();


                        gmr.Grammar =
                                "grammar StandardTest {" +
                                " S ::= a b c;" +
                                " a ::= '[0-9]+';" +
                                " b ::= '[a-z]+';" +
                                " c ::= '[0-9]+';" +
                                "};";


                        if(gmr.IsGood)
                        {
                                Console.Write("Grammar \"" + gmr.GrammarName + "\"
compiled!\n\n");


                                IMetaXScanInfo r = gmr.Scan("12345 abc 67890 123abc456");


                                if(r.MatchLength > 0)
                                {
                                        Console.Write("Lexeme = \"" + r.Lexeme + "\"\n");
                                        Console.Write(gmr.Root.get_ChildByPath("S/b").Lexeme +
"\n");


                                        return true;
                                }
                        }
                        else
                        {
                                Console.Write("Error: Grammar could not be compiled!\n");
                        }


                        return false;
                }
}
}


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.