1 Replies - 273 Views - Last Post: 24 April 2010 - 06:16 AM Rate Topic: -----

#1 sri Harsha  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 19-April 10

compilers project

Posted 24 April 2010 - 05:32 AM

i am doing compilers project .....in c.
that is to construct lexer and parser for my own language.......
it is giving error and system will hang when i remove ? in output statement(just like teminal symbol ';' in c)..
how to construct next phase of compiler design i,e SDT(syntax directed translation)...
/

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
FILE *fp;
char delim[10]={' ','\t','\n',',','{','}','[',']','(',')'};
char oper[5]={'+','-','*','/','%'};
char lop[3]={'<','>','#'};
char op[9]={'+','-','*','/','%','<','>','!','='};
char key[14][12]={"Integer","Real","Char","If","Else","Repeat","Upto","Inc","Begin","End","Input","Read","Output","Write"};
void skipcomment();
void analyze();
void checkwrite();
void check(char []);
int isdelim(char);
int isend(char c);
int isop(char);
int operator();
void apara();
void ipara();
void rpara();
int fop=0,numflag=0,f=0,k=0,y=0,z=0,flag,fop1=0;
char c,ch,sop,input[100];
char token[50];
int main()
{
	char fname[12];
	
	printf("\n enter filename:");
	scanf("%s",fname);
	fp=fopen(fname,"r");
	if(fp==NULL)
	printf("\n ERROR : The File Does Not Exist");
	else
	analyze();					//scan the input file
	printf("\t%s\n",input);				//print the output terminal array
	//printf("\tk=%d\n",k);
return 0;
}

/*------ANALYZE THE INPUT ---------------*/
void analyze()
{
	
	long int var;
	while(!feof(fp))
	{
		c=getc(fp);				//take character input
		if(c=='!')
		{
			skipcomment();
		}
		else if(isalpha(c))			//to check if alphabet
		{
			if(numflag==1)
			{
				token[z]='\0';		//numflag=1 signifies a number
				check(token);
				z=0;
			}
			else
			{
				token[z]=c;			//create token
				z++;
			}
			if(f==0)
				f=1;				//f=1 signifies an identifier
		}
		else if(isalnum(c))
		{
			if(numflag==0)				//to check if alphanumeric
				numflag=1;			
			token[z]=c;				
			z++;
		}
		else
		{
			if(isend(c))
			{					// to check if end of line character
			input[k++]='s';
			}
			else if(isdelim(c))
			{
				if(numflag==1)
				{
					token[z]='\0';
					check(token);
					numflag=0;
				}
				if(f==1)			//to check if identifier
				{
					token[z]='\0';
					numflag=0;
					check(token);
				}
				z=0;
				f=0;
			}
			else if(isop(c))
			{					// to check if operator
				if(numflag==1)
				{
					token[z]='\0';
					check(token);
					z=0;
					operator();
				}
				else if(f==1)
				{
					token[z]='\0';
					z=0;
					f=0;
					numflag=0;
					check(token);
					operator();
				}
				if(fop==1)
				{
					fop=0;
					sop='\0';
				}		
				else if(fop1==1)
				{	
					fop1=0;
				}
			}
			else if(c=='.')
			{
				token[z]=c;
				z++;
			}
		}
	}

}

/*--------------- TO CHECK IF OPERATOR-------------------------- */
int isop(char c)
{
   for(y=0;y<9;y++)				 
   {
       if(c==op[y]){return 1;}
  }
}


/*--------- TO CHECK FOR END OF LINE ' ? '----------- */
int isend(char c)
{
	if(c=='?')
	{
		ch=token[z-1];
		if(isdigit(ch))
		{
			token[z]='\0';
			z=0;
			check(token);
			return 1;
		}
		else if(isalpha(ch))
		{
		        token[z]='\0';
			z=0;
			check(token);
			return 1;
		}
	/* IF IF END OF LINE RETURN 1*/
	return 1;
	}
       /* OTHER WISE RETURN 0 */
	else return 0;

}


