I'm trying to define a specific grammar for commands shown down below:
- F n (go forward n units)
- R n (turn right n degrees)
- L n [ … ] (repeat commands inside parenthesis n times)
- COLOR f (f = line color and colors are: K: red, Y: green, M: blue, S: black) (defines color of the line)
- PEN n (n = line thickness) (1: thin, 2: medium, 3: thick) (defines thickness of the line)
Here is an example command:
L 36 [L 4 [F 100 R 90] R 10]
Output:
Here is the grammar I defined:
<start> ::= <function> | <option>
<function> ::= <forward> | <right> | <loop> | <color> | <pen>
<option> ::= <function> <start> | ε
<forward> ::= F <numbers>
<right> ::= R <numbers>
<loop> ::= L <numbers> [ <start> ]
<color> ::= COLOR <colors>
<pen> ::= PEN <numbers>
<colors> ::= M | K | S | Y
I tried to call start non-terminal in _loop function I defined:
def p_loop(p):
'loop : LOOP NUMBER LSQB grammar RSQB'
p[0]=loop_(p[2])
def loop_(x):
i=1
while i<=x:
p_grammar(p)
i=i+1
And it did'nt work.
Then, I tried this:
def p_loop(p):
'loop : LOOP NUMBER LSQB start RSQB '
i=1
while i<=p[2]:
p[0]=p[4]
def p_start(p):
'''start : function
| option'''
p[0]=p[1]
def p_function(p):
'''
function : forward
| right
| loop
| color
| pen
'''
p[0]=p[1]
It didn't work properly too. I cannot find another solution. So, how can I define a function for the L command?
Here are project codes if you want to check.
