Change parser start rule in runtime

Oct 21, 2014 at 6:43 AM
Edited Oct 21, 2014 at 6:45 AM
Hi again!
I need to be able change start parser rule in runtime (without re-generate parser and lexer).
For example, I would like to the follow abilities:
//grammar
{
statement_list -> ... ;
type_declaration -> ... ;
etc.
}

// own C# code
parser.statement_list() // for start parsing with statement_list rule
parser.type_declaration() // for start parsing with type_declaration rule
etc.
Can your HimeParserGenerator provide this functionality?
If yes, could you provide any example for this?
Thanks!
Coordinator
Oct 21, 2014 at 6:59 AM
Hello,

You can't really do this in Hime; but I kind of see where this is coming from. In a recursive decent parser, i.e. all LL parsers (think ANTLR) and most hand-written one you could start by any rule and go down. But this is not really possible with bottom-up methods like LR, the ones we use in Hime. This is mostly due to the structure of the LR automaton. I think tackling this issue would be an open research question.

However, if you are willing to pay some overhead and you know in advance what are the few starting rules you want to have you can kind-of emulate this feature in Hime. You can generate multiple parsers from the same grammar. So let's say in a single file you write something like:
grammar BaseGrammar
{
    options { Separator="MY_SEPARATOR"; }
    terminals { /* my lexical rules here */ }
    rules { /* my rules here*/ }
}

grammar GrammarStartingAtX : BaseGrammar
{
    options { Axiom="X"; }
    terminals { /* left empty*/ }
    rules { /* left empty */ }
}

grammar GrammarStartingAtY : BaseGrammar
{
    options { Axiom="Y"; }
    terminals { /* left empty*/ }
    rules { /* left empty */ }
}
Here you have a base grammar that contain all your lexical and syntactic rules. Then you have several grammars that inherit from the first one and declare different axioms (X and Y in this example). In effect, you can compile separately all the final grammars. For this purpose, you have the -g command line option that let you select the specific grammar you want to compile among all the loaded ones.

I hope this helps!

Laurent
Oct 21, 2014 at 7:14 AM
Thanks for your perfect answer!
To my great shame I did not know about this feature of the LR automate.
I this case I think that your solution would suit me.
Thanks again