A weird situation about matching in flex

折月煮酒 提交于 2019-12-13 23:50:23

问题


I am writing a scanner in flex, and I have the following two definitions:

%%
"int" printf("JUST_INT");
"int"[ \t\n]+"matrix" printf("MATRIX_INT");
[A-Za-z][A-Za-z0-9]*  printf("IDENTIFIER");
%%

When the input to the scanner is int matrixM = 3; the output is MATRIX_INT IDENTIFIER. It sees the input as int matrix M=3. But actually, this is not a matrix, the name of identifier is matrixM. Output should be JUST_INT IDENTIFIER. Why is this happening? Is that because of my definitions?


回答1:


It's not a good idea to have rules that scan across spaces like "int"[ \t\n]+"matrix". Just return "int", "matrix", and IDENTIFIER as three separate tokens and let the parser sort it out. It's much better at that than flex is. flex just does the first longest match.




回答2:


If you really want to solve this at the scanner level, you can match on

"int"[ \t\n]+"matrix"/($|[ \t\n]|;|whateverelsedelimitsanidentifier)

This makes sure that matrix is only matched if it is followed by a "non-word". As EJP said, it is not a good idea however and you should do this very probably at the parser level instead.



来源:https://stackoverflow.com/questions/15398098/a-weird-situation-about-matching-in-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!