Open In App

LEX code to extract HTML tags from a file

Improve
Improve
Like Article
Like
Save
Share
Report

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 :


Last Updated : 28 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads