%{ /* -------------------------------------------------------------------- */ /* CSCI565 - Compilers University of Southern California Spring 2010 */ /* -------------------------------------------------------------------- */ /* Description: Lex input file specification for project 1 */ /* */ /* Author: Pedro Diniz (pedro@isi.edu) */ /* Date: Jan. 25, 2010 */ /* -------------------------------------------------------------------- */ /* definitions of constants */ #include #include #include #include "y.tab.h" /* -------------------------------------- */ /* -- change the use of yylineno stack -- */ /* -------------------------------------- */ #define MAX_INCLUDE_DEPTH 20 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH]; int lineno_stack[MAX_INCLUDE_DEPTH]; int include_stack_ptr = 0; %} %option yylineno /* regular definitions */ delim [\t\n" "] ws {delim}+ letter [A-Za-z\_] digit [0-9] id {letter}({letter}|{digit})* number {digit}+(\.{digit}+)?(E[+-]{digit}+)? include \#"include"[\t ]*[\<\"] %x incl comment %% {ws} { /* no action no return */ } ";" { return (';'); } "{" { return ('{'); } "}" { return ('}'); } "," { return (','); } "=" { return ('='); } "(" { return ('('); } ")" { return (')'); } "&" { return ('&'); } "~" { return ('~'); } "-" { return ('-'); } "+" { return ('+'); } "*" { return ('*'); } "/" { return ('/'); } "." { return (DOT_OP); } "<" { return (LT_OP); } "<=" { return (LE_OP); } ">" { return (GT_OP); } ">=" { return (GE_OP); } "[" { return ('['); } "]" { return (']'); } sizeof { return(SIZEOF); } "->" { return(PTR_OP); } "!=" { return(NE_OP); } "==" { return(EQ_OP); } "&&" { return(AND_OP); } "||" { return(OR_OP); } typedef { return(TYPEDEF); } int { return(INT); } char { return(CHAR); } void { return(VOID); } struct { return(STRUCT); } if { return(IF); } else { return(ELSE); } while { return(WHILE); } return { return(RETURN); } {number} { //yylval.i = atoi(yytext); return(NUMBER); } {include} { BEGIN(incl); } "/*" { BEGIN(comment); } {id} { // yylval.s = strdup(yytext); return(IDENTIFIER); } ({letter}|{digit})+(\.)*({letter}|{digit})*[\>\"] { if(include_stack_ptr >= MAX_INCLUDE_DEPTH) { fprintf(stderr, " *** Error: Includes nested too deeply. Increase MAX_INCLUDE_DEPTH value. (exit(1))\n"); exit(1); } yytext[strlen(yytext)-1] = '\0'; FILE *yyintmp; yyintmp = fopen(yytext, "r"); if (yyintmp != NULL) { /* printf(">>> Entering file (%s)\n",yytext); */ /* YY_CURRENT_BUFFER->yy_bs_lineno = yylineno; */ lineno_stack[include_stack_ptr] = yylineno; include_stack[include_stack_ptr] = YY_CURRENT_BUFFER; include_stack_ptr++; yylineno = 1; yyin = yyintmp; yyintmp = NULL; yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); } BEGIN(INITIAL); } (.|\n) "*/" { BEGIN(INITIAL); } . { printf("unmatched text: %s",yytext); } %% void install_id(){ printf(" >>> Installing identifier %s in symbol table... (later)\n", yytext); } void install_num(){ printf(" >>> Installing number %s in symbol table (later)\n", yytext); } int yywrap(){ /* printf(" >>> Inside yywrap. (%d)\n",include_stack_ptr); */ if(include_stack_ptr <= 0){ return 1; } else { include_stack_ptr--; /* printf(" >>> Leaving file.\n"); */ yy_delete_buffer( YY_CURRENT_BUFFER ); yylineno = lineno_stack[include_stack_ptr]; yy_switch_to_buffer(include_stack[include_stack_ptr]); } BEGIN(INITIAL); return 0; } int main(){ int tok; int n; n = 0; while(1) { tok = yylex(); if(tok == 0) break; printf("line:%2d token type:%3d token text:(%s)\n", yylineno, tok, yytext); n++; } printf("Number of tokens matched is %d\n",n); return 0; }