Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

compiler construction - A weird situation about matching in flex

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

%%
"int" printf("JUST_INT");
"int"[ 	
]+"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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not a good idea to have rules that scan across spaces like "int"[ ]+"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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...