Convert EBNF to Scala Parser Combinator rules

Good evening,

I am trying to work on a parser using solely Scala Parser Cobinators i.e. not involving any fastparse, ANTLR, etc.

What would be the best option when converting this EBNF formula :

nec -> ‘[’ subject action expr ‘]’

nec -> ‘[’ variable action expr ‘]’

Thanks a lot for your help :slight_smile:

Does this work:

def nec = '[' ~! (subject | variable) ~! action ~! expr ~! ']'

I will try that.

Thanks a lot for your help.

If I may ask another question please, what type of token would you give that rule, i.e. for example a new Class Neccessity(subject or variable, action, expr) ?

Thanks a lot and good day :slight_smile:

It might be something like

sealed trait NecTarget

object NecTarget {
  case class Sub(value: Int) extends NecTarget
  case class Var(name: String) extends NecTarget

case class Nec(target: NecTarget, action: String, name: String)

…but it really depends on the concrete semantics of the subject/variable/nec concepts.

1 Like

Thanks a lot all for your help. Really appreciate it :slight_smile: