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
655adbfb
Commit
655adbfb
authored
Nov 19, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Started work on the grammar generator.
parent
6a05c158
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
0 deletions
+37
-0
src/generate_grammar.py
src/generate_grammar.py
+37
-0
No files found.
src/generate_grammar.py
0 → 100755
View file @
655adbfb
#!/usr/bin/python
argmap
=
{
'REG'
:
'WORD'
}
def
command
(
name
,
args
):
"""Generate grammar code to be pasted in grammar.y for a sinle MIPS
command."""
argc
=
len
(
args
)
mapped_args
=
[
argmap
[
arg
]
if
arg
in
argmap
else
arg
for
arg
in
args
]
code
=
'"%s" %s {
\
n
'
%
(
name
,
' COMMA '
.
join
(
mapped_args
))
code
+=
'
\
t
\
t
char **argv = (char **)malloc(%d * sizeof(char *));
\
n
'
%
argc
code
+=
'
\
t
\
t
int *argt = (int *)malloc(%d * sizeof(int));
\
n
'
%
argc
for
i
,
argtype
in
enumerate
(
args
):
code
+=
'
\
t
\
t
argv[%d] = $%d;
\
n
'
%
(
i
,
i
*
2
+
2
)
code
+=
'
\
t
\
t
argt[%d] = ARG_%s;
\
n
'
%
(
i
,
argtype
)
code
+=
'
\
t
\
t
add_line(TYPE_CMD, %s, %d, argv, argt);
\
n
'
%
(
name
,
argc
)
code
+=
'
\
t
}
\
n
'
return
code
# Define commands
commands
=
[
\
([
'add'
,
'sub'
],
[
'REG'
]
*
3
),
\
([
'addi'
,
'subi'
],
[
'REG'
,
'REG'
,
'INT'
]),
\
]
# Generate 'command' grammar
cmd
=
[]
for
names
,
args
in
commands
:
for
name
in
names
:
cmd
.
append
(
command
(
name
,
args
))
code
=
'commands:
\
n
\
t
%s
\
t
;
\
n
'
%
'
\
t
| '
.
join
(
cmd
)
print
code
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