4 Replies - 4354 Views - Last Post: 22 July 2009 - 03:45 PM Rate Topic: -----

#1 d-l-sama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-January 09

Make a compiler using lex and yacc tools

Post icon  Posted 22 July 2009 - 06:42 AM

Hello.I have to make a compiler for a typical program language but i have some problems using that tools.I am trying to understand how it works but i have some difficulties.Here is my code.It is copy-paste from my book but i have errors.Can anyone explain me why?I can't run any program of my book.


lex
%{
#include <string.h>
#include <stdio.h>
#include "y.tab.h"

#define N 9
%}

%%


[1-9][0-9]*			{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return (DEC_CONST);
					}
"*"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('*');
					}
"/"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('/');
					}
"%"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('%');
					}
"+"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('+');
					}
"-"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('-');
					}
"="					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('=');
					}
"("					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('(');
					}
")"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return (')');
					}
[a-zA-Z]([a-zA-Z]|[0-9])*	{
							int i;
							printf("%s",yytext);
							i=id_or_keyword(yytext);
							if(i==ID)
								strcpy(yylval.ystr,yytext);
							else
								yylval.yint=i;
							return (i);
						}
[ \f\n\r\t]+				printf("%s",yytext);
.					{
						printf("Agnwstos xaraktiras %s\n",yytext);
					}

%%

static char *kid[N]={"and","else","if","int","not","or","print","read","while"};
static int kval[N]={AND,ELSE,IF,INT,NOT,OR,PRINT,READ,WHILE};


int id_or_keyword(char *s){
	int i;
	for(i=0; (i<N) && (strcmp(s,kid[i]) != 0);i++);

	if(i<N)
		return kval[i];
	else
		return ID;
}




yacc
%{
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
%}

%union
{
	int yint;
	char ystr[81];
	int nonterm;
}

%token <yint> AND ELSE IF INT NOT OR PRINT READ WHILE
%token <ystr>	DEC_CONST
%token <ystr>	'(' ')'
%token <ystr>	'*' '/' '%' '+' '-' '='
%token <ystr> ID

%type <nonterm> PrimExpr Literal ParenExpr PrefixExpr MultExpr
%type <nonterm> AddExpr Expr

%start Expr
%%

PrimExpr 	:	Literal
				{
					$$=$1;
				}
		;
Literal		:	DEC_CONST
				{
					$$=atoi($1);
				}
		;
ParenExpr	:	PrimExpr
				{
					$$=$1;
				}
			|	'(' Expr ')'
				{
					$$=$2;
				}
		;
PrefixExpr	:	ParenExpr
				{
					$$=$1;
				}
		;
MultExpr		:	PrefixExpr
				{
					$$=$1;
				}
			|	MultExpr '*' PrefixExpr
				{
					$$=$1*$3;
				}
			|	MultExpr '/' PrefixExpr
				{
					$$=$1/$3;
				}
			|	MultExpr '%' PrefixExpr
				{
					$$=$1%$3;
				}
		;
AddExpr		:	MultExpr
				{
					$$=$1;
				}
			|	AddExpr '+' MultExpr
				{
					$$=$1+$3;
				}
			|	AddExpr '=' MultExpr
				{
					$$=$1-$3;
				}
		;
Expr			:	AddExpr
				{
					$$=$1;
					printf("To apotelesma einai:%d\n",$1);
				}
		;

%%
void yyerror(char *s)
{
	fputs(s,stderr); putc('\n',stderr);
}


main(void)
{
	yyparse();
}



I just want to show me why i have this error:


syd:90: warning: conflicting types for 'yyerror'
y.tab.c:1475: warning: previous implicit declaration of 'yyerror' was here
Undefined symbols:
"_yywrap", referenced from:
_yylex in ccy3LNwv.o
_input in ccy3LNwv.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Make a compiler using lex and yacc tools

#2 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Make a compiler using lex and yacc tools

Posted 22 July 2009 - 09:36 AM

Ask in Computer Science forum
Was This Post Helpful? 0
  • +
  • -

#3 d-l-sama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-January 09

Re: Make a compiler using lex and yacc tools

Post icon  Posted 22 July 2009 - 03:37 PM

