Home | History | Annotate | Line # | Download | only in manual
      1 /*
      2  * pascal.lex: An example PASCAL scanner
      3  *
      4  */
      5 
      6 %{
      7 #include <stdio.h>
      8 #include "y.tab.h"
      9 
     10 int line_number = 0;
     11 
     12 void yyerror(char *message);
     13 
     14 %}
     15 
     16 %x COMMENT1 COMMENT2
     17 
     18 white_space       [ \t]*
     19 digit             [0-9]
     20 alpha             [A-Za-z_]
     21 alpha_num         ({alpha}|{digit})
     22 hex_digit         [0-9A-F]
     23 identifier        {alpha}{alpha_num}*
     24 unsigned_integer  {digit}+
     25 hex_integer       ${hex_digit}{hex_digit}*
     26 exponent          e[+-]?{digit}+
     27 i                 {unsigned_integer}
     28 real              ({i}\.{i}?|{i}?\.{i}){exponent}?
     29 string            \'([^'\n]|\'\')+\'
     30 bad_string        \'([^'\n]|\'\')+
     31 
     32 %%
     33 
     34 "{"                  BEGIN(COMMENT1);
     35 <COMMENT1>[^}\n]+
     36 <COMMENT1>\n            ++line_number;
     37 <COMMENT1><<EOF>>    yyerror("EOF in comment");
     38 <COMMENT1>"}"        BEGIN(INITIAL);
     39 
     40 "(*"                 BEGIN(COMMENT2);
     41 <COMMENT2>[^)*\n]+
     42 <COMMENT2>\n            ++line_number;
     43 <COMMENT2><<EOF>>    yyerror("EOF in comment");
     44 <COMMENT2>"*)"       BEGIN(INITIAL);
     45 <COMMENT2>[*)]
     46 
     47  /* note that FILE and BEGIN are already
     48   * defined in FLEX or C so they can't
     49   * be used. This can be overcome in
     50   * a cleaner way by defining all the
     51   * tokens to start with TOK_ or some
     52   * other prefix.
     53   */
     54 
     55 and                  return(AND);
     56 array                return(ARRAY);
     57 begin                return(_BEGIN);
     58 case                 return(CASE);
     59 const                return(CONST);
     60 div                  return(DIV);
     61 do                   return(DO);
     62 downto               return(DOWNTO);
     63 else                 return(ELSE);
     64 end                  return(END);
     65 file                 return(_FILE);
     66 for                  return(FOR);
     67 function             return(FUNCTION);
     68 goto                 return(GOTO);
     69 if                   return(IF);
     70 in                   return(IN);
     71 label                return(LABEL);
     72 mod                  return(MOD);
     73 nil                  return(NIL);
     74 not                  return(NOT);
     75 of                   return(OF);
     76 packed               return(PACKED);
     77 procedure            return(PROCEDURE);
     78 program              return(PROGRAM);
     79 record               return(RECORD);
     80 repeat               return(REPEAT);
     81 set                  return(SET);
     82 then                 return(THEN);
     83 to                   return(TO);
     84 type                 return(TYPE);
     85 until                return(UNTIL);
     86 var                  return(VAR);
     87 while                return(WHILE);
     88 with                 return(WITH);
     89 
     90 "<="|"=<"            return(LEQ);
     91 "=>"|">="            return(GEQ);
     92 "<>"                 return(NEQ);
     93 "="                  return(EQ);
     94 
     95 ".."                 return(DOUBLEDOT);
     96 
     97 {unsigned_integer}   return(UNSIGNED_INTEGER);
     98 {real}               return(REAL);
     99 {hex_integer}        return(HEX_INTEGER);
    100 {string}             return{STRING};
    101 {bad_string}         yyerror("Unterminated string");
    102 
    103 {identifier}         return(IDENTIFIER);
    104 
    105 [*/+\-,^.;:()\[\]]   return(yytext[0]);
    106 
    107 {white_space}        /* do nothing */
    108 \n                   line_number += 1;
    109 .                    yyerror("Illegal input");
    110 
    111 %%
    112 
    113 void yyerror(char *message)
    114 {
    115    fprintf(stderr,"Error: \"%s\" in line %d. Token = %s\n",
    116            message,line_number,yytext);
    117    exit(1);
    118 }
    119 
    120 
    121