On Wed, 22 July, 1998 at 09:16:38AM +0800, Luke Thin wrote: > Hi Ali, > > Do you have any books whatsoever on Lex and Yacc? I am doing compiler > design and I am already lost on the first week :(( Ahh! It's my old nemeses, Lex and Yacc! [runs for the hills] But seriously folks, these are two things which you will really appreciate once you know about them. Sorry, I don't have any books on them, but feel free to ask me for advice, starting points, whatever. I'll begin by saying that lex is like awk on drugs. It takes something resembling an awk program file, (except that it's in two or three sections and the actions are written in C code) and produces a C source code file containing a function called yylex(). This function reads from standard input (by default), scanning for all the patterns and as it encounters them, it executes the associated actions. The real power of lex is apparent when it is used in combination with yacc. Instead of just doing stuff and writing text to the terminal when pattens are matched, yylex() returns a code, or "token ID", and perhaps (indirectly) a specific string. The thing which repeatedly calls yylex() and actually makes use of the token IDs is the yyparse function, which is in a .c file which was produced from a .y file by yacc. This .y file contains a bunch of rules, much like those in a makefile, which indicate how various syntactic groups are made up. There can be alternatives, e.g.: thingy: TOKEN1 { printf( "simple\n" ); } | TOKEN2 chunk TOKEN3 { printf( "complex\n" ); } ; chunk: TOKEN4 { printf( "small part\n" ); } As you can see, it's pretty complex, but the principle is that it continually gathers tokens and repeatedly cycles through the targets, iterating through the dependency trees which make them up, until it finds one which matches some of the targets and tokens in the input queue. It then executes the appropriate block associated with the dependency list and replaces the matched tokens & targets with the target which represents them in the rule. -- _______________________________________________________________________ | | | -=*Alastair Irvine*=- WWW page: http://members.xoom.com/alastair/ | | job-hunting Software Engineer Magic:tG player, RPGer, net-nut | | e-mail: alastair@ucc.gu.uwa.edu.au or alastair.irvine@usa.net | |_______________________________________________________________________|