S → Y | 1X
X → 1X | 0
Y → Y 0 | 1X1 | 2X2
I do not understand how to do factoring or substitution when there are more than one of the same symbol. Thanking you.
X → 1 X | 0
refers to a 0-or-more 1
followed by 0
, so it's equivalent to
X → 1* 0
The same approach can be used to remove your left-recursion. You can rewrite
S → Y | 1 X
X → 1 X | 0
Y → Y 0 | 1 X 1 | 2 X 2
as
S → Y | 1 X
X → 1* 0
Y → ( 1 X 1 | 2 X 2 )* 0
In EBNF:
S → Y | 1 X
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
In BNF:
S → Y | 1 X
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
1* → 1 1* | Ɛ
A* → A A* | Ɛ
If all you wanted to do was to eliminated left-recursion, you're done.
If you want to eliminate common prefixes too, you're not done because both sub-rules of S
can start with 1 X
. To fix this, inline and distribute to obtain the following:
S → 0
| 1 X 1 Y
| 2 X 2 Y
| 1 X
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
Now, we're finally in a position to factor out the common 1 X
.
S → 0
| 1 X ( 1 Y )?
| 2 X 2 Y
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
In EBNF:
S → 0 | 1 X B? | 2 X 2 Y
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
B → 1 Y
In BNF:
S → 0 | 1 X B? | 2 X 2 Y
X → 1* 0
Y → A* 0
A → 1 X 1 | 2 X 2
B → 1 Y
B? → B | Ɛ
1* → 1 1* | Ɛ
A* → A A* | Ɛ
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.