All pastes #197308 Raw Edit

Someone

public text v1 · immutable
·published 2006-10-11 06:44 UTC
rendered paste body
%{

// prototype
void initial() ;
int read( char name[18], int scope ) ;
void create( char name[18], char dataType, char type, int scope, int refer ) ;
void printSymT() ;

#define	 ASSIGN		300
#define	 FUNCT		301
#define	 PRINT		302
#define	 INPUT		303
#define  CONTINUE	304
#define  IF		305
#define  THEN		306
#define  ENDIF		307
#define  ELSE		308
#define  WHILE		309
#define  DO		310
#define  ENDDO		311
#define  INT		312
#define	 RETURN		313		
#define  IDENT		314
#define  END		315	

%}

/* regular definition */
delim		[ \t\n]
ws		{delim}+
letter		[A-Za-z]
digit		[0-9]
id		{letter}+({letter}|{digit})*
ascii_char	[^\"\n]
escaped_char	\\n
text_string	({ascii_char}|{escaped_char})*
text		\"{text_string}\"
comments	"//".*"\n"

%%

{ws}		{ /*no action and no return */ }

{digit}+	{ if( read( yytext, 0 ) == -1 ) create( yytext, 'I', 'L', 0, atoi(yytext) ); return INT; }
{text}		{ create( yytext, 'S', 'L', 0, -1 ) ; }
{comments}	{ /*no action and no return */ }

"="		{ return (ASSIGN); }

funct		{ return (FUNCT); }
print		{ return (PRINT); }
input		{ return (INPUT); }
continue	{ return (CONTINUE); }
if		{ return (IF); }
then		{ return (THEN); }
endif		{ return (ENDIF); }
else		{ return (ELSE); }
while		{ return (WHILE); }
do		{ return (DO); }
enddo		{ return (ENDDO); }
int		{ return (INT); }

end		{ return(END) ; }

{id}		{ if( read( yytext, 0 ) == -1 ) create( yytext, 'I', 'I', 0, -1 ) ; return IDENT;}

. 		{ return( yytext[0] ) ; }

%%

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int entryNum = 0 ; // the number of the entry in symbol table

struct SymTable 
{
	int scope ;	
	char dataType ;		// Integer or String
	char type ;		// Identifier or Literal
	char name[18] ;		// lexeme
	int reference ; 	// Numeric literal 
};

// symbol table maximum enrty 200 
struct SymTable symTab[200] ;

main()
{
	int in ;

	printf( "\nCOURSE:CS451  NAME:HYOJOO AHN  NN:01  PROGRAM#:06\n\n\n" ) ;

	initial() ;
	create( "FRED", 'I', 'I', 0, -1 ) ;

	printSymT() ; // print the symbol table

	in = yylex() ;

	while( in != 315 )
	{
		printf( "returned value %3d ", in ) ;
		printf( " %s\n" , yytext ) ;	

		in = yylex() ;
	}

	printSymT() ;
}x

// clear the table
void initial()
{
	entryNum = 0 ;
}

// read the elements from the symbol table
int read( char name[18], int scope )
{
	// tell whether it is in symbal Table or not
	int i ;

	// check from the last entry to first entry
	for( i = entryNum - 1 ; i >= 0 ; i-- )
	{

		if(strcmp(name, symTab[i].name) == 0 && scope == symTab[i].scope)
		{
			// printf( "It already included in Symbol Table on %3d \n\n" , i ) ;
			return i ;
		}
	}
	return -1 ;
}

// insert the new symbol to the symbol table  
void create( char name[18], char dataType, char type, int scope, int refer )
{
	symTab[ entryNum ].dataType = dataType ;
	symTab[ entryNum ].type = type ;
	symTab[ entryNum ].scope = scope ;
	strcpy ( symTab[ entryNum ].name, name ) ;
	symTab [ entryNum ].reference = refer ;
		
	entryNum ++ ;
}

void printSymT()
{
	int i = 0 ;

	printf( "\n<< Symbol Table >>\n\n" ) ;
	printf( "Index        Name         scope   D-Type   Type     reference \n" ) ;

	for ( i = 0 ; i < entryNum ; i++ )
	{
		printf( "%3d", i ) ;  					// index
		printf( "%20s", symTab[i].name ) ;		// name
		printf( "%6d", symTab[i].scope ) ;		// scope
		printf( "%8c", symTab[i].dataType ) ;		// data type
		printf( "%8c", symTab[i].type ) ;		// type
		printf( "%20d", symTab[i].reference ) ;	// value
		printf( "\n" ) ;
	}
	
	printf( "\n" ) ;
}

yywrap()
{
	return 1;
}