Commit 58e3df8c authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added line administration and remove debug print statements.

parent 570b4050
#!/usr/bin/python
import ply.lex as lex import ply.lex as lex
import ply.yacc as yacc import ply.yacc as yacc
# Global lines administration
lines = []
tokens = ('NEWLINE', 'WORD', 'COMMENT', 'DIRECTIVE', 'COMMA', 'COLON') tokens = ('NEWLINE', 'WORD', 'COMMENT', 'DIRECTIVE', 'COMMA', 'COLON')
# Tokens # Tokens
...@@ -46,11 +48,7 @@ def t_error(t): ...@@ -46,11 +48,7 @@ def t_error(t):
lexer = lex.lex() lexer = lex.lex()
# Parsing rules # Parsing rules
precedence = ( start = 'input'
('nonassoc', 'COLON', 'COMMA'),
('nonassoc', 'COMMENT', 'DIRECTIVE'),
('nonassoc', 'WORD'),
)
def p_input(p): def p_input(p):
'''input : '''input :
...@@ -63,11 +61,11 @@ def p_line_instruction(p): ...@@ -63,11 +61,11 @@ def p_line_instruction(p):
def p_line_comment(p): def p_line_comment(p):
'line : COMMENT NEWLINE' 'line : COMMENT NEWLINE'
print 'comment line', list(p)[1:] lines.append(('comment', p[1], {'inline': False}))
def p_line_inline_comment(p): def p_line_inline_comment(p):
'line : instruction COMMENT NEWLINE' 'line : instruction COMMENT NEWLINE'
print 'inline comment', list(p)[1:] lines.append(('comment', p[2], {'inline': True}))
def p_instruction_command(p): def p_instruction_command(p):
'instruction : command' 'instruction : command'
...@@ -75,43 +73,32 @@ def p_instruction_command(p): ...@@ -75,43 +73,32 @@ def p_instruction_command(p):
def p_instruction_directive(p): def p_instruction_directive(p):
'instruction : DIRECTIVE' 'instruction : DIRECTIVE'
print 'directive', list(p)[1:] lines.append(('directive', p[1], {}))
def p_instruction_label(p): def p_instruction_label(p):
'instruction : WORD COLON' 'instruction : WORD COLON'
print 'label', list(p)[1:] lines.append(('label', p[1], {}))
def p_command_3(p):
'command : WORD WORD COMMA WORD COMMA WORD'
print 'command with 3 args', list(p)[1:]
def p_command_2(p): def p_command(p):
'command : WORD WORD COMMA WORD' '''command : WORD WORD COMMA WORD COMMA WORD
print 'command with 2 args', list(p)[1:] | WORD WORD COMMA WORD
| WORD WORD'''
def p_command_1(p): lines.append(('command', p[1], {'args': list(p)[2::2]}))
'command : WORD WORD'
print 'command with 1 args', list(p)[1:]
def p_error(p): def p_error(p):
print 'Syntax error at "%s"' % p.value print 'Syntax error at "%s" on line %d' % (p.value, lexer.lineno)
yacc.yacc() yacc.yacc()
yacc.parse(open('hello.s').read())
def parse_file(filename):
#lexer.input(open('hello.s').read()) global lines
#
#while True: lines = []
# tok = lexer.token()
# try:
# if not tok: content = open(filename).read()
# break yacc.parse(content)
# except IOError:
# print tok print 'File "%s" could not be opened' % filename
#while 1: return lines
#try:
# s = input('calc > ') # Use raw_input on Python 2
#except EOFError:
# break
#yacc.parse(s)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment