我不确定在我的野牛解析器中如何输出(复杂)有用的信息

发布于 2024-12-26 03:46:27 字数 789 浏览 6 评论 0原文

我正在编写一个 Flex/Bison Lexer/Parser 来解析我自己设计的脚本,作为大学的作业。一切进展顺利,我的脚本可以正确解析,检测到任何错误等,并且都可以识别标记并传递语义含义。

然而,我有点不确定如何将其转化为有用的信息...我只需要输出简单的文本,这本身就很容易,但是我正在努力解决在 yacc 中在哪里执行此操作的逻辑。

例如,假设我的 yacc 是这样的 -

statement : 
   DRAW shape AT location  { printf("Draw shape at location"); } 
;

shape : 
   CUBE 
  | PYRAMID
  | SPHERE 
; 

location : 
   '(' NUMBER ',' 'NUMBER' ',' 'NUMBER' ')' { int x = $2; int y = $4; int z = $6;
                                              printf("%d,%d,%d",x,y,z); 
; 

我想做的是让它打印出类似 drawShape(shape, x, y, z); 的东西; - 但我不确定如何将位置的语义值反馈到语句中,或者如何获取找到的形状标记是立方体、金字塔还是球体。

目前,位置处的打印语句首先触发,所以我的输出类似于

1,2,3在位置绘制形状

是否最好只创建一些变量来存储该位置的语义值并将它们用于每个语句? (这看起来有点草率,但我不知道如何做得更好。)

I'm writing a Flex/Bison Lexer/Parser in order to parse a script of my own design, as an assignment for University. It's going very well, and my script parses correctly, detects any errors etc. and both recognizes tokens and passes the semantic meanings across.

However, I am a little unsure how to translate this into useful information... I just need to output simple text which is easy enough in itself, however I am struggling with the logic of where to do this in yacc.

For instance, say my yacc is something like this -

statement : 
   DRAW shape AT location  { printf("Draw shape at location"); } 
;

shape : 
   CUBE 
  | PYRAMID
  | SPHERE 
; 

location : 
   '(' NUMBER ',' 'NUMBER' ',' 'NUMBER' ')' { int x = $2; int y = $4; int z = $6;
                                              printf("%d,%d,%d",x,y,z); 
; 

What I would like to do is to have it print out something like drawShape(shape, x, y, z); - but I am unsure how to feed back the semantic values from location into the statement, or how to get whether the shape token found was a cube, a pyramid or a sphere.

At the moment the print statement at location triggers first, so my output is something like

1,2,3Draw shape at location

Is it best just to create some variables to store the semantic values from the location and use them for each statement? (This seems a little sloppy, but I'm not sure how to do this any better.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有影子陪我不离不弃 2025-01-02 03:46:27

通常的方法是让每个操作创建一个数据结构,该数据结构通过 $$ 返回并由后续操作使用。例如,您可能有

%union {
    int      ival;
    struct {
        int  x,y,z;
    }        coord;
}

%token<ival>  NUMBER
%type<coord>  location

%%

statement:
    DRAW shape AT location { printf("Draw shape at (%d,%d,%d)\n", $4.x, $4.y, $4.z); }
;

location:
    '(' NUMBER ',' NUMBER ',' NUMBER ')'  { $.x = $2; $.y = $4; $.z = $6; }
;

The usual way is to have each action create a data structure that gets returned via $$ and used by later actions. For example, you might have

%union {
    int      ival;
    struct {
        int  x,y,z;
    }        coord;
}

%token<ival>  NUMBER
%type<coord>  location

%%

statement:
    DRAW shape AT location { printf("Draw shape at (%d,%d,%d)\n", $4.x, $4.y, $4.z); }
;

location:
    '(' NUMBER ',' NUMBER ',' NUMBER ')'  { $.x = $2; $.y = $4; $.z = $6; }
;
智商已欠费 2025-01-02 03:46:27

我已经将其更改为 -

statement:
    DRAW shape AT location {printf("Draw shape at (%d, %d, %d)",x,y,z); }
;

location :
    '(' NUMBER ',' NUMBER ',' NUMBER ')' {x=$2; y=$4; z=$6); 
; 

这可以正常工作,但是如果有更优雅的解决方案,我会感兴趣?

I've gone ahead and changed this to -

statement:
    DRAW shape AT location {printf("Draw shape at (%d, %d, %d)",x,y,z); }
;

location :
    '(' NUMBER ',' NUMBER ',' NUMBER ')' {x=$2; y=$4; z=$6); 
; 

This works correctly, however I would be interested if there is a more elegant solution?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文