lexer.c 831 Bytes
Newer Older
lxq's avatar
lxq committed
1 2 3 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syntax_analyzer.h>
lyz's avatar
lyz committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

///
extern int lines;
extern int pos_start;
extern int pos_end;

///
extern FILE *yyin;
extern char *yytext;
extern int yylex();

// Mac-only hack.
YYSTYPE yylval;

///
int main(int argc, const char **argv) {
lxq's avatar
lxq committed
21 22 23 24
    if (argc != 2) {
        printf("usage: lexer input_file\n");
        return 0;
    }
lyz's avatar
lyz committed
25

lxq's avatar
lxq committed
26 27 28 29 30 31
    const char *input_file = argv[1];
    yyin = fopen(input_file, "r");
    if (!yyin) {
        fprintf(stderr, "cannot open file: %s\n", input_file);
        return 1;
    }
lyz's avatar
lyz committed
32

lxq's avatar
lxq committed
33 34 35 36 37 38 39 40
    int token;
    printf("%5s\t%10s\t%s\t%s\n", "Token", "Text", "Line",
           "Column (Start,End)");
    while ((token = yylex())) {
        printf("%-5d\t%10s\t%d\t(%d,%d)\n", token, yytext, lines, pos_start,
               pos_end);
    }
    return 0;
lyz's avatar
lyz committed
41
}