Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does return yytext[0] do?

Tags:

flex-lexer

lex

In this lex part of a lex-yacc program what is the purpose of adding the lines

. return yytext[0];
\n return yytext[0];

This the lex part

%{
#include "y.tab.h"
%}
%%
a return A;
b return B;
. return yytext[0];
\n return yytext[0];
%%

What does it return when it encounters \n ?

like image 746
Khacho Avatar asked Sep 10 '26 06:09

Khacho


1 Answers

Not sure why Ajay2707 posted a comment and not an answer, because he is right. According to http://dinosaur.compilertools.net/flex/manpage.html yytext is a string containing the token matched by flex. Taking [0] takes the first character. So

  • . return yytext[0]; passes through any character except A, B, and \n
  • \n return yytext[0]; passes through the \n character

This is because the pattern '.' does not match \n

To put it short this lexer changes a and b to upper caps, and nothing else.

like image 188
hjohanns Avatar answered Sep 12 '26 19:09

hjohanns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!