1

Please i am trying to pass the yyleng of a matched string from my (.l) file to the (.y) file. Here is a sample of the issue:

In the Lex File:

<state1>.+    {   fprintf(yyout, "%d", yyleng);    } 

In the Yacc File:

/* I need to know the methodology used to receive a specific yyleng to the yacc file. Shall I use global variables? or there is a specific way for dealing with this issue? */

Thanks in advance for your help! ~ Any suggestions are highly appreciated.

CompilingCyborg
  • 4,760
  • 13
  • 44
  • 61
  • See near duplicate [question](http://stackoverflow.com/questions/4514075/how-to-pass-the-yytext-from-the-lex-file-to-yacc) (asking about `yytext` instead of `yyleng`): – Jonathan Leffler Dec 23 '10 at 13:27

2 Answers2

3

yyleng is a global variable; declare it in your grammar file and use it.

Flex uses:

typedef size_t yy_size_t;
extern yy_size_t yyleng;

Lex uses:

extern int yyleng;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thanks so much for your help! Please if i need to learn more about using yytype, yyleng and others, do you recommend me any specific free online resources (especially if there is any for beginners)? – CompilingCyborg Dec 23 '10 at 13:49
  • 1
    I learned Lex and Yacc from the [UNIX 7th Edition](http://plan9.bell-labs.com/7thEdMan) manuals - at a time when that was only 5 years or so old. I learned a whole lot more from the O'Reilly "Lex and Yacc" book (1st and 2nd editions - the new (third) edition is called [Flex and Bison](http://oreilly.com/catalog/9780596155988)). I find that Google is helpful when I search for 'yacc tutorial' - most of the tutorials seem to cover Lex too, and/or are for Flex and Bison too. – Jonathan Leffler Dec 23 '10 at 13:58
  • I greatly appreciate your time and help! ~ Thanks so much for taking the time and answering my basic question :) – CompilingCyborg Dec 23 '10 at 14:00
1

Define you own yytype and pass any values over it

Dewfy
  • 23,277
  • 13
  • 73
  • 121