由于使用Flex和Bison具有模棱两可的语法,因此无序的结果

发布于 2025-01-22 04:34:39 字数 1356 浏览 5 评论 0原文

我正在尝试使用Flex和Bison创建一个用于可变声明的部分(与HTML有点相似),我的语法是正确的(没有词汇或语法错误),但是未订购显示的结果。

示例.txt:

    <SUB VARIABLE>
        < a AS INT />;
        <string AS STR />; 
        < x | y AS FLT />;
        <bool AS BOL />; 
        <char AS CHR />; 
    </SUB VARIABLE>
    

我得到的结果(不正确的结果):

a ---> 1
x ---> 2
y ---> 2
string ---> 4
char ---> 3
bool ---> 5

我想显示的结果(正确的结果):

a ---> 1
string ---> 4
x ---> 2
y ---> 2
bool ---> 5
char ---> 3

这是我的代码:

Synt.y:

DECLARATION:    DECLARATION     '<' SUB VARIABLE '>'    SUITE  
            
            |
;


SUITE: '<' idf  SUITE_VAR {inserer($2,getType());}
            | '<' '/' SUB VARIABLE '>' 
;

SUITE_VAR:      '|' idf  SUITE_VAR {inserer($2, getType());}
            |   AS INT '/' '>' ';' SUITE {setType(1);}
            |   AS FLT '/' '>' ';' SUITE {setType(2);}
            |   AS CHR '/' '>' ';' SUITE {setType(3);}
            |   AS STR '/' '>' ';' SUITE {setType(4);}
            |   AS BOL '/' '>' ';' SUITE {setType(5);}
;


我的语法可能模棱两可,我尝试了许多其他语法,但我也遇到了同样的问题。 您能告诉我如何写我的语法以取得订单的结果吗? 多谢。

I'm trying to create a section for variable declaration (a bit similar to HTML) using Flex and Bison, my grammar is correct (no lexical or syntax errors), but the displayed result isn't ordered.

example.txt:

    <SUB VARIABLE>
        < a AS INT />;
        <string AS STR />; 
        < x | y AS FLT />;
        <bool AS BOL />; 
        <char AS CHR />; 
    </SUB VARIABLE>
    

the result I get (the incorrect one):

a ---> 1
x ---> 2
y ---> 2
string ---> 4
char ---> 3
bool ---> 5

the result I want to display (the correct one):

a ---> 1
string ---> 4
x ---> 2
y ---> 2
bool ---> 5
char ---> 3

Here's my code:

synt.y:

DECLARATION:    DECLARATION     '<' SUB VARIABLE '>'    SUITE  
            
            |
;


SUITE: '<' idf  SUITE_VAR {inserer($2,getType());}
            | '<' '/' SUB VARIABLE '>' 
;

SUITE_VAR:      '|' idf  SUITE_VAR {inserer($2, getType());}
            |   AS INT '/' '>' ';' SUITE {setType(1);}
            |   AS FLT '/' '>' ';' SUITE {setType(2);}
            |   AS CHR '/' '>' ';' SUITE {setType(3);}
            |   AS STR '/' '>' ';' SUITE {setType(4);}
            |   AS BOL '/' '>' ';' SUITE {setType(5);}
;


My grammar may be ambiguous, I tried many other grammars but I had the same problem.
Could you please tell me how I should write my grammar to have an ordered result?
Thanks a lot.

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

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

发布评论

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

评论(1

成熟的代价 2025-01-29 04:34:39

这不是您的语法错误。这是您可能遇到问题的inserer功能的语义。

This is not a mistake in your grammar. It's the semantics of the inserer function where you probably have an issue.

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