GrammarKit explanation?
Ok so, BNF is quite the mystery to me. I'm trying to find information about it, and every time I think I understand, it breaks completely.
I want to create a BNF for something very simple:
//Optional comment
script someName() {
}
I have the following BNF:
{
//configuration for generated class names here
tokens=[
//Tokens
KEY_SCRIPT = "script"
KEY_RETURN = "return"
// literals
l_identifier="regexp:[a-zA-Z_][a-zA-Z_$0-9]*"
l_identifier_text="regexp:\`[^`]+\`"
l_decimal="regexp:[+-]?(([1-9]\d+)|([0-9]))\.\d+"
l_integer="regexp:[+-]?(([1-9]\d+)|([0-9]))"
l_string="regexp:('([^'\\]|\\.)*'|\"([^\"\\]|\\.)*\")"
l_raw_string="regexp:.*"
// base
semicolon=";"
parenthese_open="("
parenthese_close=")"
bracket_CurlyOpen="{"
bracket_CurlyClose="}"
bracket_SquareOpen="["
bracket_SquareClose="]"
]
}
single_comment ::= '//' l_raw_string
script_declaration ::= KEY_SCRIPT l_identifier parenthese_open parenthese_close bracket_CurlyOpen bracket_CurlyClose
cs2 ::= (single_comment) * (script_declaration) (single_comment) * <<eof>>
however, IntelliJ says script_declaration is an unreachable rule and cs2 is an unused rule.
The BNF flavor used in GrammarKit seems to be something different (with less capability?) than most explanations
I can find on the internet. And even those are quite incomplete or unclear.
Can someone explain this and possibly provide me with some resources other than the GrammarKit github, as it leaves too many things unclear.
Please sign in to leave a comment.
You seem to miss the notion of a grammar root rule. It is the first rule in the grammar and it represents a file as a whole, so it's inherently "private". All other rules are to be reachable from the root, e.g.
{ .. attrs and tokens }
root ::= function *
function ::= script id '(' arg_list ')' '{' statement_list '}'
Also do not add comment and whitespace rules, they are skipped by PsiBuilder.
I advise you to play a bit with the sample grammar and see what is generated, etc.
https://github.com/JetBrains/Grammar-Kit/blob/master/TUTORIAL.md