0

Am new to python and am trying my hands on regex and EBNF. Am not sure if my conversion are accurate. I just need a second look at my conversions.

 EBNF                                      REGEX
 a{a} (convereted from regex)              a+  

 a{{ab} ya}  (convereted from regex)       a((xa)*ya)

 e{e}{bb|p[p] [p]d}c                       e+(bb|(p)*d)?c       
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • what is the exact question ? is expression in column 1 equivalent to column 2 in their repective context ? – NeronLeVelu Jan 21 '15 at 10:22
  • @NeronLeVelu..Yes..for the first two..is the ebnf equivalent to the regex and for the last one is the regex equivalent to the ebnf. An correction would be helpful. Thanks –  Jan 21 '15 at 18:51

1 Answers1

1

While the features of EBNF are straightforward, the actual notation can appear in a few different forms. I'll be using the terminology and notation of ISO/IEC 14977 : 1996(E) (it's available as a PDF if you Google for it).

The regular expression a+ is a Terminal-string (§5.2) followed by a Repeated-sequence (§5.6). The EBNF form would be:

"a", {"a"}

The regular expression a((xa)*ya) is a bit more complicated. If you first look at just a(ya), you have a Terminal-string followed by a Grouped-sequence (§5.4). It would look like this:

"a", ("y", "a")

The subexpression (xa)* by itself is a Repeated-sequence containing a sequence of two Terminal-strings. It alone would look like this:

{"x", "a"}

You can combine the previous two items to obtain the EBNF form for the complete expression:

"a", ({"x", "a"}, "y", "a")

The final expression introduces two additional concepts, specifically the Definitions-list (§5.10) and the Optional-sequence (§5.5). The subexpression (bb|(p)*d) includes a Definitions-list. The EBNF for this subexpression alone is the following:

("b", "b" | {"p"}, "d")

When you include the optional quantifier, which expands this to the subexpression (bb|(p)*d)?, you update the EBNF to the following:

[("b", "b" | {"p"}, "d")]

Since the Optional-sequence also serves as a grouping construct, you can remove the unnecessary start-group-symbol and end-group-symbol to obtain the following:

["b", "b" | {"p"}, "d"]

By combining the steps used above, you can obtain the EBNF form for the complete third regular expression:

"e", {"e"}, ["b", "b" | {"p"}, "d"], "c"
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280