I have to make a compiler for a typical program language but i have some problems using that tools.I am trying to understand how it works but i have some difficulties.Here is my code.It is copy-paste from my book but i have errors.Can anyone explain me why?I can't run any program of my book.


lex

%{
#include <string.h>
#include <stdio.h>
#include "y.tab.h"

#define N 9
%}

%%


[1-9][0-9]*			{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return (DEC_CONST);
					}
"*"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('*');
					}
"/"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('/');
					}
"%"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('%');
					}
"+"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('+');
					}
"-"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('-');
					}
"="					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('=');
					}
"("					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return ('(');
					}
")"					{
						printf("%s",yytext);
						strcpy(yylval.ystr,yytext);
						return (')');
					}
[a-zA-Z]([a-zA-Z]|[0-9])*	{
							int i;
							printf("%s",yytext);
							i=id_or_keyword(yytext);
							if(i==ID)
								strcpy(yylval.ystr,yytext);
							else
								yylval.yint=i;
							return (i);
						}
[ \f\n\r\t]+				printf("%s",yytext);
.					{
						printf("Agnwstos xaraktiras %s\n",yytext);
					}

%%

static char *kid[N]={"and","else","if","int","not","or","print","read","while"};
static int kval[N]={AND,ELSE,IF,INT,NOT,OR,PRINT,READ,WHILE};


int id_or_keyword(char *s){
	int i;
	for(i=0; (i<N) && (strcmp(s,kid[i]) != 0);i++);

	if(i<N)
		return kval[i];
	else
		return ID;
}





yacc
%{
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
%}

%union
{
	int yint;
	char ystr[81];
	int nonterm;
}

%token <yint> AND ELSE IF INT NOT OR PRINT READ WHILE
%token <ystr>	DEC_CONST
%token <ystr>	'(' ')'
%token <ystr>	'*' '/' '%' '+' '-' '='
%token <ystr> ID

%type <nonterm> PrimExpr Literal ParenExpr PrefixExpr MultExpr
%type <nonterm> AddExpr Expr

%start Expr
%%

PrimExpr	 :	Literal
				{
					$$=$1;
				}
	   ;
Literal		:	DEC_CONST
				{
					$$=atoi($1);
				}
	   ;
ParenExpr	:	PrimExpr
				{
					$$=$1;
				}
			|	'(' Expr ')'
				{
					$$=$2;
				}
	   ;
PrefixExpr	:	ParenExpr
				{
					$$=$1;
				}
	   ;
MultExpr		:	PrefixExpr
				{
					$$=$1;
				}
			|	MultExpr '*' PrefixExpr
				{
					$$=$1*$3;
				}
			|	MultExpr '/' PrefixExpr
				{
					$$=$1/$3;
				}
			|	MultExpr '%' PrefixExpr
				{
					$$=$1%$3;
				}
	   ;
AddExpr		:	MultExpr
				{
					$$=$1;
				}
			|	AddExpr '+' MultExpr
				{
					$$=$1+$3;
				}
			|	AddExpr '=' MultExpr
				{
					$$=$1-$3;
				}
	   ;
Expr			:	AddExpr
				{
					$$=$1;
					printf("To apotelesma einai:%d\n",$1);
				}
	   ;

%%
void yyerror(char *s)
{
	fputs(s,stderr); putc('\n',stderr);
}


main(void)
{
	yyparse();
}





just want to show me why i have this error and how could i solve this:


syd:90: warning: conflicting types for 'yyerror'
y.tab.c:1475: warning: previous implicit declaration of 'yyerror' was here
Undefined symbols:
"_yywrap", referenced from:
_yylex in ccy3LNwv.o
_input in ccy3LNwv.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Make a compiler using lex and yacc tools

Posted 22 July 2009 - 03:40 PM

Moved to C/C++ :)
Was This Post Helpful? 0
  • +
  • -

#5 d-l-sama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 03-January 09

Re: Make a compiler using lex and yacc tools

Posted 22 July 2009 - 03:45 PM

View Postdebjit625, on 22 Jul, 2009 - 08:36 AM, said:

Ask in Computer Science forum

Ok thanks.I have just posted there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1