Skip to content

Instantly share code, notes, and snippets.

@wgaylord
Created February 8, 2021 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wgaylord/bd12c6a0b57db5b831e8a82205224c39 to your computer and use it in GitHub Desktop.
Save wgaylord/bd12c6a0b57db5b831e8a82205224c39 to your computer and use it in GitHub Desktop.
RPLY program that is giving a weird error. - Has test data at the bottom, Just run the file to test,
PS D:\Compiler\BoatCPU\ASM> ..\..\..\python39\python.exe .\main.py
Traceback (most recent call last):
File "D:\Compiler\BoatCPU\ASM\main.py", line 84, in <module>
print(parser.parse(lexer.lex(q)))
File "D:\python39\lib\site-packages\rply\parser.py", line 22, in parse
current_state = self._reduce_production(
File "D:\python39\lib\site-packages\rply\parser.py", line 80, in _reduce_production
value = p.func(targ)
File "D:\Compiler\BoatCPU\ASM\main.py", line 26, in expressions_expressions_expression
return t.append(p[1])
AttributeError: 'NoneType' object has no attribute 'append'
PS D:\Compiler\BoatCPU\ASM>
from rply import LexerGenerator, ParserGenerator
lg = LexerGenerator()
lg.ignore(r'\s+')
lg.ignore(r'\n+')
lg.add('REGISTER',r'[ABCDEFGHIJKLMNO]')
lg.add('ASSEMBLER_DIRECTIVE',r'org|def')
lg.add('OPCODE',r'nop|loadimm|loadram|loadregister|loadpc|loadstatus|loadstack|storeram|storeregister|storestack|push|pop|or|xor|and|add|sub|not|compare|shiftleft|shiftright|loadimm8|loadram8|loadregister8|loadstatus8|storeram8|storeregister8|push8|pop8|or8|xor8|and8|add8|sub8|not8|compare8|shiftleft8|shiftright8|jump|jumproffset|jumproffset8|jumpoffset|jumprelativeregister|jumprelativeregister8|jumpregister|jumpregisteroffset|branche|branchg|branchl|brancho|intrrupt|intrruptvector|returnint|disableint|storestatus|enableint')
lg.add('NAME',r'[a-zA-Z] [a-zA-Z0-9."]*')
lg.add('NUMBER',r"\b(0x[0-9a-fA-F]+|[0-9]+)\b")
lexer = lg.build()
pg = ParserGenerator(
# A list of all token names, accepted by the parser.
['NUMBER', 'REGISTER', 'OPCODE',
'NAME'
]
)
@pg.production('expressions : expressions expression')
def expressions_expressions_expression(p):
t = p[0]
return t.append(p[1])
@pg.production('expressions : ')
def expressions_expressions_expression(p):
return []
@pg.production('expression : OPCODE REGISTER')
def expression_singleReg(p):
# p is a list of the pieces matched by the right hand side of the
# rule
return p
@pg.production('expression : OPCODE REGISTER REGISTER')
def expression_doubleReg(p):
# p is a list of the pieces matched by the right hand side of the
# rule
return p
@pg.production('expression : OPCODE REGISTER REGISTER REGISTER')
def expression_tripleReg(p):
# p is a list of the pieces matched by the right hand side of the
# rule
return p
@pg.production('expression : OPCODE REGISTER NUMBER')
def expression_regNum(p):
# p is a list of the pieces matched by the right hand side of the
# rule
return p
@pg.production('expression : OPCODE REGISTER NAME')
def expression_regName(p):
# p is a list of the pieces matched by the right hand side of the
# rule
return p
@pg.error
def error_handler(token):
raise ValueError("Ran into a %s where it wasn't expected" % token.gettokentype())
parser = pg.build()
q = """loadimm A 100
loadimm B 100
add A B C
storeram C 2000"""
print(parser.parse(lexer.lex(q)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment