home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / compiler / 1937 < prev    next >
Encoding:
Text File  |  1992-11-22  |  2.4 KB  |  77 lines

  1. Newsgroups: comp.compilers
  2. Path: sparky!uunet!world!iecc!compilers-sender
  3. From: eifrig@beanworld.cs.jhu.edu (Jonathan Eifrig)
  4. Subject: Re: In lex, how do I begin in a state??
  5. Reply-To: eifrig@beanworld.cs.jhu.edu (Jonathan Eifrig)
  6. Organization: The Johns Hopkins University CS Department
  7. Date: Sun, 22 Nov 1992 22:25:44 GMT
  8. Approved: compilers@iecc.cambridge.ma.us
  9. Message-ID: <92-11-129@comp.compilers>
  10. References: <92-11-122@comp.compilers>
  11. Keywords: lex
  12. Sender: compilers-sender@iecc.cambridge.ma.us
  13. Lines: 62
  14.  
  15. James A. Cadwell <cadwell@seattleu.edu> writes:
  16. >In lex, one uses BEGIN STATE-NAME in an action to place lex in a
  17. >state.  My question is: how do I begin in a state?? That is, have
  18. >a state in effect before any input is read.
  19.  
  20. The Moderator adds:
  21. >[In short, you have to execute a BEGIN before the lexer starts scanning.
  22. >Code put at the front of the rules section is run whenever you call yylex(),
  23. >so you could do something like this:
  24. >
  25. >%%
  26. >%{
  27. >    static int first_time = 1;
  28. >
  29. >    if(first_time) {
  30. >        BEGIN FOOSTATE;
  31. >        first_time = 0;
  32. >    }
  33. >...
  34. >
  35. >The new version of O'Reilly's lex&yacc, much of which I wrote, explains this
  36. >in more detail. -John]
  37.  
  38.     This is, of course, the right way to do things.  However, if
  39. you're in a hurry, or are writing a lex-only program (using the _main in
  40. the lex library), one can use the undocumented start state INITIAL.  This
  41. is probably a Bad Idea as far as portability goes, but if you're doing
  42. something simple like stripping comments with a two-line lexer, it's
  43. probably sufficient.
  44.  
  45.     As far as the Moderator's suggestion goes, it's probably better to
  46. put the lexer into a state by hand before calling yylex() by adding a
  47. special initialization routine in the "user subroutines" section of the
  48. lex file, after the rules:
  49.  
  50. <START>.....
  51. ...
  52. %%
  53. int
  54. kick_start_lexer() { BEGIN FOOSTATE; }
  55.  
  56. and then manually initialize the lexer before calling yylex():
  57.  
  58. extern int kick_start_lexer();
  59.  
  60. int
  61. main()
  62. {
  63.    ...
  64.    kick_start_lexer();
  65.    if (yyparse())) ...
  66.    ...
  67. }
  68.  
  69.     This way, the test to see if the lexer is being called for the
  70. first time is eliminated.  Hey, it's only one instruction, but that's one
  71. comparison _per_token_.  These things start to add up after a while!  :-)
  72. --
  73. Jack Eifrig (eifrig@cs.jhu.edu)       The Johns Hopkins University, C.S. Dept.
  74. -- 
  75. Send compilers articles to compilers@iecc.cambridge.ma.us or
  76. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  77.