/*----- TO CHECK IF DELIMITOR--------*/
int isdelim(char c)
{
	int i=0,j=0,f1,f2;
	char str[20],temp1[6]={"Enter"},temp2[5]={"Exit"};
	
	if(c=='[')
	{
		while((str[j]=getc(fp))!= ']')
			j++;
	
			str[j]='\0';
			/* compare with 'Enter' */	
			while(i<j)
			{
				if (temp1[i] != str[i])
				{
					f1=1;
					break;
				}
                                 else
				i++;
			}
			if(f1!=1)
			input[k++]='u'; /*---TOKEN FOR [enter] -----*/
			i=0;
			while(i<j)
			{
				if (temp2[i] != str[i])
				{
					f2=1;
					break;
				}
                                else
				i++;
			}
			if(f2!=1)
			input[k++]='z';/*------ TOKEN FOR [exit] -----*/
	}



	for(i=0;i<10;i++)
	{     
              /* IF CHARACTER IS DELIMITOR RETURN 1 */
		if(c==delim[i])
		return 1;
	}

	return 0;

}

/*---------------- TO GET OPERATOR TOKEN---------*/

int operator()
{
	int i,j;
	ch=getc(fp);

	/* TO CHECK FOR = and == */
	if(c=='=')		
	{
		if(ch=='=')
		{	fop=1;
			sop=ch;
			input[k++]='l'; /*-- TOKEN FOR == */
			return 1;
		}
		fop1=1;
		input[k++]='a';/*---- TOKEN FOR =  */
		ungetc(ch,fp);
		return 1;
	}
	
         /*---- TO CHECK FOR ARITHMETIC OPERATOR */
	for(i=0;i<5;i++)
	{
		if(c==oper[i])
		{
			if(c=='-'&& ch=='-')
			{
					fop=1;
					sop=ch;
					input[k++]='a'; /* TOKEN FOR  --  */
					return 1;
			}
			else if(c=='+'&&ch=='+')
			{
					fop=1;
					sop=ch;
					input[k++]='a';/* TOKEN FOR  ++  */
					return 1;
			}
			else if(ch=='=')
			{
					fop=1;
					sop=ch;
					input[k++]='a';/* TOKEN FOR    +|-|/|* =    */
					return 1;
			}
			else
			{
			fop1=1;			
			input[k++]='a';
			ungetc(ch,fp);
			return 1;
			}
		}
	}
			



/* FOR LOGICAL OPERATORS */

	for(i=0;i<3;i++)
	{
		if(c==lop[i])
		{

				if(ch=='=')
				{
					fop=1;
					sop=ch;
					input[k++]='l'; /*     TOKEN FOR  <=  |  >=  | #=  */
					return 1;
				}
				else
				{	fop1=1;
					input[k++]='l';/*   TOKEN FOR  >  | < | #      */
					ungetc(ch,fp);
					return 1;
				}

		}
	}
	return 0;
}

/*---- FUNCTION TO SKIP THE COMMENT ---------*/

void skipcomment()
{
	ch=getc(fp);
	while(f==0)
	{
		ch=getc(fp);
		if(ch=='!')
			f=1;
	}
	f=0;
	input[k++]='c';
}



/*----TO ASSIGN TOKEN NAMES TO KEYWORDS, NUMBERS AND IDENTIFIERS --*/
void check(char t[])
{
	int i,count=0;
	if(numflag==1)
	{
		input[k++]='n';
		numflag=0;
		return;
	}
	else if(strcmp(t,"Begin")==0)
	{
		input[k++]='b';
	}	
	else if(strcmp(t,"End")==0)
	{
		input[k++]='f';
	}	
	else if(strcmp(t,"Upto")==0)
	{
		input[k++]='t';
	}	
	else if(strcmp(t,"Inc")==0)
	{
		input[k++]='j';
	}	
	else if(strcmp(t,"Repeat")==0)
	{
		input[k++]='y';
	}
	else if(strcmp(t,"Else")==0)
	{
		input[k++]='e';
	}
	else if(strcmp(t,"Input")==0)
	{
		flag=1;
		rpara();
	}
	else if(strcmp(t,"Read")==0)
	{
		apara();	
	}
	else if(strcmp(t,"Integer")==0)
	{
		ipara();
	}
	else if(strcmp(t,"Char")==0)
	{
		ipara();
	}
	else if(strcmp(t,"Real")==0)
	{
		ipara();
	}
	else if(strcmp(t,"If")==0)
	{
		input[k++]='x';
	}
	else if(strcmp(t,"Output")==0)
	{
		checkwrite();
	}
	else if(strcmp(t,"Write")==0)
	{
		apara();
	}
	
	else
	{
	input[k++]='v';
	f=0;
	}
}


