简体   繁体   中英

Creating lexical analyzer using flex

As a school assignment, I am creating a Lexical Analyzer using FLEX. The analyzer is used by a parser, which is also a part of the assignment. We are supposed to use lexical analyzer and the parser for a language called VSL. However, I can't seem to get the analyzer to work.

I will use some keywords from VSL as example: FUNC, PRINT and IF.

Here is the current output from the rules section:

"FUNC"      {return FUNC;}
"START"     {return START;}
"PRINT"     {return PRINT;}

.           { RETURN( yytext[0] ); }

The . simply returns every character one by one when not matching any other rule, and was provided in the near-empty file from the university. The tokens to be returned are specified in the provided parser, and the three used above are among those. I have determined from the test outputs (generated by tools also provided) that FUNC, START and PRINT is recognized, but they do not show up in the test outputs, and apparently they are not passed on. I have the same problem with the variables (strings, digits, etc.) They consist of digits and/or letters, which are handled in the rules (should not be necessary to show them as well). What am I doing wrong? What is my next step?

If I remove every single rule I have made, and let the last rule with the . remain, then all letters and digits become visible in the test output, so I know that the lexer recognizes them at least. But what is the next step?

It turns out that the gcc compiler on my Mac OS X Mavericks has different behaviour than GNU gcc compiler that I have access to online through ssh. The local compiler ignored errors generated by the online compiler, as well as having own local errors not relevant to the GNU gcc. So part of the problam was confusion on my part.

It also turns out that this is the correct code:

"FUNC"      {RETURN(FUNC);}
"START"     {RETURN(START);}
"PRINT"     {RETURN(PRINT);}

.           { RETURN( yytext[0] ); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM