ic.l 2.36 KB
%{

#include "ic.h"

/* 
 * y.tab.h is created by "yacc -vd ic.y".  It is included so the
 * Lexical Analyzer knows the definitions of the "terminals" (%token)
 * in the grammar and the %union types.
 */

#include "y.tab.h"

/* commentdepth keeps track of nested comments */
int		commentdepth	= 0;
%}

%start		norm comment

LETTER		[a-zA-Z_]
DIGIT		[0-9]
HEXDIGIT	[0-9A-F]
OCTALDIGIT	[0-7]
ID		({LETTER}({LETTER}|{DIGIT})*)
DECINTEGER	(-?{DIGIT}+)
OCTINTEGER	({OCTALDIGIT}+("B"|"C"))
HEXINTEGER	({DIGIT}{HEXDIGIT}*"H")
SCALEFACTOR	("E"("+"|"-")?{DIGIT}+)
REAL		({DIGIT}+"."{DIGIT}*{SCALEFACTOR}?/(".."|[^.]))
STRING		((\"[^"]*\")|(\'[^']*\'))

%%
<norm>[ \t\n]		;		/* ignore whitespace */

<norm>{DECINTEGER}	{
		    	    yylval.intval=
				(int) strtol(yytext, (char **) NULL, 10);
		    	    return(INTCONST);
			}
<norm>{OCTINTEGER}	{
			/* remove the B or C at end */
			    yytext[yyleng-1]='\0'; 
		    	    yylval.intval=
				(int) strtol(yytext,(char **) NULL,8);
		    	    return(INTCONST);
			}
<norm>{HEXINTEGER}	{
			/* remove the H at end */
			    yytext[yyleng-1]='\0'; 
		    	    yylval.intval=
				 (int) strtol(yytext,(char **) NULL,16);
		    	    return(INTCONST);
			}
<norm>{REAL}		{
		    	    sscanf(yytext, "%lf",&yylval.floatval);
		    	    return(FLOATCONST);
			}
<norm>{STRING}		{
                            yylval.stringval=
				(char *) malloc((unsigned) yyleng+1);
			    strcpy(yylval.stringval,yytext);
			    return(STRINGCONST);
			}

<norm>[+\-*/=<>\^,.;:|([{}\])] 	return(yytext[0]);
<norm>"&"		return(AND);
<norm>":="		return(COLONEQUAL);
<norm>("#"|"<>")       	return(NOTEQUAL);
<norm>">="	       	return(GREATEREQUAL);
<norm>"<="	        return(LESSEQUAL);

"/*"			{/* Note: this is always active, because of nesting. */
			   BEGIN comment;
			   ++commentdepth;
			}
<norm>{ID}		{
		            yylval.idptr = symtab->Enter(ID, yytext, yyleng);
			    return(yylval.idptr->lextype);
			}

<comment>(.|\n)		;	/* discard everything within a comment */

<comment>"*/"		{
			   if (--commentdepth == 0)
			      BEGIN norm;
			}

<norm>.			{ /* No other rule applies, so it's a bad token */
		   	   fprintf(stderr,"Bad input char `%c' on line %d\n",
			      yytext[0],yylineno);
			}
%%

/* lexinit - starts the Lexical Analyzer off in the right start condition */
void initLex(void)
{
   BEGIN norm;
}

/* does this really need to be here? */
yywrap()
{
   return 1;
}