Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
peephole
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
peephole
Commits
58e3df8c
Commit
58e3df8c
authored
Nov 30, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added line administration and remove debug print statements.
parent
570b4050
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
40 deletions
+27
-40
src/parser.py
src/parser.py
+27
-40
No files found.
src/parser.py
100755 → 100644
View file @
58e3df8c
#!/usr/bin/python
import
ply.lex
as
lex
import
ply.yacc
as
yacc
# Global lines administration
lines
=
[]
tokens
=
(
'NEWLINE'
,
'WORD'
,
'COMMENT'
,
'DIRECTIVE'
,
'COMMA'
,
'COLON'
)
# Tokens
...
...
@@ -46,11 +48,7 @@ def t_error(t):
lexer = lex.lex()
# Parsing rules
precedence = (
('
nonassoc
', '
COLON
', '
COMMA
'),
('
nonassoc
', '
COMMENT
', '
DIRECTIVE
'),
('
nonassoc
', '
WORD
'),
)
start = '
input
'
def p_input(p):
'''input :
...
...
@@ -63,11 +61,11 @@ def p_line_instruction(p):
def p_line_comment(p):
'
line
:
COMMENT
NEWLINE
'
print '
comment
line
', list(p)[1:]
lines.append(('
comment
', p[1], {'
inline
': False}))
def p_line_inline_comment(p):
'
line
:
instruction
COMMENT
NEWLINE
'
print '
inline
comment
', list(p)[1:]
lines.append(('
comment
', p[2], {'
inline
': True}))
def p_instruction_command(p):
'
instruction
:
command
'
...
...
@@ -75,43 +73,32 @@ def p_instruction_command(p):
def p_instruction_directive(p):
'
instruction
:
DIRECTIVE
'
print '
directive
', list(p)[1:]
lines.append(('
directive
', p[1], {}))
def p_instruction_label(p):
'
instruction
:
WORD
COLON
'
print '
label
', list(p)[1:]
def p_command_3(p):
'
command
:
WORD
WORD
COMMA
WORD
COMMA
WORD
'
print '
command
with
3
args
', list(p)[1:]
lines.append(('
label
', p[1], {}))
def p_command_2(p):
'
command
:
WORD
WORD
COMMA
WORD
'
print '
command
with
2
args
', list(p)[1:]
def p_command_1(p):
'
command
:
WORD
WORD
'
print '
command
with
1
args
', list(p)[1:]
def p_command(p):
'''command : WORD WORD COMMA WORD COMMA WORD
| WORD WORD COMMA WORD
| WORD WORD'''
lines.append(('
command
', p[1], {'
args
': list(p)[2::2]}))
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.parse(open('
hello
.
s
').read())
#lexer.input(open('
hello
.
s
').read())
#
#while True:
# tok = lexer.token()
#
# if not tok:
# break
#
# print tok
#while 1:
#try:
# s = input('
calc
>
') # Use raw_input on Python 2
#except EOFError:
# break
#yacc.parse(s)
def parse_file(filename):
global lines
lines = []
try:
content = open(filename).read()
yacc.parse(content)
except IOError:
print '
File
"%s"
could
not
be
opened
' % filename
return lines
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment