Xref: utzoo comp.lang.c:38666 comp.unix.questions:30693 comp.unix.wizards:25105 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!bsg!pg From: pg@bsg.com (Peter Garst) Newsgroups: comp.lang.c,comp.unix.questions,comp.unix.wizards Subject: Re: lex and yacc help desired Message-ID: <1991Apr24.181143.3307@bsg.com> Date: 24 Apr 91 18:11:43 GMT References: <1991Apr23.164744.25927@mnemosyne.cs.du.edu> Sender: Peter Garst Organization: Bloomsbury Software Group Lines: 37 In article <1991Apr23.164744.25927@mnemosyne.cs.du.edu> awade@isis.cs.du.edu (allen wade) writes: > >I am able to figure out the regular expressions for Lex, but I am confused >about how Lex and Yacc work together. > When you are writing a lex/yacc interface, the work is on the lex side; the yacc side is hidden in the code generated by yacc. On the lex side, you generally need to do two things when you have found a token that you want to pass to yacc: 1) set the variable "yylval" to the value of the token, and 2) return the macro defining the token number. For example, if you have recognized an integer and want to pass it to yacc, you will do this in a lex action: yylval = atoi(yytext); /* Set yylval to the number found */ return(NUMBER); /* Tell yacc you found a number */ In your grammar file you can then get the value of the number with one of the $n variables in an action associated with a rule. The easiest way to make sure NUMBER is defined is to include the scanner generated by lex in the last part of the yacc grammar file. You can use "#include "put-your-scanner-name-here"" after the second %% in the yacc file. The the "%token NUMBER" in the declarations section will define NUMBER for your scanner. Debugging yacc grammars and parsers can be difficult and time consuming; we have a symbolic debugger (and yacc upgrade) which makes it much easier. See our recent comp.newprod posting, or get in touch for more information. Peter Garst pg@bsg.com Bloomsbury Software Group, PO Box 390018, Mountain View, CA 94039 (415) 964-3486