Open-source image
open-source
Hime logo

Table of content

Grammar inheritance

It is possible to make one grammar inherit from one or more grammars. This means that all terminals, variables, and their associated lexical and syntactic rules defined in the inherited grammars will be understood in the inheriting one as if defined in it. This is expressed in the following manner:

grammar MyGrammar : BaseGram1, BaseGram2 {}
  • All lexical rules defining terminals in BaseGram1 and BaseGram2 are understood in MyGrammar as if defined in there. The priority of the lexical rules is then, from the lowest to the highest:
    • Lexical rules in BaseGram1
    • Inline terminals in syntactic rules in BaseGram1
    • Lexical rules in BaseGram2
    • Inline terminals in syntactic rules in BaseGram2
    • Lexical rules in MyGrammar
    • Inline terminals in syntactic rules in MyGrammar
  • All syntactical rules (defining variables) in BaseGram1 and BaseGram2 are understood in MyGrammar as if defined in there.
  • All template syntactical rules (defining variables) in BaseGram1 and BaseGram2 are understood in MyGrammar as if defined in there.

Example:

grammar Commons
{
options { }
terminals
{
IDENTIFIER -> [_a-zA-Z] [_a-zA-Z0-9]* ;
}
grammar { }
}

grammar CSharp : Commons
{
options
rules
{
class -> 'class' IDENTIFIER '</span> <span style={{color:"darkorange"}}>' ;
}
}

grammar Python : Commons
{
options
rules
{
class -> 'class' IDENTIFIER ':' ;
}
}