Advertisement
Guest User

lexer

a guest
Mar 18th, 2018
1,602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. %option noyywrap
  2.  
  3. %{
  4.     #include <stdio.h>
  5.     #include <stdlib.h>
  6.     #include <string.h>
  7.     #include "symtab.c"
  8.    
  9.     extern FILE *yyin;
  10.     extern FILE *yyout;
  11.    
  12.     int lineno = 1; // initialize to 1
  13.     void ret_print(char *token_type);
  14.     void yyerror();
  15. %}
  16.  
  17. %x ML_COMMENT
  18.  
  19. alpha       [a-zA-Z]
  20. digit       [0-9]
  21. alnum       {alpha}|{digit}
  22. print       [ -~]
  23.  
  24. ID          {alpha}+{alnum}*
  25. ICONST      "0"|[0-9]{digit}*
  26. FCONST      "0"|{digit}*"."{digit}+
  27. CCONST      (\'{print}\')|(\'\\[nftrbv]\')
  28. STRING      \"{print}*\"
  29.  
  30. %%
  31.  
  32. "//".*                  { printf("Eat up comment at line %d\n", lineno); }
  33.  
  34. "/*"                    { printf("Eat up comment from line %d ", lineno); BEGIN(ML_COMMENT); }
  35. <ML_COMMENT>"*/"        { printf("to line %d\n", lineno); BEGIN(INITIAL); }
  36. <ML_COMMENT>[^*\n]+    
  37. <ML_COMMENT>"*"        
  38. <ML_COMMENT>"\n"        { lineno += 1; }
  39.  
  40.  
  41. "char"|"CHAR"           { ret_print("KEYWORD_CHAR"); }
  42. "int"|"INT"             { ret_print("KEYWORD_INT"); }
  43. "float"|"FLOAT"         { ret_print("KEYWORD_FLOAT"); }
  44. "double"|"DOUBLE"       { ret_print("KEYWORD_DOUBLE"); }
  45. "if"|"IF"               { ret_print("KEYWORD_IF"); }
  46. "else"|"ELSE"           { ret_print("KEYWORD_ELSE"); }
  47. "while"|"WHILE"         { ret_print("KEYWORD_WHILE"); }
  48. "for"|"FOR"             { ret_print("KEYWORD_FOR"); }
  49. "continue"|"CONTINUE"   { ret_print("KEYWORD_CONTINUE"); }
  50. "break"|"BREAK"         { ret_print("KEYWORD_BREAK"); }
  51. "void"|"VOID"           { ret_print("KEYWORD_VOID"); }
  52. "return"|"RETURN"       { ret_print("KEYWORD_RETURN"); }
  53.  
  54.  
  55. "+"|"-"                 { ret_print("ADDOP"); }
  56. "*"                     { ret_print("MULOP"); }
  57. "/"                     { ret_print("DIVOP"); }
  58. "++"|"--"               { ret_print("INCR"); }
  59. "||"                    { ret_print("OROP"); }
  60. "&&"                    { ret_print("ANDOP"); }
  61. "!"                     { ret_print("NOTOP"); }
  62. "=="|"!="               { ret_print("EQUOP"); }
  63. ">"|"<"|">="|"<="       { ret_print("RELOP"); }
  64.  
  65.  
  66. "("             { ret_print("LPAREN"); }
  67. ")"             { ret_print("RPAREN"); }
  68. "]"             { ret_print("LBRACK"); }
  69. "["             { ret_print("RBRACK"); }
  70. "{"             { ret_print("LBRACE"); }
  71. "}"             { ret_print("RBRACE"); }
  72. ";"             { ret_print("SEMI"); }
  73. "."             { ret_print("DOT"); }
  74. ","             { ret_print("COMMA"); }
  75. "="             { ret_print("ASSIGN"); }
  76. "&"             { ret_print("REFER"); }
  77.  
  78.  
  79. {ID}            {
  80.                     // insert identifier into symbol table
  81.                     insert(yytext, strlen(yytext), UNDEF, lineno);
  82.                     ret_print("ID");
  83.                 }
  84. {ICONST}        { ret_print("ICONST"); }
  85. {FCONST}        { ret_print("FCONST"); }
  86. {CCONST}        { ret_print("CCONST"); }
  87. {STRING}        { ret_print("STRING"); }
  88.  
  89.  
  90. "\n"            { lineno += 1; }
  91. [ \t\r\f]+          /* eat up whitespace */
  92.  
  93. .               { yyerror("Unrecognized character"); }
  94.  
  95. %%
  96.  
  97. void ret_print(char *token_type){
  98.     printf("yytext: %s\ttoken: %s\tlineno: %d\n", yytext, token_type, lineno);
  99. }
  100.  
  101. void yyerror(char *message){
  102.     printf("Error: \"%s\" in line %d. Token = %s\n", message, lineno, yytext);
  103.     exit(1);
  104. }
  105.  
  106. int main(int argc, char *argv[]){
  107.     // initialize symbol table
  108.     init_hash_table();
  109.  
  110.     // open input file
  111.     yyin = fopen(argv[1], "r");
  112.    
  113.     // lexical analysis
  114.     yylex();
  115.     fclose(yyin);
  116.    
  117.     // symbol table dump
  118.     yyout = fopen("symtab_dump.out", "w");
  119.     symtab_dump(yyout);
  120.     fclose(yyout); 
  121.    
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement