Skip to content
Related Articles

Related Articles

Improve Article
LEX code to extract HTML tags from a file
  • Difficulty Level : Basic
  • Last Updated : 28 Aug, 2019

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




/* 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;
}

Output :

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :