Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im writing a simple progamm using the lists in flex but it shows an error in line 22 and i cant understand it.Can you pls help me ?

Here the code


C++
%{
	#include<stdio.h>
%}
%%
typedef struct liste{
	char * cle;
	int    data;
	struct liste * svt;
} noeud, *liste;
liste inserer( char * k )
{
	liste aux;
	aux = ts;
	while ( aux ){
		if ( strcmp( aux->cle, k ) == 0 )
		return aux;
		aux = aux -> svt;
	}
	printf("\nInsertion...");
	aux = ( liste ) malloc( sizeof(noeud) );
	aux->cle = (char *) malloc( strlen(k) + 1 );
	strcpy( aux->cle, k ); //LIGNE 22
	aux->data = 0;
	aux->svt  = ts;
	ts = aux;
	return ts;
}


What I have tried:

I tried deleting the spaces and that is all
Posted
Updated 28-Mar-16 21:31pm
v3

You are using the same name for the type and one object (liste) but the object is a pointer while the type is not. So you should use a different name for the pointer and use that for your variable:
C++
typedef struct liste{
	char * cle;
	int    data;
	struct liste * svt;
} noeud, *pliste;

pliste inserer( char * k )
{
    pliste aux;
    /* ... */
    aux = ( pliste ) malloc( sizeof(noeud) );


[EDIT]
The file format is not a valid flex file because the separator between rules and user code is missing. If there are no rules just insert the line:
C++
%{
	#include<stdio.h>
	#include<string.h>
%}
%%
/* Rules */
%%
/* User code*/
typedef struct liste{
/* ... */

[/EDIT]
[EDIT2]
Added inclusion of string.h above to make the compiler happy.
[/EDIT]
 
Share this answer
 
v3
Comments
Member 12421850 29-Mar-16 4:11am    
I fixed that i mean i replaced 'liste' by 'pliste' but it kept showing the same error.
Jochen Arndt 29-Mar-16 4:20am    
Which error (please show the complete message)?

If it is something regarding the strcpy() function, include the string.h header file.
Member 12421850 29-Mar-16 4:24am    
I wish i can take a capture but i cant though but when i want to excute the file it sends this error:--->Line 22: unrecognized rule <---- that is the ONLY whole message
And yes it is about the strcpy() i guess..
Jochen Arndt 29-Mar-16 4:41am    
It is a flex error. I just tested it and there is a "%%" separator line missing. I will update my answer.
Member 12421850 29-Mar-16 4:47am    
I will be waiting, thank you!
The first problem is that your code is at a place it should not be, because of misplaced %.
You are very confused about the difference between a pointer and the data pointed by a pointer.
I see at least 6 errors in the code.
[French]
D'après ce que je vois, tu brule les étapes, et c'est pas bon.
Conseil:
- laisse de coté flex pour l'instant.
- apprend correctement le langage C en lisant la documentation et en suivant les exemples et les tutoriels (il vaut mieux trop que pas assez).
- quand tu maitrisera, tu pourra revenir à flex.

pour flex:
- prend aussi le temps d'apprendre, commence avec des sujets que tu maitrise.
- pour ton projet, essaye de débugger ton code avant de l'intégrer dans ton projet Flex.
 
Share this answer
 
Comments
Member 12421850 29-Mar-16 4:09am    
It only showed ONE error about that line 22.
And im not confused at all the code was given to me by a teacher and he asked me to correct the mistake.
Patrice T 29-Mar-16 6:08am    
parle tu français ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900