LEX code to extract HTML tags from a file

Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt.
Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.
Prerequisite: Flex (Fast lexical Analyzer Generator)

Approach:
The extraction is based on the way tags are are written in HTML. All tags are included in < >. This will set the rule to extract the HTML tags from the given input file.

Input File: tags.txt

Below is the implementation to extract HTML tags from file:

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Declaration section*/
%{
%}
  
%%
"<"[^>]*> {printf("%s\n", yytext); }  /* if anything enclosed in 
                                      these < > occur print text*/
. ;  // else do nothing
%%    
  
int yywrap(){}
       
int main(int argc, char*argv[])
{
    // Open tags.txt in read mode
    extern FILE *yyin = fopen("tags.txt","r"); 
  
    // The function that starts the analysis
    yylex(); 
  
    return 0;
}

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.