1

I am trying to convert an EBNF file into working BNF for IntelliJ Grammar-kit.

In the EBNF there are rules as such:

BinOpChar ::= "~" | "!" | "@" | "#" | "$" | "%" | "^" | "&" | "*" | "-"
BinOp ::= BinOpChar, {BinOpChar}

How can I create such rules without resorting to regex? Reason being is that this kind of construct happens very often and it becomes repetitive to do in regex.

To be clear, I would like to be able to create a rule to match @@ from BinOpChar but don't match @ @. Is that possible?

Johnny Everson
  • 8,343
  • 7
  • 39
  • 75

1 Answers1

1

The easiest way is to list each operator independently:

{
  tokens=[
    //...
    op_1='~'
    op_2='!'
    op_3='@'
    op_4='@@'
    op_5='#'
    //...
  ]
}

If you really want to accept all n + n^2 tokens, you will need to use a regular expression:

{
  tokens=[
    //...
    bin_op:'regexp:[~!@#]{1,2}'
    //...
  ]
}

But the idea is, you want to use the lexer to define tokens, and the grammar to define expressions and so forth. So in the grammar if you write:

{
  tokens=[
    space='regexp:\s+'
  ]
}

BinOp ::= BinOpChar [BinOpChar]
BinOpChar ::= "~" | "!" | "@" | "#"

Then it's going to accept @@ and @ @. Does that make sense?

breandan
  • 1,965
  • 26
  • 45
  • 1
    Weird, the last example you posted didn't work on Live Preview, I get: `'~', '!', '@' or '#' expected, got '@@'`. Does intellij live preview work? – Johnny Everson Jan 20 '16 at 01:12
  • Sorry, the last example was a little confusing (the first expansion should be the root). Live Preview should now work verbatim in a `.bnf` file. – breandan Jan 20 '16 at 01:23
  • Thanks! '@ @' is not accepted for what I need, but mixing Tokens from regex seems to work, so I will mix from your suggestions. I really appreciate your help – Johnny Everson Jan 20 '16 at 03:49