gpt4 book ai didi

c - 与 Bison 的斗争

转载 作者:行者123 更新时间:2023-11-30 19:50:04 25 4
gpt4 key购买 nike

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

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");

这是我的 Bison 文件。

%{
#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);
}

最佳答案

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

@larsman 写道:

Your str_multicat should receive a final NULL argument in every call.

关于c - 与 Bison 的斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com