summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 2fd1c2cc876bfdfdfd6b82a4248fc8602714ea9c (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
#include <stdio.h>
#include <stdlib.h>
#include "lexer.h"
#include "symbol-table.h"

static int id_compare(const void *a, const void *b) {
    const symtab *aa = *(const symtab**)a;
    const symtab *bb = *(const symtab**)b;
    return strcmp(aa->lexeme, bb->lexeme);
}

int main(int argc, char **argv) {
    if (argc > 1)
        yyin = fopen(argv[1], "r");
    else
        yyin = stdin;
    yylex();

    int len;
    symtab **id_list = fillTab(&len);
    qsort(id_list, len, sizeof(symtab*), id_compare);

    puts("Frequency of identifiers:");
    for (int i = 0; i < len; i++) {
        printf("%-16s%d\n", id_list[i]->lexeme, id_list[i]->counter);
    }

    return 0;
}

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