home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13389 < prev    next >
Encoding:
Internet Message Format  |  1992-09-08  |  1.9 KB

  1. Path: sparky!uunet!gatech!usenet.ins.cwru.edu!agate!ucbvax!amex-trs.com!gonzalod
  2. From: gonzalod@amex-trs.com (Gonzalo Diethelm)
  3. Newsgroups: comp.lang.c++
  4. Subject: yacc and C identifiers
  5. Message-ID: <9209050134.AA43930@cs90code.csv-tgsc.amex-trs.com>
  6. Date: 5 Sep 92 01:34:52 GMT
  7. Sender: daemon@ucbvax.BERKELEY.EDU
  8. Lines: 60
  9.  
  10.  
  11.  
  12. Distribution: world
  13.  
  14. Hello everyone,
  15.  
  16. Two questions about yacc. Assume I'm trying to implement a C compiler:
  17.  
  18. 1. How do you handle identifiers? By this I mean, let's say the
  19.    lexer stores identifiers in the symbol table and returns an ID token
  20.    to the parser, how do I access the identifier from the parser?
  21.    What I'm doing now is having a production
  22.  
  23.      id : ID
  24.           {
  25.             $$ = yylval;
  26.           }
  27.  
  28.    where the lexer sets yylval to the position in the symbol table
  29.    the identifier is stored in. This works, but I'm not sure it is
  30.    the correct approach. I know I could define the type of the parser
  31.    stack to be a union and store there the value of the identifier
  32.    (position in symbol table?). Which approach is better? Is there
  33.    another solution superior to any of these?
  34.  
  35. 2. How would you handle name spaces? For example, how could you differentiate
  36.    between the var in
  37.  
  38.      int   var;
  39.  
  40.   and the var in
  41.  
  42.      struct s { int var; };
  43.  
  44.   or, even better, between all these vars
  45.  
  46.     struct s1
  47.     {
  48.       int var;
  49.       struct s2
  50.       {
  51.         int var;
  52.         union  u1
  53.         {
  54.           int var;
  55.           float f;
  56.         }
  57.       }
  58.     }
  59.  
  60.   I CAN think of ways to do this, but somehow I feel the parser itself
  61.   has enough information stored in its states to handle the name spaces.
  62.  
  63. Any pointers are welcome. Examples are preferred... 8^). Thanks in advance.
  64.  
  65.  
  66. gonzalod@amexphx.amex-trs.com
  67. Gonzalo A. Diethelm                 (602)375-8410  home
  68. 16220 N 7th St Ap 1314              (602)492-3536  office
  69. Phoenix, AZ 85022                   (602)492-4248  FAX
  70.