'How do I parsing with yacc

Hi i develop a project with python. the name of the project is drawing robot. I use turtle module. For example i entered F 45. firstly i made lexical analysis after parsing finally drawing turtle.forward(45). i made lexical analysis but i didn't make parsing.How do I parsing with yacc.

My lexical code

    import ply.lex as lex
 
 
tokens = (
    'NUMBER',        
    'NAME',
    'LPAREN',
    'RPAREN',
    'FORWARD'        
)         
t_LPAREN  = r'\['
t_RPAREN  = r'\]'
 
def t_NUMBER(t):
    r'\d+'
    t.value = int(t.value)    
    return t
"""def t_NAME(t):
    r'[a-zA-Z_][a-zA-Z_0-9]*'
    t.type="NAME"
    return t"""
    
def t_FORWARD(t):
    r"F"
    t.type="FORWARD"
    return t

def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)
     
t_ignore  = ' \t'
     
def t_error(t):
    print("Illegal character '%s'" % t.value[0])
    t.lexer.skip(1)
   
lexer = lex.lex()

file=open("abc.txt","r")
data=file.readlines()
file.close()
for i in data:
    data2=i

lexer.input(data2)
     
while True:
    tok = lexer.token()
    if not tok: 
        break      
    print(tok)

My parsing code ( I tried to do something but I don't know how to do it didn't work)

    from lexical import tokens
import ply.yacc as yacc

import turtle
ok=turtle.Turtle()


def p_term_forward(p):
    'expression : FORWARD'
    ok.forward(p[0])
    turtle.done()

def p_expression_term(p):
    'expression : term'
    p[0] = p[1]

def p_term_factor(p):
    'term : factor'
    p[0] = p[1]

def p_factor_num(p):
    'factor : NUMBER'
    p[0] = p[1]

def p_factor_expr(p):
    'factor : LPAREN expression RPAREN'
    p[0] = p[2]

# Error rule for syntax errors
def p_error(p):
    print("Syntax error in input!")

# Build the parser
parser = yacc.yacc()

while True:
   try:
       s = input('calc > ')
   except EOFError:
       break
   if not s: continue
   result = parser.parse(s)
   print(result)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source