Khacho
Khacho

Reputation: 301

What does return yytext[0] do?

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 ?

Upvotes: 3

Views: 4042

Answers (1)

hjohanns
hjohanns

Reputation: 96

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.

Upvotes: 2

Related Questions