编辑文件时出现分段错误(核心转储)?

时间:2015-12-19 16:25:44

标签: c segmentation-fault bison yacc ppm

我正在ubuntu中创建一个带有bison的程序,根据用户输入编辑一个ppm文件,但是我遇到了一个分段错误核心转储错误,而我似乎无法修复它,继承我的代码:

void ponto(int x, int y, char fname[100]){
  int r,g,b;
  int i,j;
  int col,row;
  int c = 255;

  FILE *myFile = fopen(fname, "r");
  if (myFile == NULL)
  {
    printf("Ficheiro nao existe");
  }
  else
    fscanf(myFile, "P3\n%d%d\n255\n ", &col, &row);

  fscanf(myFile,"%d %d %d\n ",&r,&g,&b );

  FILE *fout = fopen(fname,"w"); 

  fprintf(fout, "P3\n%d %d\n255\n",col,row);

  for (j = 0;j < col; )
  {
    for (i = 0;i < row; i++)
    {   
      if ( j == y -1 && i == x - 1)
      {
        fprintf(fout,"0 0 0 ");
      }
      else
        fprintf(fout,"%d %d %d  ",r,g,b );

    }
    fprintf(fout,"\n");
    j++;
  }
  fclose(fout);
}

并且继承了我的yacc代码:

%{ 
#include <stdio.h>
#include "defs.h"
#include "funcoes.h" 
Lista lista_variaveis = NULL; /* guarda o valor das variavaies definidas */ 
char * cfile = NULL; /* variavel currentfile para saber que ficheiro estamos a usar */
%}

%union { 
       int valor;
       RES resolucao;
       COR color;
       COOR cord;   
       char * fname;
       char * idvar;}

%type  <resolucao> resol
%type  <valor> num
%type  <color> rgb
%type  <valor> expr
%type  <cord>  coord

%token <idvar> IDVAR
%token <valor> INT
%token <fname> FNAME
%token SAIR NOVA ABRIR GUARDAR PONTO

%start s
%%
s     : comando
      ;
comando :  NOVA resol rgb ';'{makef($2.resx,$2.resy,$3.r,$3.g,$3.b);
                          cfile = "teste2.pnm";printf("%s",cfile); } comando                      
        |  ABRIR FNAME ';' {openf($2);cfile = $2};  comando 
        |  PONTO coord ';' {ponto($2.xx,$2.yy,cfile);} comando    
        |  GUARDAR               
        |  defvar ';' comando
        |  SAIR         {return 0; /*termina */ }
        ; 
coord : expr ',' expr { $$.xx = $1; $$.yy = $3;}
      ; 
resol : expr 'x' expr  { $$.resx = $1; $$.resy = $3;}     
      ;
rgb   : expr':'expr':'expr { $$.r = $1; $$.g = $3; $$.b = $5;}
      ;
num   : num '+' num { $$ = $1 + $3;} 
      | num '*' num { $$ = $1 * $3;}
      | INT         { $$ = $1;}
      ;  
expr  : num         { $$ = $1;}  
      | IDVAR       { $$ = valor_variavel(lista_variaveis, $1);} 
      ; 
defvar: IDVAR '=' num  { define_variavel(&lista_variaveis,$1,$3);} 
      ;

%%``

0 个答案:

没有答案