问题描述
在我的最终项目中给我留下了几个代码,其中一个是用于 flex 和 bison 的。问题是 gcc 给我返回了消息
"error: request for member ‘n’ in something not a structure or union
141 | | CONSTANTE {$$=narynew("Constante");}"
%{
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "parserMiniC.tab.h"
int yylex(void);
void yyerror(char*);
typedef struct node {
char *type;
struct node **child;
int children;
}node_t;
typedef struct _liste_t { node_t *node; struct _liste_t *suivant;} liste_t;
liste_t * creer_liste( node_t *p );
liste_t * concatener_listes( liste_t *l1,liste_t *l2 );
node_t *attacher_liste( liste_t *liste,node_t *p);
node_t *narynew(char *nom);
node_t *naryattach(node_t *par,node_t *child);
void *naryprint(node_t *par);
bool error_Syntaxical=false;
extern unsigned int lineno;
extern bool error_lexical;
%}
%union{
node_t *n;
}
%left PLUS MOINS
%left MUL DIV
%left LSHIFT RSHIFT
%left BOR BAND
%left LAND LOR
%nonassoc THEN
%nonassoc ELSE
%left OP
%left REL
%type<n> expression fonction variable
%type<liste> instruction liste_instructions parm liste_parms
%token<n> CONSTANTE
%token IDENTIFICATEUR VOID INT FOR WHILE IF ELSE SWITCH CASE DEFAULT
%token BREAK RETURN PLUS MOINS MUL DIV LSHIFT RSHIFT BAND BOR LAND LOR LT GT
%token GEQ LEQ EQ NEQ NOT EXTERN
%start programme
%%
programme:
liste_declarations liste_fonctions
;
liste_declarations :
liste_declarations declaration
|
;
liste_fonctions:
liste_fonctions fonction
|fonction
;
declaration:
type liste_declarateurs ';'
;
liste_declarateurs:
liste_declarateurs ',' declarateur
| declarateur
;
declarateur:
IDENTIFICATEUR
| declarateur '[' CONSTANTE ']'
;
fonction:
type IDENTIFICATEUR '(' liste_parms ')' '{' liste_declarations liste_instructions '}'
| EXTERN type IDENTIFICATEUR '(' liste_parms ')' ';'
;
type:
VOID
| INT
;
liste_parms:
liste_parms ',' parm
| parm
;
parm:
INT IDENTIFICATEUR
;
liste_instructions :
liste_instructions instruction
|instruction
;
instruction:
iteration
| selection
| saut
| affectation ';'
| bloc
| appel
;
iteration:
FOR '(' affectation ';' condition ';' affectation ')' instruction
| WHILE '(' condition ')' instruction
;
selection:
IF '(' condition ')' instruction %prec THEN
| IF '(' condition ')' instruction ELSE instruction
| SWITCH '(' expression ')' instruction
| CASE CONSTANTE ':' instruction
| DEFAULT ':' instruction
;
saut:
BREAK ';'
| RETURN ';'
| RETURN expression ';'
;
affectation:
variable '=' expression
;
bloc:
'{' liste_declarations liste_instructions '}'
;
appel:
IDENTIFICATEUR '(' liste_expressions ')' ';'
;
variable:
IDENTIFICATEUR
| variable '[' expression ']'
;
expression:
'(' expression ')'
| expression binary_op expression %prec OP
| MOINS expression
| CONSTANTE {$$=narynew("Constante");}
| variable {$$=narynew("Variable");}
| IDENTIFICATEUR '(' liste_expressions ')'
;
liste_expressions:
liste_expressions ',' expression
| expression
|
;
condition:
NOT '(' condition ')'
| condition binary_rel condition %prec REL
| '(' condition ')'
| expression binary_comp expression
;
binary_op:
PLUS
| MOINS
| MUL
| DIV
| LSHIFT
| RSHIFT
| BAND
| BOR
;
binary_rel:
LAND
| LOR
;
binary_comp:
LT
| GT
| GEQ
| LEQ
| EQ
| NEQ
;
%%
int main(void){
printf("Debut de l'analyse Syntaxique :\n");
yyparse();
printf("Fin de l'analyse !\n");
printf("Resultat :\n");
naryprint(root);
return EXIT_SUCCESS;
}
void yyerror(char *s) {
fprintf(stderr,"Erreur de Syntaxe a la ligne %d: %s\n",lineno,s);
}
// tree fonctions ...
void *naryprint(node_t *par)
{
size_t cldidx;
node_t *cld;
node_t *match;
match = NULL;
for (cldidx = 0; cldidx < par->children; ++cldidx) {
cld = par->child[cldidx];
if (cld == NULL)
continue;
printf("%s --> %s \n",par->type,cld->type);
}
for (cldidx = 0; cldidx < par->children; ++cldidx) {
cld = par->child[cldidx];
if (cld == NULL)
continue;
naryprint(cld);
}
}
// narynew -- get new node
node_t *narynew(char *nom)
{
node_t *node;
node = calloc(1,sizeof(node_t));
node->children=0;
node->type=nom;
return node;
}
// naryattach -- attach new child node
node_t *naryattach(node_t *par,node_t *child)
{
size_t cldidx;
node_t *cld;
// increase size of array
cldidx = par->children++;
par->child = realloc(par->child,sizeof(node_t *) * par->children);
// allocate new node
cld = child;
// cross-link parent and child
par->child[cldidx] = cld;
printf("%s est attaché \n",cld->type);
return par;
}
liste_t *creer_liste( node_t *p ) {
liste_t *liste;
liste = (liste_t *) malloc(sizeof( liste_t ));
liste->node = p;
liste->suivant = NULL;
return liste;}
liste_t * concatener_listes( liste_t *l1,liste_t *l2 ) {
liste_t *l = l1;
if ( l1 == NULL )
return l2;
while ( l->suivant != NULL )
l = l->suivant;
l->suivant = l2;
return l1;
}
node_t *attacher_liste( liste_t *liste,node_t *p ) {
liste_t *l;
node_t *n;
for ( l = liste; l != NULL; l = l->suivant ) {
n=l->node;
naryattach(p,n);
}
return p;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)