YACC grammar for C language

Piet van Oostrum piet at ruuinf
Sat Jan 14 02:44:36 AEST 1989


In article <1183 at goofy.megatest.UUCP>, djones at megatest (Dave Jones) writes:
`
`I'll show you how I did it.  I'm not too happy with the solution.
`If you can think of a better way, please show me. I peeked into pcc
`to see how they did it. Believe me, you don't even want to know. (Gack.)
`
`.......
`
`  struct_specifier
`	: ntd STRUCT td IDENTIFIER '{' struct_declaration_list '}'
`					{ $$=tree(N_StructIDDef); }
`
The best way to do this is:
1. Have different tokens for IDENTIFIER and TYPEDEF names.
2. in every context where a typedef name may be used as a normal identifier
(e.g. in field names, declarations, parameters, enum lists) use the
nonterminal identifier.
3. add the production rule:
		identifier: IDENTIFIER | TYPEDEF { $$=detypedef($1); };
You use IDENTIFIER or TYPEDEF in those rules where identifier would be
ambiguous.

This is the way I find most logical, and not surprisingly this is the way
gcc does it. (If you want to see a good compiler look at gcc :=}.)
-- 
Piet van Oostrum, Dept of Computer Science, University of Utrecht
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands
Telephone: +31-30-531806. piet at cs.ruu.nl (mcvax!hp4nl!ruuinf!piet)



More information about the Comp.lang.c mailing list