/*-------TO ASSIGN TOKEN NAMES TO ARRAY io FUNCTION -----*/
void apara()
{
int count=0;
		c=getc(fp);
		
	while(1)
	{
	z=0;
	count++;
	
		while(c!=',')
		{
			
			if(isalpha(c))
			{
				token[z++]=c;
			}
				c=getc(fp);
				if(c=='?')
				break;
		}
		if(c=='?')
		break;
		if(count>2)
		{
		printf("Error : Invalid Number Arguments In Array Function\n");
		return;
		}
		
	c=getc(fp);
	}
	input[k++]='i';
	input[k++]='s';
	count=0;	
}

/*----- TO ASSIGN TOKEN NAMES TO INPUT FUNCTIONS-------*/
void ipara()
{
int count=0;

		c=getc(fp);                              /* Real a,b,b? */
		
	while(1)
	{
	z=0;
	count++;
	
		while(c!=',')
		{
			
			if(isalpha(c))
			{
				token[z++]=c;
			}
				c=getc(fp);
				if(c=='?')
				{
				flag=1;
				break;
				}
		}
		if(c=='?')
		{
		flag=1;
		break;
		}
		
	c=getc(fp);
			
	}
	if(flag==1) input[k++]='d';
	else 
	{
	printf("Error : Missing End Of Statement  ' ? ' \n");
	return;
	}
	count=0;	
}

void rpara()
{
int count=0;

		c=getc(fp);
		
	while(1)
	{
	z=0;
	count++;
	
		while(c!=',')
		{
			
			if(isalpha(c))
			{
				token[z++]=c;
			}
				c=getc(fp);
				if(c=='?')
				break;
		}
		if(c=='?')
		break;
		
		
	c=getc(fp);
			
	}
	if(flag==1) input[k++]='r';

	input[k++]='s';
	count=0;	
}

/*--- TO ASSIGN TERMINAL VALUES FOR 'OUTPUT'  FUNCTION -------*/
void checkwrite()
{
	int count=0;
	c=getc(fp);	
		while(1)
		{
		
			if(c=='"')
			{
				count++;
				if(count>2){printf("Error : ' Output ' Syntax Error\n");return;}
				c=getc(fp);
				while(c!='"')
				{			
					c=getc(fp);
					if(c=='?'){printf("Error ; ' Output ' Syntax Error\n");return;}
					
				}
			}
			else if(c==' ') c=getc(fp);
			else if(count==0) 
			{
				printf("Error ; ' Output ' Syntax Error\n");
				return;
				
			}	
			
		if(c=='"') {count++; c=getc(fp);}
		
		if(c=='?') break;	
			
		}
		input[k++]='w';
		input[k++]='s';
}







My language specification is as fallows...

[Enter]
Real q,b,u?
Read a,b?
Write a,n?
Read s,d?
Input "hghbghhjb"?
Output "uuhu"?
If v<f
Begin
Output "jhsdbfjh"?
Repeat j=67 Upto t Inc 56
Begin
Output "hghg"?
End
End
[Exit]

Is This A Good Question/Topic? 0
  • +

Replies To: compilers project

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,224
  • Joined: 23-August 08

Re: compilers project

Posted 24 April 2010 - 06:16 AM

The wonderful Martyn.Rae -- a brilliant, highly respected member of our community here -- has written some fabulous tutorials on Writing an Interpreter. I'm sure it would please him to no end to know that the time and effort he put into crafting these tutorials was appreciated, so please...take a look in our C++ Tutorials section.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1