1 / 18

611 SET 5

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] =

lamont
Download Presentation

611 SET 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 611 SET 5 SEARCHING & SORTING IN C plus HANDLING COMMENTS IN LEX

  2. 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.

  3. 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.

  4. A suitable function is the following: int compare_strings(const void *name1, const void *name2) return(strcmp((char *) name1, (char *) name2));

  5. 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.

  6. 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.

  7. 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.

  8. Homework Write a program in C which contains the declaration: Names[8] [6] = {“Betty“, “David”, “Ed”, “Sean”, “Peter“, “Mary“, “Able”, “Cathy”}; and …

  9. 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.

  10. 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.

  11. 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

  12. One activates such a start condition, by including in one’s C code BEGIN(special); and deactivates it with the C code BEGIN(0);

  13. 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);

  14. 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]+

  15. 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; “/*”([^*]*(“*”[^“/”])*)*“*/” ;

  16. 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.

  17. 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

  18. 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;}

More Related