summaryrefslogtreecommitdiff
path: root/src/parser.y
blob: 5f7b4bba7cc2bbd47350ff76c98c38207180a169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>

size_t line_number = 1;

extern int yylex();
extern int yyparse();
extern FILE *yyin;
void yyerror(const char *s);
%}

%union {
    int i_val;
    char *s_val;
    char *symbol;
    char *modifier;
    char *directive;
}

%token <i_val>     T_INTEGER
%token <s_val>     T_STRING
%token <symbol>    T_SYMBOL
%token <directive> T_DIRECTIVE
%token <symbol>    T_LABEL
%token <modifier>  T_MODIFIER
%token             T_ENDL
%token             T_COMMA
%token             T_OPENPAREN
%token             T_CLOSEPAREN
%%
asm:
    statements
    ;

statements:
    statements statement
    | statement
    ;

statement:
    label
    | directives
    | instructions
    | T_ENDL
    ;

label:
    T_LABEL T_ENDL
    {
        printf("Found Label: %s\n", $1);
    };

instructions:
    rb_type
    | i_type
    | s_type
    | u_type
    | j_type
    | i_type_modifier
    | s_type_modifier
    | pseudo_type
    ;

rb_type:
    T_SYMBOL T_SYMBOL T_COMMA T_SYMBOL T_COMMA T_SYMBOL T_ENDL
    {
        printf("Read instruction: %s(%s, %s, %s)\n", $1, $2, $4, $6);
    };

i_type:
    T_SYMBOL T_SYMBOL T_COMMA T_SYMBOL T_COMMA T_INTEGER T_ENDL
    {
        printf("Read instruction: %s(%s, %s, %d)\n", $1, $2, $4, $6);
    };

i_type_modifier:
    T_SYMBOL T_SYMBOL T_COMMA T_SYMBOL T_COMMA T_MODIFIER T_OPENPAREN T_SYMBOL T_CLOSEPAREN T_ENDL
    {
        printf("Read instruction: %s(%s, %s, %s of %s)\n", $1, $2, $4, $6, $8);
    };

s_type:
    T_SYMBOL T_SYMBOL T_COMMA T_INTEGER T_OPENPAREN T_SYMBOL T_CLOSEPAREN T_ENDL
    {
        printf("Read instruction: %s(%s, %s + %d)\n", $1, $2, $6, $4);
    };

s_type_modifier:
    T_SYMBOL T_SYMBOL T_COMMA T_MODIFIER T_OPENPAREN T_SYMBOL T_CLOSEPAREN T_ENDL
    {
        printf("Read instruction: %s(%s, %s of %s)\n", $1, $2, $4, $6);
    };

u_type:
    T_SYMBOL T_SYMBOL T_COMMA T_INTEGER T_ENDL
    {
        printf("Read instruction: %s(%s, %d)\n", $1, $2, $4);
    };

j_type:
    T_SYMBOL T_SYMBOL T_COMMA T_SYMBOL T_ENDL
    {
        printf("Read instruction: %s(%s, %s)\n", $1, $2, $4);
    };

pseudo_type:
    T_SYMBOL T_SYMBOL T_ENDL
    {
        printf("Read Pseudo Instruction: %s(%s)\n", $1, $2);
    };

directives:
    directive_int
    | directive_string
    ;

directive_int:
    T_DIRECTIVE T_INTEGER T_ENDL
    {
        printf("Read directive %s %d\n", $1, $2);
    };

directive_string:
    T_DIRECTIVE T_STRING T_ENDL
    {
        printf("Read directive %s %s\n", $1, $2);
    };

%%

void yyerror(const char *s) {
    printf("Parser Error: %lu: %s\n", line_number, s);
}

int main(int argc, char *argv[]) {
    FILE *input = stdin;

    int j = 0;
    while (j != -1) {
        j = getopt(argc, argv, "o:");

        switch (j) {
        case 'o':
            input = fopen(optarg, "r");
            if (input == NULL) {
                perror(argv[0]);
                return -1;
            }
            break;
        default:
            break;
        }
    }

    yyin = input;
    yyparse();
}