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
c3ba2d2d
Commit
c3ba2d2d
authored
Nov 23, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished parser.
parent
655adbfb
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
172 additions
and
29 deletions
+172
-29
.gitignore
.gitignore
+1
-1
src/Makefile
src/Makefile
+12
-9
src/lex.l
src/lex.l
+0
-19
src/peephole.l
src/peephole.l
+24
-0
src/peephole.y
src/peephole.y
+135
-0
No files found.
.gitignore
View file @
c3ba2d2d
...
...
@@ -7,4 +7,4 @@
lex.yy.c
y.tab.c
y.tab.h
p
arser
p
eephole
src/Makefile
View file @
c3ba2d2d
CC
=
gcc
LEX
=
lex
YACC
=
yacc
CFLAGS
=
-ll
parser
:
yacc lex
$(CC)
-c
lex.yy.c y.tab.c
$(CC)
-o
$@
lex.yy.o y.tab.o
$(CFLAGS)
CFLAGS
=
-std
=
c99
-pedantic
-Wall
-D_POSIX_SOURCE
-D_GNU_SOURCE
$(PIC)
-pipe
lex
:
lex.l
$(LEX)
lex.l
all
:
peephole
yacc
:
grammar.y
$(
YACC)
-d
grammar.y
peephole
:
y.tab.o lex.yy.o
$(
CC)
-o
$@
$^
-lm
all
:
parser
y.tab.c y.tab.h
:
peephole.y
$(YACC)
-d
$^
lex.yy.c
:
peephole.l
$(LEX)
$^
clean
:
rm
-f
lex.yy.
*
y.tab.
*
*
.o peephole
src/lex.l
deleted
100644 → 0
View file @
655adbfb
%{
#include <stdio.h>
#include "y.tab.h"
%}
word [a-zA-Z0-9$_]+
int [0-9]+
%%
[\n] { return NL; } /* Newline */
#.* { yylval.sval = yytext; return COMMENT; } /* Comment */
\..* { yylval.sval = yytext; return DIRECTIVE; } /* Assembly directive */
{word}: { yylval.sval = yytext; return LABEL; } /* Label */
{int} { yylval.ival = atoi(yytext); return INT; } /* Integer */
{word} { yylval.sval = yytext; return WORD; } /* Label reference / registry address */
{int}(\({word}\))? { yylval.sval = yytext; return OFFSET; } /* Registry offset address */
[a-z0-9\.]+ { yylval.sval = yytext; return CMD; } /* Command */
[,] { return COMMA; } /* Comma */
[ \t]+ ; /* Ignore whitespace */
src/peephole.l
0 → 100644
View file @
c3ba2d2d
%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
extern int lineno;
%}
%option noyywrap
word [a-zA-Z0-9$_.]+
int [0-9]+
%%
\n { lineno++; printf("ikmatchhemwel!"); return NEWLINE; }
: { return COLON; }
, { return COMMA; }
#.* { yylval.sval = strdup(yytext); return COMMENT; }
\..* { yylval.sval = strdup(yytext); return DIRECTIVE; }
{word} { yylval.sval = strdup(yytext); return WORD; }
{int}(\({word}\))? { yylval.sval = strdup(yytext); return WORD; }
[ \t]+ ;
src/
grammar
.y
→
src/
peephole
.y
View file @
c3ba2d2d
...
...
@@ -4,80 +4,91 @@
// Instruction types
#define TYPE_COMMENT 0
#define TYPE_DIRECTIVE 1
#define TYPE_LABEL 2
#define TYPE_CMD 3
// Argument types
#define ARG_INT 0
#define ARG_REG 1
#define ARG_LABEL 2
#define ARG_OFFSET 3
#define TYPE_INLINE_COMMENT 1
#define TYPE_DIRECTIVE 2
#define TYPE_LABEL 3
#define TYPE_CMD 4
typedef struct line {
// Type:
int type;
c
onst c
har *name;
char *name;
int argc;
char **argv;
int *argt;
struct line *prev;
struct line *next;
} line;
void yyerror(char*);
void add_line(int type, const char *name, int argc, char **argv, int *argt);
void yyerror(char *error);
extern int yylex(void);
void add_line(int type, char *name, int argc, char **argv);
char **malloc_args(int argc);
line *first_line, *last_line;
int lineno = 0;
%}
%union {
int ival;
char *sval;
}
%token NL COMMA
%token <ival> INT
%token <sval> COMMENT DIRECTIVE WORD LABEL OFFSET CMD
%start symbol
%type <sval> command
%token <sval> COMMENT DIRECTIVE LABEL WORD
%token NEWLINE COMMA COLON
%%
command:
"add" WORD COMMA WORD COMMA WORD {
char **argv = (char **)malloc(3 * sizeof(char *));
int *argt = (int *)malloc(3 * sizeof(int));
argv[0] = $2;
argv[1] = $4;
argv[2] = $6;
argt[2] = argt[1] = argt[0] = ARG_REG;
add_line(TYPE_CMD, "add", 3, argv, argt);
}
input:
/* Empty */
| input line
;
symbol:
symbol symbol
| COMMENT { printf("Found comment: %s\n", $1); }
| DIRECTIVE { printf("Found directive: %s\n", $1); }
| WORD { printf("Found word: %s\n", $1); }
| LABEL { printf("Found label: %s\n", $1); }
| INT { printf("Found integer: %d\n", $1); }
| OFFSET { printf("Found offset registry address: %s\n", $1); }
| CMD { printf("Found command: %s\n", $1); }
| COMMA { printf("Found comma\n"); }
| NL { printf("Found newline\n"); }
line:
NEWLINE
| COMMENT NEWLINE { add_line(TYPE_COMMENT, $1, 0, NULL); }
| instruction NEWLINE
| instruction COMMENT NEWLINE { add_line(TYPE_INLINE_COMMENT, $2, 0, NULL); }
;
instruction:
command
| DIRECTIVE { add_line(TYPE_DIRECTIVE, $1, 0, NULL); }
| WORD COLON { add_line(TYPE_LABEL, $1, 0, NULL); }
| error
;
command:
WORD WORD COMMA WORD COMMA WORD {
char **argv = malloc_args(3);
argv[0] = $2;
argv[1] = $4;
argv[2] = $6;
add_line(TYPE_CMD, $1, 3, argv);
}
| WORD WORD COMMA WORD {
char **argv = malloc_args(2);
argv[0] = $2;
argv[1] = $4;
add_line(TYPE_CMD, $1, 2, argv);
}
| WORD WORD {
char **argv = malloc_args(1);
argv[0] = $2;
add_line(TYPE_CMD, $1, 1, argv);
}
;
%%
extern int yylex();
extern int yyparse();
extern FILE *yyin;
main(int argc, const char *argv[])
{
/*int main(void) {
return yyparse();
}*/
int main(int argc, const char *argv[]) {
if( argc < 2 ) {
printf("No file specified");
exit(-1);
...
...
@@ -99,26 +110,26 @@ main(int argc, const char *argv[])
} while( !feof(yyin) );
}
void yyerror(char *s)
{
printf("Error: %s\n", s);
exit(1);
void yyerror(char *error) {
fprintf(stderr, "Error at line %d: %s\n", lineno, error);
}
void add_line(int type, c
onst char *name, int argc, char **argv, int *argt
) {
void add_line(int type, c
har *name, int argc, char **argv
) {
line *l = (line*)malloc(sizeof(line));
printf("
\nName: %s\n\n"
, name);
printf("
%d: %s\n", lineno
, name);
l->argc = argc;
l->argv = argv;
l->argt = argt;
if( last_line == NULL ) {
// First line
first_line = last_line = l;
} else {
// Add line to linked list
l->prev = last_line;
last_line = last_line->next = l;
}
}
char **malloc_args(int argc) {
return (char **)malloc(3 * sizeof(char *));
}
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