180 likes | 341 Views
611 SET 5. SEARCHING & SORTING IN C plus HANDLING COMMENTS IN LEX. BINARY SEARCH C provides the bsearch function for this purpose. It’s specified in STDLIB.H. The items to be searched may be an array of strings in alphabetic order, e.g. Names[8] [6] =
E N D
611 SET 5 SEARCHING & SORTING IN C plus HANDLING COMMENTS IN LEX
BINARY SEARCH C provides the bsearch function for this purpose. It’s specified in STDLIB.H. The items to be searched may be an array of strings in alphabetic order, e.g. Names[8] [6] = {“Able “, “Betty”, “Cathy”, “David”, “Ed“, “Mary “, “Peter”, “Sean”}; where the no. of strings is 8, and the largest string is of length ≤ 6.
bsearch makes use of an auxiliary function that you have to supply. It’s purpose in this case is to determine which of two strings occurs before the other in an alphabetic ordering.
A suitable function is the following: int compare_strings(const void *name1, const void *name2) return(strcmp((char *) name1, (char *) name2));
Now we can search the list of 8 names, to see whether a string “Somebody” occurs in it, using: bsearch(Somebody, (void *) Names, 8, 6, compare_strings)); It returns a value of NULL if the search is unsuccessful.
SORTING VIA QUICKSORT Assume the contents of “Names” are rearranged so that they are not in alphabetic order. We can here employ the C function qsort to sort them into alphabetic order. It too is specified in STDLIB.H.
The sort is accomplished, using: qsort((void *) Names, 8, 6, compare_strings) As before, this sorts the list “Names” of 8 strings, the largest of which is of length ≤ 6, using the same compare_strings function described above.
Homework Write a program in C which contains the declaration: Names[8] [6] = {“Betty“, “David”, “Ed”, “Sean”, “Peter“, “Mary“, “Able”, “Cathy”}; and …
Employs qsort to sort “Names” into alphabetic order And then uses bsearch to determine whether or not an inputted string occurs in Names. It should write out the verdict on the screen. 3. The program should bypass comments & quoted strings using the method on the slides following.
THREE MORE CONSTRUCTS IN LEX 1. Negative character classes. These resemble character classes, except that the first symbol is “^”. A negative character class such as e.g. [^b3-7] matches any single character exceptb,3,4,5,6,7.
Exclusive start conditions (implemented in Flex, but not every version of Lex). These specify additional conditions for matching a string. One defines a name for a start condition, e.g. “special”, by including the following statement at the end of the first section of the Lex definition file (i.e. after the macro definitions, if any): %x special
One activates such a start condition, by including in one’s C code BEGIN(special); and deactivates it with the C code BEGIN(0);
While the start condition “special” is active, only recursive expressions prefixed by <special> are matched against the source by Lex Example. %x special %% [1-9]+ sum = 1; Mary BEGIN(special); <special>Henry sum = 2; <special>Marge BEGIN(0);
Here the start condition “special” becomes active when “Mary” is matched against the source, and inactive when “Marge” is matched against the source. While the start condition is not active, Lex will attempt to match [1-9]+ against the source, but will not attempt to match “Henry”. While the start condition is active, Lex will attempt to match “Henry”, but not attempt to match [1-9]+
THE PROBLEM WITH COMMENTS C, and several other languages, allow multi-line comments that begin with “/*” and endup with “*/” . One could skip over such comments, by providing a r.e. for them, with no C code associated, such as; “/*”([^*]*(“*”[^“/”])*)*“*/” ;
The problem with this is that the entire comment will be placed in yytext. Some comments are pages long, and may exceed the maximum size for yytext, causing error. A way around this is via the use of an exclusive start condition, active only while within the comment.
Here is an example of the code required to bypass comments, without the risk of overflow: %x comment %% “/*” BEGIN(comment); <comment>“*/” BEGIN(0); <comment>. ; <comment>\n ++line_no; followed by r.e.’s without the prefix
ECHO is a built-in function that you can use in the C code associated with regular expressions. It writes out onto the standard output file the contents of yytext. For example: %% [1-9]+ {ECHO; … return digit;}