summaryrefslogtreecommitdiffstats
path: root/src/lexer.l
blob: c67052975c69dc7fe1baf0c30e8db9712639ed8a (plain) (blame)
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
%option noyywrap
%{
#include <stdlib.h>
#include <string.h>
#include "symbol-table.h"

#define SIZE_OF_ARR(x) (sizeof(x)/sizeof(x[0]))

static int line_number = 1;

/* You need to define for all tokens in C--, here are some examples */
typedef enum CcmmcToken_enum {
    CCMMC_TOKEN_ID = 1,
    CCMMC_TOKEN_CONST_INT,
    CCMMC_TOKEN_CONST_FLOAT,
    CCMMC_TOKEN_CONST_STRING,
    CCMMC_TOKEN_COMMENT,
    CCMMC_TOKEN_OP_ASSIGN,
    CCMMC_TOKEN_OP_OR,
    CCMMC_TOKEN_OP_AND,
    CCMMC_TOKEN_OP_NOT,
    CCMMC_TOKEN_OP_ADD,
    CCMMC_TOKEN_OP_SUB,
    CCMMC_TOKEN_OP_MUL,
    CCMMC_TOKEN_OP_DIV,
    CCMMC_TOKEN_OP_GT,
    CCMMC_TOKEN_OP_LT,
    CCMMC_TOKEN_OP_GE,
    CCMMC_TOKEN_OP_LE,
    CCMMC_TOKEN_OP_NE,
    CCMMC_TOKEN_OP_EQ,
    CCMMC_TOKEN_DL_LPAREN,
    CCMMC_TOKEN_DL_RPAREN,
    CCMMC_TOKEN_DL_LBRACK,
    CCMMC_TOKEN_DL_RBRACK,
    CCMMC_TOKEN_DL_LBRACE,
    CCMMC_TOKEN_DL_RBRACE,
    CCMMC_TOKEN_DL_COMMA,
    CCMMC_TOKEN_DL_SEMICOL,
    CCMMC_TOKEN_DL_DOT,
    CCMMC_TOKEN_NEWLINE,
    CCMMC_TOKEN_ERROR = 100
} CcmmcToken;
%}

letter          [A-Za-z]
digit           [0-9]
ID              {letter}({letter}|{digit}|"_")*
WS              [ \t]+

/* You need to define the following RE's */
CONST_INT       [+-]?{digit}+
CONST_FLOAT     [+-]?(([0-9]+\.?|[0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?)
                /* {digit}+\.{digit}+ */
CONST_STRING    \"([^\"\n]|(\\.))*\"
COMMENT         \/\*([^*]|\n|(\*+([^*/]|\n)))*\*+\/

/* operators */
OP_ASSIGN       "="
OP_OR           "||"
OP_AND          "&&"
OP_NOT          "!"
OP_ADD          "+"
OP_SUB          "-"
OP_MUL          "*"
OP_DIV          "/"
OP_GT           ">"
OP_LT           "<"
OP_GE           ">="
OP_LE           "<="
OP_NE           "!="
OP_EQ           "=="

NEWLINE         "\n"

/* separators */
DL_LPAREN       "("
DL_RPAREN       ")"
DL_LBRACK       "["
DL_RBRACK       "]"
DL_LBRACE       "{"
DL_RBRACE       "}"
DL_COMMA        ","
DL_SEMICOL      ";"
DL_DOT          "."

ERROR           .

%%

{WS}            {}
{ID}            {
                    int i;
                    char *reserved[] = {"return", "typedef", "if", "else",
                        "int", "float", "for", "void", "while"};

                    for (i = 0; i < SIZE_OF_ARR(reserved); i++)
                        if (strcmp(yytext, reserved[i]) == 0) break;
                    if (i == SIZE_OF_ARR(reserved)) {
                        CcmmcSymbol * ptr;
                        ptr = ccmmc_symbol_table_lookup(yytext);
                        if (ptr == NULL)
                            ccmmc_symbol_table_insert_id(yytext, line_number);
                        else
                            ptr->counter++;
                    }
                }
{CONST_INT}     {}
{CONST_FLOAT}   {}
{CONST_STRING}  {}

{COMMENT}       puts(yytext);
{OP_ASSIGN}     {}
{OP_OR}         {}
{OP_AND}        {}
{OP_NOT}        {}
{OP_ADD}        {}
{OP_SUB}        {}
{OP_MUL}        {}
{OP_DIV}        {}
{OP_GT}         {}
{OP_LT}         {}
{OP_GE}         {}
{OP_LE}         {}
{OP_NE}         {}
{OP_EQ}         {}

{NEWLINE}       line_number++;

{DL_LPAREN}     {}
{DL_RPAREN}     {}
{DL_LBRACK}     {}
{DL_RBRACK}     {}
{DL_LBRACE}     {}
{DL_RBRACE}     {}
{DL_COMMA}      {}
{DL_SEMICOL}    {}
{DL_DOT}        {}

{ERROR}         {
                    fprintf(stderr, "%d: error: undefined character `%s'\n",
                        line_number, yytext);
                    exit(1);
                }


%%

// vim: set sw=4 ts=4 sts=4 et: