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
553b4774
Commit
553b4774
authored
Nov 19, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Started some test work on grammar.
parent
98f930b8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
27 deletions
+83
-27
src/lex.l
src/lex.l
+5
-7
src/lex_out.y
src/lex_out.y
+78
-20
No files found.
src/lex.l
View file @
553b4774
...
...
@@ -2,20 +2,18 @@
#include <stdio.h>
#include "y.tab.h"
%}
reg \$[a-zA-Z0-9]+
label [a-zA-Z0-9_\.]+
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 */
{label}: { yylval.sval = yytext; return LABEL; } /* Label */
{reg} { yylval.sval = yytext; return REG; } /* Registry address */
{word}: { yylval.sval = yytext; return LABEL; } /* Label */
{int} { yylval.ival = atoi(yytext); return INT; } /* Integer */
{
int}(\({reg}\))? { yylval.sval = yytext; return OFFSET; } /* Registry offset
*/
[a-z0-9\.]+ { yylval.sval = yytext; return INSTR; } /* Instruction
*/
{label} { yylval.sval = yytext; return REF; } /* Label reference
*/
{
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/lex_out.y
View file @
553b4774
%{
#include <stdio.h>
#include <stdlib.h>
// Instruction types
#define TYPE_COMMENT 0
#define TYPE_DIRECTIVE 1
#define TYPE_LABEL 2
#define TYPE_CMD 3
// Argument types
#define ARG_REG 0
#define ARG_OFFSET 1
typedef struct line {
int type;
const 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);
line *first_line, *last_line;
%}
%union {
...
...
@@ -11,22 +37,38 @@ void yyerror(char*);
%token NL COMMA
%token <ival> INT
%token <sval> COMMENT DIRECTIVE REG LABEL OFFSET INSTR REF
%token <sval> COMMENT DIRECTIVE WORD LABEL OFFSET CMD
%start symbol
%type <sval> reg3
%%
symb:
symb symb
| COMMENT {printf("Found a comment: %s\n", $1);}
| DIRECTIVE {printf("Found a directive: %s\n", $1);}
| REG {printf("Found a registry address: %s\n", $1);}
| LABEL {printf("Found a label: %s\n", $1);}
| REF {printf("Found a label reference: %s\n", $1);}
| INT {printf("Found an integer: %d\n", $1);}
| OFFSET {printf("Found an offset registry address: %s\n", $1);}
| INSTR {printf("Found an instruction: %s\n", $1);}
| COMMA {printf("Found a comma\n");}
| NL {printf("Found a newline\n");}
reg3: "add" | "sub"
command:
reg3 WORD WORD WORD {
char *argv[] = (char **)malloc(3 * sizeof(char *));
char *argt[] = (int *)malloc(3 * sizeof(int));
argv[0] = $2;
argv[1] = $3;
argv[2] = $4;
argt[2] = argt[1] = argt[0] = ARG_REG;
add_line(TYPE_CMD, (char *)$1, 3, argv, argt);
}
;
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"); }
;
%%
...
...
@@ -34,10 +76,9 @@ extern int yylex();
extern int yyparse();
extern FILE *yyin;
main(int argc, const char
*
argv[])
main(int argc, const char
*
argv[])
{
if (argc < 2)
{
if( argc < 2 ) {
printf("No file specified");
exit(-1);
}
...
...
@@ -45,7 +86,7 @@ main(int argc, const char* argv[])
// open a file handle to a particular file:
FILE *myfile = fopen(argv[1], "r");
// make sure it is valid:
if
(!myfile
) {
if
( !myfile
) {
printf("Cannot open %s\n", argv[1]);
return -1;
}
...
...
@@ -55,11 +96,28 @@ main(int argc, const char* argv[])
// parse through the input until there is no more:
do {
yyparse();
} while
(!feof(yyin)
);
} while
( !feof(yyin)
);
}
void yyerror(char *s)
{
printf("Found error: %s\n", s);
exit(-1);
printf("Error: %s\n", s);
exit(1);
}
void add_line(int type, const char *name, int argc, char **argv, int *argt) {
line *l = (line*)malloc(sizeof(line));
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;
}
}
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