由于使用Flex和Bison具有模棱两可的语法,因此无序的结果
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是您的语法错误。这是您可能遇到问题的
inserer
功能的语义。This is not a mistake in your grammar. It's the semantics of the
inserer
function where you probably have an issue.