If the idea is to generate a regex equivalent to this BNF, then it would be something like this:
import re
r = re.compile('^[01]+$')
print(r.match('010100111')) # retorna um objeto Match
print(r.match('010120111')) # None
The regex uses the markers ^
and $
which indicate respectively the start and end of the string.
Then we have the character class [01]
which means "one digit 0
or 1
" and the quantifier +
which indicates one or more occurrences. Thus, regex recognizes a sequence of zeros and ones of minimum size 1 and no maximum limit (exactly the same as BNF represents).
In the example above, we can see that if the string only has zeros and ones, a match, and if you have any different character, you find nothing.
Note: I could even use [0-1]
instead of [01]
, but in this case it is the same ("a digit from 0 to 1" and "the digit 0 or 1" is basically the same thing).
Before the editing and the comments, it was not clear that the goal was to generate the above regex. Anyway, I will keep the version that tried to respond to original question.
If given a BNF, you want to create a sequence that matches the definition, then regex nay is the right tool for the task.
In case, you would have to implement at hand, or use some ready lib. An alternative is to use the NLTK. As I don’t use this kind of thing much, I ended up taking an example from here and adapting to your case:
from nltk import CFG
from nltk.parse.generate import generate
# cria a gramática a partir da BNF
grammar = CFG.fromstring(""" SEQ -> DIG SEQ | DIG
DIG -> "0" | "1" """)
# gera várias "palavras" válidas da gramática
for production in generate(grammar, depth=5):
print(' '.join(production))
In the above case, the exit was:
0 0 0
0 0 1
0 1 0
0 1 1
0 0
0 1
1 0 0
1 0 1
1 1 0
1 1 1
1 0
1 1
0
1
From there you can read the documentation (here and here) to generate exactly what you need.
For the grammar in question generates a sequence potentially infinite of zeros and ones (and not "a sequence from 0 to 2", as originally said), so it is unclear whether you need to generate any sequence whatever is valid, or some specific, or all of certain size, etc.
But the general idea is probably this...
And you are using which string to try to search for occurrences using the regular expression in question?
– Luiz Felipe
The regex would have to be
[0-2]
, but depending on what you need may not be enough. Not to mention that you only created a string but are not actually using regex. If you can [Dit] the question and put more details (for example, which text you want to extract the numbers from, etc)– hkotsubo
I didn’t get to include string, actually I’m very doubtful on this topic in question. If you can help me.
– Camilla Marques
"but I couldn’t get the numerical sequence to show", what sequence?
– Woss
I already changed the question, it would be a sequence of 0 to 2.
– Camilla Marques
You’re still confused. Capturing a sequence of numbers is one thing (it sounds like you’re going to extract it from somewhere else). Displaying the sequence is something else. What exactly the program should do?
– hkotsubo
I changed the question, I need to create a program that displays a numerical sequence according to BNF: <SEQ> ::= <DIG><SEQ> | <DIG> <DIG> ::= 0|1
– Camilla Marques
To know the true functioning of regular expressions Read this.
– Solkarped