All pastes #47439 Raw Edit

Untitled

public text v1 · immutable
#47439 ·published 2006-03-29 14:14 UTC
rendered paste body
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"y.tab.h"
#define buffer_len 90000
struct token
{
	char token_type[101];
	char value[101];
	int lineno;
	int linepos;
};
struct token tokens[buffer_len];
int ntokens=0;
int no_chars=0;
int no_lines=0;
void add_token(char *type)
{
 strcpy(tokens[ntokens].token_type,type);
 strcpy(tokens[ntokens].value,yytext);
 tokens[ntokens].lineno=no_lines+1;
 tokens[ntokens].linepos=no_chars+1;
 ntokens++;
 no_chars+=strlen(yytext);
 if(ntokens>=buffer_len)
 {
	printf("\n Error! Token buffer Full.");
	exit(0);
 }
}
%}
variable [a-zA-Z_][a-zA-Z_0-9]*
number    [0-9]+
number2   [0-9]+.[0-9]+
number3   0x[number]
number4   0x[0-9]+.[0-9]+
string    \"[^\"]*\"
nextline [\n\r]
%%
"int" { printf("\n INT");
	add_token("keyword");
	return INT;
      }
"char" {  add_token("keyword");
	 return CHAR;
       }
"float" {  add_token("keyword");
	   return FLOAT;
        }
"for" {  add_token("keyword");
      }
"while" {  add_token("keyword");
	}
"do" {  add_token("keyword");
     }
"static" {  add_token("keyword");
	 }
"union" {  add_token("keyword");
	}
"struct" {  add_token("keyword");
	 }
"asm" {  add_token("keyword");
      }
"goto" {  add_token("keyword");
       }
"const" {  add_token("keyword");
	}
"auto" {  add_token("keyword");
       }
"break" {  add_token("keyword");
	}
"continue" {  add_token("keyword");
	   }
"unsigned" {  add_token("keyword");
	      return UNSIGNED;
	   }
"pascal" {  add_token("keyword");
	 }
"long" {  add_token("keyword");
	  return LONG;
       }
"double" {  add_token("keyword");
	    return DOUBLE;
	 }
"switch" {  add_token("keyword");
	 }
"case" {  add_token("keyword");
       }
"void" {  add_token("keyword");
	  return VOID;
       }
"return" {  add_token("keyword");
	 }
"SQL" { add_token("keyword"); }
"SELECT" { add_token("SQL keyword"); }
"FROM"   { add_token("SQL keyword"); }
"WHERE"  { add_token("SQL keyword"); }

{variable} {add_token("identifier"); return IDENTIFIER;}
{number} { add_token("number"); return NUMBER;}
{number2} { add_token("number"); return NUMBER;}
{number3} { add_token("number"); return NUMBER;}
{number4} { add_token("number"); return NUMBER;}
{string}  {add_token("string");}
{nextline} { no_lines++;no_chars=0;}
"++" { add_token("operator"); }
"--" { add_token("operator"); }
"<=" { add_token("operator"); }
">=" { add_token("operator"); }
"==" { add_token("operator"); }
">"  { add_token("operator"); }
"<"  { add_token("operator"); }
"&&" { add_token("operator"); }
"||" { add_token("operator"); }
"<<" { add_token("operator"); }
">>" { add_token("operator"); }
"->" { add_token("Indirect Member Selector"); }
"+"  { add_token("operator"); return PLUS;}
"-"  { add_token("operator"); return MINUS;}
"/"  { add_token("operator"); return DIVIDE;}
"*"  { add_token("operator"); return MULTIPLY;}
"="  { add_token("operator"); return EQUAL;}
";"  { add_token("Stmt Terminator"); return TERMINATOR;}
"("  { add_token("Open Bracket"); return OPEN;}
")"  { add_token("Close Bracker"); return CLOSE;}
"["  { add_token("Open Big Bracket");  }
"]"  { add_token("Close Big Bracket"); }
"{"  { add_token("Open Block"); return CURLY_OPEN;}
"}"  { add_token("Close Block"); return CURLY_CLOSE;}
","  { add_token("Comma"); return COMMA;}
"."  { add_token("Dirct Member Selector"); }
.    { no_chars++; }
%%
/*int main(int argc,char **argv)
{
int i;
if ( argc > 1 )
        yyin = fopen( argv[1], "r" );
else
        yyin = stdin;
yylex();
char string[1001];
FILE *fp=fopen("out.txt","w");
for(i=0;i<ntokens;i++)
{
	sprintf(string,"\n Token No= %d, Token Type= %s, Token Value= %s (%d,%d)",i+1,tokens[i].token_type,tokens[i].value,tokens[i].lineno,tokens[i].linepos);
	fputs(string,fp);
}
fclose(fp);
return(1);
}*/