Blame examples/manual/pascal.lex

Packit f00812
/*
Packit f00812
 * pascal.lex: An example PASCAL scanner
Packit f00812
 *
Packit f00812
 */
Packit f00812
Packit f00812
%{
Packit f00812
#include <stdio.h>
Packit f00812
#include "y.tab.h"
Packit f00812
Packit f00812
int line_number = 0;
Packit f00812
Packit f00812
void yyerror(char *message);
Packit f00812
Packit f00812
%}
Packit f00812
Packit f00812
%x COMMENT1 COMMENT2
Packit f00812
Packit f00812
white_space       [ \t]*
Packit f00812
digit             [0-9]
Packit f00812
alpha             [A-Za-z_]
Packit f00812
alpha_num         ({alpha}|{digit})
Packit f00812
hex_digit         [0-9A-F]
Packit f00812
identifier        {alpha}{alpha_num}*
Packit f00812
unsigned_integer  {digit}+
Packit f00812
hex_integer       ${hex_digit}{hex_digit}*
Packit f00812
exponent          e[+-]?{digit}+
Packit f00812
i                 {unsigned_integer}
Packit f00812
real              ({i}\.{i}?|{i}?\.{i}){exponent}?
Packit f00812
string            \'([^'\n]|\'\')+\'
Packit f00812
bad_string        \'([^'\n]|\'\')+
Packit f00812
Packit f00812
%%
Packit f00812
Packit f00812
"{"                  BEGIN(COMMENT1);
Packit f00812
<COMMENT1>[^}\n]+
Packit f00812
<COMMENT1>\n            ++line_number;
Packit f00812
<COMMENT1><<EOF>>    yyerror("EOF in comment");
Packit f00812
<COMMENT1>"}"        BEGIN(INITIAL);
Packit f00812
Packit f00812
"(*"                 BEGIN(COMMENT2);
Packit f00812
<COMMENT2>[^)*\n]+
Packit f00812
<COMMENT2>\n            ++line_number;
Packit f00812
<COMMENT2><<EOF>>    yyerror("EOF in comment");
Packit f00812
<COMMENT2>"*)"       BEGIN(INITIAL);
Packit f00812
<COMMENT2>[*)]
Packit f00812
Packit f00812
 /* note that FILE and BEGIN are already 
Packit f00812
  * defined in FLEX or C so they can't  
Packit f00812
  * be used. This can be overcome in                               
Packit f00812
  * a cleaner way by defining all the
Packit f00812
  * tokens to start with TOK_ or some
Packit f00812
  * other prefix.
Packit f00812
  */
Packit f00812
Packit f00812
and                  return(AND);
Packit f00812
array                return(ARRAY);
Packit f00812
begin                return(_BEGIN);
Packit f00812
case                 return(CASE);
Packit f00812
const                return(CONST);
Packit f00812
div                  return(DIV);
Packit f00812
do                   return(DO);
Packit f00812
downto               return(DOWNTO);
Packit f00812
else                 return(ELSE);
Packit f00812
end                  return(END);
Packit f00812
file                 return(_FILE);
Packit f00812
for                  return(FOR);
Packit f00812
function             return(FUNCTION);
Packit f00812
goto                 return(GOTO);
Packit f00812
if                   return(IF);
Packit f00812
in                   return(IN);
Packit f00812
label                return(LABEL);
Packit f00812
mod                  return(MOD);
Packit f00812
nil                  return(NIL);
Packit f00812
not                  return(NOT);
Packit f00812
of                   return(OF);
Packit f00812
packed               return(PACKED);
Packit f00812
procedure            return(PROCEDURE);
Packit f00812
program              return(PROGRAM);
Packit f00812
record               return(RECORD);
Packit f00812
repeat               return(REPEAT);
Packit f00812
set                  return(SET);
Packit f00812
then                 return(THEN);
Packit f00812
to                   return(TO);
Packit f00812
type                 return(TYPE);
Packit f00812
until                return(UNTIL);
Packit f00812
var                  return(VAR);
Packit f00812
while                return(WHILE);
Packit f00812
with                 return(WITH);
Packit f00812
Packit f00812
"<="|"=<"            return(LEQ);
Packit f00812
"=>"|">="            return(GEQ);
Packit f00812
"<>"                 return(NEQ);
Packit f00812
"="                  return(EQ);
Packit f00812
Packit f00812
".."                 return(DOUBLEDOT);
Packit f00812
Packit f00812
{unsigned_integer}   return(UNSIGNED_INTEGER);
Packit f00812
{real}               return(REAL);
Packit f00812
{hex_integer}        return(HEX_INTEGER);
Packit f00812
{string}             return{STRING};
Packit f00812
{bad_string}         yyerror("Unterminated string");
Packit f00812
Packit f00812
{identifier}         return(IDENTIFIER);
Packit f00812
Packit f00812
[*/+\-,^.;:()\[\]]   return(yytext[0]);
Packit f00812
Packit f00812
{white_space}        /* do nothing */
Packit f00812
\n                   line_number += 1;
Packit f00812
.                    yyerror("Illegal input");
Packit f00812
Packit f00812
%%
Packit f00812
Packit f00812
void yyerror(char *message)
Packit f00812
{
Packit f00812
   fprintf(stderr,"Error: \"%s\" in line %d. Token = %s\n",
Packit f00812
           message,line_number,yytext);
Packit f00812
   exit(1);
Packit f00812
}
Packit f00812
Packit f00812