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
c5798fdd
Commit
c5798fdd
authored
Nov 30, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Translated YACC and LEX to a single PLY file.
parent
f8e59831
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
117 additions
and
0 deletions
+117
-0
src/parser.py
src/parser.py
+117
-0
No files found.
src/parser.py
0 → 100755
View file @
c5798fdd
#!/usr/bin/python
import
ply.lex
as
lex
import
ply.yacc
as
yacc
tokens
=
(
'NEWLINE'
,
'WORD'
,
'COMMENT'
,
'DIRECTIVE'
,
'COMMA'
,
'COLON'
)
# Tokens
def
t_NEWLINE
(
t
):
r'\n+'
t
.
lexer
.
lineno
+=
t
.
value
.
count
(
'
\
n
'
)
return
t
def
t_COLON
(
t
):
r':'
return
t
def
t_COMMA
(
t
):
r','
return
t
def
t_COMMENT
(
t
):
r'\
#.*
'
return t
def t_DIRECTIVE(t):
r'
\
..
*
'
return t
def t_offset_address(t):
r'
[
0
-
9
]
+
(
\
([
a
-
zA
-
Z0
-
9
$
_
.]
+
\
))
?
'
t.type = '
WORD
'
return t
def t_WORD(t):
r'
[
a
-
zA
-
Z0
-
9
$
_
.]
+
'
return t
# Ignore whitespaces
t_ignore = '
\
t
'
def t_error(t):
print('
Illegal
character
"%s"' % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
# Parsing rules
precedence = (
('
nonassoc
', '
COLON
', '
COMMA
'),
('
nonassoc
', '
COMMENT
', '
DIRECTIVE
'),
('
nonassoc
', '
WORD
'),
)
def p_input(p):
'''input :
| input line'''
pass
def p_line_instruction(p):
'
line
:
instruction
NEWLINE
'
pass
def p_line_comment(p):
'
line
:
COMMENT
NEWLINE
'
print '
comment
line
', list(p)[1:]
def p_line_inline_comment(p):
'
line
:
instruction
COMMENT
NEWLINE
'
print '
inline
comment
', list(p)[1:]
def p_instruction_command(p):
'
instruction
:
command
'
pass
def p_instruction_directive(p):
'
instruction
:
DIRECTIVE
'
print '
directive
', list(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:]
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_error(p):
print '
Syntax
error
at
"%s"' % p.value
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)
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