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.  
  14.  
  15.  
  16. %{
  17. /* program to recognize a C program */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20.  
  21. int COMMENT = 0;
  22. %}
  23.  
  24. identifier [a-zA-Z][a-zA-Z0-9]*
  25. %%
  26. #.* { printf("\n%s is a PREPROCESSOR DIRECTIVE", yytext); }
  27. int | float | char | double | while | for | do | if | break | continue | void | switch | case | long | struct | const | typedef | return | else | goto { printf("\n\t%s is a KEYWORD", yytext); }
  28. "/*" { COMMENT = 1; }
  29. "*/" { COMMENT = 0; }
  30. {identifier}\( { if (!COMMENT) printf("\n\nFUNCTION\n\t%s", yytext); }
  31. { { if (!COMMENT) printf("\n BLOCK BEGINS"); } }
  32. } { if (!COMMENT) printf("\n BLOCK ENDS"); }
  33. {identifier}(\[[0-9]*\])? { if (!COMMENT) printf("\n %s IDENTIFIER", yytext); }
  34. \".*\" { if (!COMMENT) printf("\n\t%s is a STRING", yytext); }
  35. [0-9]+ { if (!COMMENT) printf("\n\t%s is a NUMBER", yytext); }
  36. . { if (!COMMENT) printf("\n\t%s is an UNKNOWN CHARACTER", yytext); }
  37. <= | >= | < | == | > { if (!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR", yytext); }
  38. = { if (!COMMENT) printf("\n\t%s is an ASSIGNMENT OPERATOR", yytext); }
  39. %%
  40.  
  41. int main(int argc, char **argv) {
  42. if (argc > 1) {
  43. FILE *file;
  44. file = fopen(argv[1], "r");
  45. if (!file) {
  46. printf("could not open %s \n", argv[1]);
  47. exit(0);
  48. }
  49. yyin = file;
  50. }
  51. yylex();
  52. printf("\n\n");
  53. return 0;
  54. }
  55.  
  56. int yywrap() {
  57. return 1;
  58. }
Success #stdin #stdout #stderr 0.03s 6960KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/W9b4MT/prog:18:1: Syntax error: Operator expected
ERROR: /home/W9b4MT/prog:58:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit