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)
|
||||
88
HW1_holaquetal/parser.out
Normal file
88
HW1_holaquetal/parser.out
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
Created by PLY version 3.11 (http://www.dabeaz.com/ply)
|
||||
|
||||
Grammar
|
||||
|
||||
Rule 0 S' -> phrase
|
||||
Rule 1 phrase -> a QUE TAL
|
||||
Rule 2 a -> HOLA COMA a
|
||||
Rule 3 a -> HOLA
|
||||
|
||||
Terminals, with rules where they appear
|
||||
|
||||
COMA : 2
|
||||
HOLA : 2 3
|
||||
QUE : 1
|
||||
TAL : 1
|
||||
error :
|
||||
|
||||
Nonterminals, with rules where they appear
|
||||
|
||||
a : 1 2
|
||||
phrase : 0
|
||||
|
||||
Parsing method: LALR
|
||||
|
||||
state 0
|
||||
|
||||
(0) S' -> . phrase
|
||||
(1) phrase -> . a QUE TAL
|
||||
(2) a -> . HOLA COMA a
|
||||
(3) a -> . HOLA
|
||||
|
||||
HOLA shift and go to state 3
|
||||
|
||||
phrase shift and go to state 1
|
||||
a shift and go to state 2
|
||||
|
||||
state 1
|
||||
|
||||
(0) S' -> phrase .
|
||||
|
||||
|
||||
|
||||
state 2
|
||||
|
||||
(1) phrase -> a . QUE TAL
|
||||
|
||||
QUE shift and go to state 4
|
||||
|
||||
|
||||
state 3
|
||||
|
||||
(2) a -> HOLA . COMA a
|
||||
(3) a -> HOLA .
|
||||
|
||||
COMA shift and go to state 5
|
||||
QUE reduce using rule 3 (a -> HOLA .)
|
||||
|
||||
|
||||
state 4
|
||||
|
||||
(1) phrase -> a QUE . TAL
|
||||
|
||||
TAL shift and go to state 6
|
||||
|
||||
|
||||
state 5
|
||||
|
||||
(2) a -> HOLA COMA . a
|
||||
(2) a -> . HOLA COMA a
|
||||
(3) a -> . HOLA
|
||||
|
||||
HOLA shift and go to state 3
|
||||
|
||||
a shift and go to state 7
|
||||
|
||||
state 6
|
||||
|
||||
(1) phrase -> a QUE TAL .
|
||||
|
||||
$end reduce using rule 1 (phrase -> a QUE TAL .)
|
||||
|
||||
|
||||
state 7
|
||||
|
||||
(2) a -> HOLA COMA a .
|
||||
|
||||
QUE reduce using rule 2 (a -> HOLA COMA a .)
|
||||
|
||||
33
HW1_holaquetal/parsetab.py
Normal file
33
HW1_holaquetal/parsetab.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
|
||||
# parsetab.py
|
||||
# This file is automatically generated. Do not edit.
|
||||
# pylint: disable=W,C,R
|
||||
_tabversion = '3.10'
|
||||
|
||||
_lr_method = 'LALR'
|
||||
|
||||
_lr_signature = 'COMA HOLA QUE TAL\n phrase : a QUE TAL\n \n a : HOLA COMA a\n | HOLA\n '
|
||||
|
||||
_lr_action_items = {'HOLA':([0,5,],[3,3,]),'$end':([1,6,],[0,-1,]),'QUE':([2,3,7,],[4,-3,-2,]),'COMA':([3,],[5,]),'TAL':([4,],[6,]),}
|
||||
|
||||
_lr_action = {}
|
||||
for _k, _v in _lr_action_items.items():
|
||||
for _x,_y in zip(_v[0],_v[1]):
|
||||
if not _x in _lr_action: _lr_action[_x] = {}
|
||||
_lr_action[_x][_k] = _y
|
||||
del _lr_action_items
|
||||
|
||||
_lr_goto_items = {'phrase':([0,],[1,]),'a':([0,5,],[2,7,]),}
|
||||
|
||||
_lr_goto = {}
|
||||
for _k, _v in _lr_goto_items.items():
|
||||
for _x, _y in zip(_v[0], _v[1]):
|
||||
if not _x in _lr_goto: _lr_goto[_x] = {}
|
||||
_lr_goto[_x][_k] = _y
|
||||
del _lr_goto_items
|
||||
_lr_productions = [
|
||||
("S' -> phrase","S'",1,None,None,None),
|
||||
('phrase -> a QUE TAL','phrase',3,'p_phrase','hola.py',28),
|
||||
('a -> HOLA COMA a','a',3,'p_a','hola.py',34),
|
||||
('a -> HOLA','a',1,'p_a','hola.py',35),
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue