3

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 ?

Khacho
  • 199
  • 3
  • 16
  • 2
    Possible duplicate of [What is the meaning of yytext\[0\]?](http://stackoverflow.com/questions/33842818/what-is-the-meaning-of-yytext0) – rici Nov 22 '15 at 19:36
  • Thanks for the reply sir, I looked into the answer on the link that you gave, but still it's not clear to me, can you please explain with respect an example? Thank you – Khacho Nov 22 '15 at 19:50
  • Try here: http://www.tldp.org/HOWTO/Lex-YACC-HOWTO-6.html – rici Nov 22 '15 at 20:57
  • 3
    Added comment by @VSaiNagendra "`yytext` contains text matched by the current token. It means `yytext[0]` returns the first character of the matched token." – Ajay2707 Oct 29 '18 at 11:58

1 Answers1

2

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.

hjohanns
  • 96
  • 8