与野牛一起苦苦挣扎

时间:2011-12-27 23:11:32

标签: c bison

这是我尝试解析的输入文件:

Sphere(worldGlobe).
Texture(worldGlobe, worldGlobeTexture1).
Clouds(clouds1).

我得到了类似的分段错误。出于某种原因,如果我只是改变这些句子的顺序,一切正常。

Sphere(worldGlobe).
Clouds(clouds1).
Texture(worldGlobe, worldGlobeTexture1).

str_multicat只是连接更多字符串。语法很简单。如果我从“谓词”的最后一个模式中删除此代码,则不会出现分段错误:

char result[1000];  
str_multicat(result,
"file -import \"D:/Program Files/Autodesk/Maya2012/presets/fluids/examples        /CloudsAndFog/FasterClouds.ma\";\n",
"rename cloudLayer ", cloudLayer, ";\n",
"rename skyFog  ", skyFog, ";\n"); 

这是我的野牛档案。

%{
#include <math.h>   
#include <stdio.h>  
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

int yylex(void);
void yyerror(char*);  

void str_multicat(char* result, ...);
%}

%union {
  char* s;
  double d;
  int i;
} 

/* Tokens */
%token SPHERE
%token TEXTURE
%token CLOUDS
%token CLOUDS_TRANSPARENCY
%token ROTATE
%token TRANSLATEIN
%token CAMERA
%token CAMERASHOT

%token LBRACKET
%token RBRACKET
%token DOT
%token COMMA

%token <d> DOUBLE
%token <i> INT;
%token <s> NAME

%%

listOfPredicates: /* empty string */
        | predicate listOfPredicates
;

predicate: 
SPHERE LBRACKET NAME RBRACKET DOT
{   
    char result[1000];  
    strcpy(result, "polySphere -n ");
    strcat(result, $3); 
    strcat(result, ";\n\0");        

    printf("%s\n", result); 
}
|
TEXTURE LBRACKET NAME COMMA NAME RBRACKET DOT
{       
    char object[50];
    strcpy(object, $3);

    char material[50];
    strcpy(material, object);
    strcat(material, "Mat");

    char materialSG[50];
    strcpy(materialSG, material);
    strcat(materialSG, "SG");

    char texture[50];
    strcpy(texture, $5);

    char objectPlace2dTexture[50];
    strcpy(objectPlace2dTexture, object);
    strcat(objectPlace2dTexture, "_place2dTexture");

    char objectFile[50];
    strcpy(objectFile, object);
    strcat(objectFile, "_file");

    char result[10000];
    str_multicat(result, 
    "// Assign new material\n",
    "shadingNode -asShader lambert -n ", material, ";\n",
    "sets -renderable true -noSurfaceShader true -empty -name ", materialSG, ";\n",
    "connectAttr -f ", material, ".outColor ", materialSG, ".surfaceShader;\n",
    "assignCreatedShader \"lambert\" \"\" ", material, " \"", object, "\";\n",
    "sets -e -forceElement ", materialSG, ";\n",
    "// Create file and place2dTexture nodes and connect them\n",
    "shadingNode -asTexture file -n ", objectFile, ";\n",
    "shadingNode -asUtility place2dTexture -n ", objectPlace2dTexture, ";\n",
    "connectAttr -f ",  objectPlace2dTexture, ".coverage ", objectFile, ".coverage;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".translateFrame ", objectFile,          ".translateFrame;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".rotateFrame ", objectFile, ".rotateFrame;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".mirrorU ", objectFile, ".mirrorU;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".mirrorV ", objectFile, ".mirrorV;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".stagger ", objectFile, ".stagger;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".wrapU ", objectFile, ".wrapU;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".wrapV ", objectFile, ".wrapV;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".repeatUV ", objectFile, ".repeatUV;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".offset ", objectFile, ".offset;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".rotateUV ", objectFile, ".rotateUV;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".noiseUV ", objectFile, ".noiseUV;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".vertexUvOne ", objectFile, ".vertexUvOne;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".vertexUvTwo ", objectFile, ".vertexUvTwo;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".vertexUvThree ", objectFile, ".vertexUvThree;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".vertexCameraOne ", objectFile, ".vertexCameraOne;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".outUV ", objectFile, ".uv;\n",
    "connectAttr -f ",  objectPlace2dTexture, ".outUvFilterSize ", objectFile, ".uvFilterSize;\n",
    "connectAttr -force ", objectFile, ".outColor ",  material, ".color;\n",
    "// Finally assign the texture file\n", 
    "setAttr -type \"string\" ", objectFile, ".fileTextureName \"C:/Maya/Textures/", texture, ".jpg\";\n"   
    );

    printf("%s\n", result); 
}
|

CLOUDS LBRACKET NAME RBRACKET DOT

{
    char cloudsName[50];
    strcpy(cloudsName, $3);


    char cloudLayer[50];
    strcpy(cloudLayer, cloudsName);
    strcat(cloudLayer, "_cloudLayer");

    char skyFog[50];
    strcpy(skyFog, cloudsName); 

strcat(skyFog, "_skyFog");

    char result[1000];  
    str_multicat(result,
"file -import \"D:/Program Files/Autodesk/Maya2012/presets/fluids/examples    /CloudsAndFog/FasterClouds.ma\";\n",
    "rename cloudLayer ", cloudLayer, ";\n",
    "rename skyFog  ", skyFog, ";\n");  


    printf("%s\n", result);

}
 ;

%%

main() {
    yyparse();
}

void yyerror(char *s) {
    fprintf(stderr, "ERROR: %s\n", s);
}

void str_multicat(char* result, ...)
{
    // Init arguments list
    va_list argptr;
    va_start(argptr, result);

    // Use arguments
    char* val;            
        strcpy(result, va_arg(argptr, char*));
    while(val = va_arg(argptr, char*))
    {       
                strcat(result, val);
    }
    strcat(result, "\0");

    // End arguments list
    va_end(argptr);
}

1 个答案:

答案 0 :(得分:0)

(问题在评论中回答。见Question with no answers, but issue solved in the comments (or extended in chat)

@larsman写道:

  

您的str_multicat应在每次通话中收到最终NULL个参数。