Search code examples
regexformal-languages

how to write an "or" operation while translating Grammar to regex


I want to translate following grammar to regex

A -> bcA | dD |aB
B -> aB |a
D -> dD |d

So here string will end either in a's or d's

Regular expression will start with (bc)+

but can anyone explain how can write d+/a+ in regular expression for languages?


Solution

  • It depends on the regex dialect you're using.

    In general, a | within a () grouping creates an 'or', for example:

    th(er|os)e
    

    ... matches 'there' or 'those'.

    But check the documentation for your dialect, to check whether brackets have to be escaped, whether '|' is supported, and so on.