languages_and_translators/HW1_holaquetal/hola.py

52 lines
603 B
Python
Raw Permalink Normal View History

2019-03-10 19:38:15 -06:00
import ply.lex as lex
import ply.yacc as yacc
tokens = [
'HOLA',
'COMA',
'QUE',
'TAL',
]
t_HOLA = r'hola'
t_COMA = r','
t_QUE = r'que'
t_TAL = r'tal'
t_ignore = r' '
def t_error(t):
print("Illegal character!")
t.lexer.skip(1)
lexer = lex.lex()
def p_phrase(p):
'''
phrase : a QUE TAL
'''
print('✓✓✓ Valid phrase')
def p_a(p):
'''
a : HOLA COMA a
| HOLA
'''
def p_error(p):
print('xxx Invalid phrase')
parser = yacc.yacc()
while True:
try:
s = input('')
except EOFError:
break
parser.parse(s)