Untitled

 avatar
unknown
plain_text
2 years ago
1.3 kB
5
Indexable
#include <stdio.h>
#include <stdlib.h>
#include "lexer.h"
#include "parser.h"

int main() {
    // Open the source file
    FILE* file = fopen("source.tj", "r");
    if (file == NULL) {
        printf("Failed to open the source file.\n");
        return 1;
    }

    // Determine the size of the file
    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    rewind(file);

    // Allocate memory for the source code
    char* source_code = malloc(file_size + 1);
    if (source_code == NULL) {
        printf("Failed to allocate memory for the source code.\n");
        fclose(file);
        return 1;
    }

    // Read the source code from the file
    size_t read_size = fread(source_code, 1, file_size, file);
    source_code[read_size] = '\0';

    // Close the file
    fclose(file);

    // Tokenize the source code
    Token* tokens = tokenize(source_code);
    if (tokens != NULL) {
        printf("Lexical Analysis:\n");
        printTokens(tokens);

        printf("\nParsing:\n");
        parse(tokens);

        // Clean up tokens
        free(tokens);
    } else {
        printf("Tokenization failed.\n");
    }

    // Clean up memory
    free(source_code);

    return 0;
}
Editor is loading...