Here is a "general" Lindenmayer System generator I wrote in the spirit of code-golf. Please beware that the objective of code-golfing is writing the shorter possible program to fulfill an objective, disregarding good practices, robustness, etc. Stay safe and don't use this style in real life.
f[i_, b_, h_, j_, r_, n_] :=
(a = h; p = j; s = k = {}; t = Flatten;
(Switch[#,
6, s = {a, p, s},
8, {a, p, s} = s,
_C, k = {k, Black, Line@{p, p += {Cos@a, Sin@a}}},
_W, k = {k, White, Line@{p, p += {Cos@a, Sin@a}}}];
If[# < 9, a += I^# b]) & /@ t@Nest[# /. r &, i, n];
Graphics@t@k)
Where
i : Initial state;
b : rotation angle
h : initial angle
j : initial position
r : production rules
n : iterations
And the production rules for the grammar are:
2 = Turn Left (-);
4 = Turn Right (+);
6 = Push and Turn Left ("[");
8 = Pop and Turn Right ("]");
C[i] = Draw (Any number of symbols)
W[i] = Draw in White (Any number of symbols)
Any other symbol = Do Nothing, just use it in producing next state
So the rules for the carpet are:
f[{C[1]}, Pi/2, 0, {0, 0},
{C@1 -> {C@1, 4, C@1, 2, C@1, 2, C@1, 2, W@3, 4, C@1, 4, C@1, 4, C@1, 2, C@1},
W@3 -> {W@3, W@3, W@3}}, 5]
Other usage examples (sorry, couldn't resist)
Examples:
f[{C@1, X}, Pi/2, 0, {0, 0}, {X -> {X, 4, Y, C@1}, Y -> {C@1, X, 2, Y}}, 10]
f[{C@1}, Pi/2, 0, {0,0}, {C@1->{C@1, 2, C@1, 4, C@1, 4, C@1, 2, C@1}}, 6]
f[{C@1}, Pi/4, Pi/2, {0, 0}, {C@2 -> {C@2, C@2}, C@1 -> {C@2, 6, C@1, 8, C@1}}, 10]
f[{C[1]}, Pi/3, 0, {0, 0},
{C@1 -> {C@2, 4, C@1, 4, C@2}, C@2 -> {C@1, 2, C@2, 2, C@1}}, 10]
f[{X},5/36 Pi, Pi/3, {0,0},
{X->{C@1, 4, 6, 6, X, 8, 2, X, 8, 2, C@1, 6, 2, C@1, X, 8, 4, X},
C@1->{C@1, C@1}}, 6]
Edit
Since you wanted a Graph
... here it is:
f[i_, b_, h_, j_, r_, n_] := (a = h; p = j; s = k = {}; g =.;
t = Flatten;
(Switch[#,
6, s = {a, p, s},
8, {a, p, s} = s,
_C, AppendTo[k, {p, (p += {Cos@a, Sin@a})}],
_W, p += {Cos@a, Sin@a}];
If[# < 9, a += I^# b]) & /@ t@Nest[# /. r &, i, n];);
f[{C[1]}, Pi/2, 0, {0, 0},
{C@1 -> {C@1, 4, C@1, 2, C@1, 2, C@1, 2, W@3, 4, C@1, 4, C@1, 4, C@1, 2, C@1},
W@3 -> {W@3, W@3, W@3}}, 2];
g = Graph[Rule @@@ k];
(PropertyValue[{g, #}, VertexCoordinates] = #) & /@ VertexList@g;
g
If you let Mathematica to place the Vertices automagically, you also get nice pictures