fork download
  1. lex_by_file( FileName ) :-
  2. get_input_from_file( FileName, TokenList ),
  3. lex( TokenList, OutputList ),
  4. %write_output( OutputList ), !.
  5. maplist(writeln,OutputList),!.
  6.  
  7. %% atom_number(atom, number)
  8. lex([],[]).
  9. lex(['int' | T], ['TYPE: int' | R]) :- lex(T, R).
  10. lex(['bool' | T], ['TYPE: bool'] | R) :- lex(T, R).
  11. lex([',' | T], ['COMMA: ,'] | R) :- lex(T, R).
  12. lex([X | T], [X | R]) :- lex(T, R).
  13.  
Success #stdin #stdout #stderr 0.05s 6940KB
stdin
/* Definition Section */
%{
#include <stdio.h>

// Global counters, initialized to zero
int vowels = 0;
int consonants = 0;
int operators = 0;
int commas = 0;
int semicolons = 0;
int newlines = 0;
%}

/* Rules Section */
%%
[aeiouAEIOU]    { vowels++; }
[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] { consonants++; }
[+\-*/%<>=!&|~^] { operators++; }
","             { commas++; }
";"             { semicolons++; }
"\n"            { newlines++; }
.               { /* This rule matches and ignores all other characters */ }
%%

/* User Code Section */
int yywrap(void) {
    // This function is called at the end of the file.
    // Returning 1 signals that there are no more files to process.
    return 1;
}

int main(int argc, char *argv[]) {
    // If a filename is provided as an argument, open it.
    if (argc > 1) {
        FILE *file = fopen(argv[1], "r");
        if (!file) {
            perror("Error opening file");
            return 1;
        }
        // Set the input stream for the lexer
        yyin = file;
    } 
    // Otherwise, the lexer will read from standard input by default.

    // Start the analysis
    yylex();

    // Print the final report
    printf("\n------------------------------------\n");
    printf("        ANALYSIS REPORT          \n");
    printf("------------------------------------\n");
    printf(" Vowels      : %d\n", vowels);
    printf(" Consonants  : %d\n", consonants);
    printf(" Operators   : %d\n", operators);
    printf(" Commas      : %d\n", commas);
    printf(" Semicolons  : %d\n", semicolons);
    printf(" New Lines   : %d\n", newlines);
    printf("------------------------------------\n");

    return 0;
}
stdout
Standard output is empty
stderr
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: No previous search
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ?    Call: (3) program ? ERROR: Unknown option (h for help)
   Call: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ?    Exception: (3) program ?    Call: (3) program ? ERROR: Unknown option (h for help)
   Call: (3) program ? [Illegal port specification]
   Call: (3) program ? ERROR: Unknown option (h for help)
   Call: (3) program ? ERROR: Unknown option (h for help)
   Call: (3) program ? [Illegal port specification]
   Call: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ?    Exception: (3) program ?    Exception: (3) program ?    Exception: (3) program ?    Exception: (3) program ?    Exception: (3) program ?    Exception: (3) program ?    Exception: (3) program ?    Exception: (3) program ?    Exception: (3) program ?    Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ?    Call: (3) program ? ERROR: Unknown option (h for help)
   Call: (3) program ? EOF: exit