Finished with first homework
This commit is contained in:
parent
ef04c9bb79
commit
23a94196fb
3 changed files with 172 additions and 0 deletions
51
HW1_holaquetal/hola.py
Normal file
51
HW1_holaquetal/hola.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue