PLEASE READ MY NOTE FIRST. Note: The following are the contents of a flex file that will be used to implement a lexical analyzer for a subset of the C programming language. I cannot understand anything in the code given below, so I need your help. I need you to explain every section of the code to me step-by-step in detail. At the end, please summarize what the code is all about and how it is functioning. Here's the code from the Flex file: %option noyywrap %{ #include using namespace std; ofstream yyoutlog; //output filestream for log file string loglist = ""; //list of tokens along with line numbers // define necessary c++ variables and functions here (if any) int lines = 1; %} /* Regular Definitions */ delim [ \t\v\r] /* write regular expressions for whitespace and newline */ ws newline letter_ [A-Za-z_] digit [0-9] /* write regular expressions for id, float and integers */ id %% {ws} { /* ignore whitespace */ } {newline} { /* do necessary bookkeeping (line number tracking etc.) */ } {id} { loglist="Line no "+to_string(lines)+": Token Lexeme "+yytext+" found"+"\n"+"\n"; yyoutlog< Lexeme "+yytext+" found"+"\n"+"\n"; yyoutlog< Lexeme "+yytext+" found"+"\n"+"\n"; yyoutlog< Lexeme "+yytext+" found"+"\n"+"\n"; yyoutlog<
PLEASE READ MY NOTE FIRST. Note: The following are the contents of a flex file that will be used to implement a lexical analyzer for a subset of the C programming language. I cannot understand anything in the code given below, so I need your help. I need you to explain every section of the code to me step-by-step in detail. At the end, please summarize what the code is all about and how it is functioning. Here's the code from the Flex file: %option noyywrap %{ #include using namespace std; ofstream yyoutlog; //output filestream for log file string loglist = ""; //list of tokens along with line numbers // define necessary c++ variables and functions here (if any) int lines = 1; %} /* Regular Definitions */ delim [ \t\v\r] /* write regular expressions for whitespace and newline */ ws newline letter_ [A-Za-z_] digit [0-9] /* write regular expressions for id, float and integers */ id %% {ws} { /* ignore whitespace */ } {newline} { /* do necessary bookkeeping (line number tracking etc.) */ } {id} { loglist="Line no "+to_string(lines)+": Token Lexeme "+yytext+" found"+"\n"+"\n"; yyoutlog< Lexeme "+yytext+" found"+"\n"+"\n"; yyoutlog< Lexeme "+yytext+" found"+"\n"+"\n"; yyoutlog< Lexeme "+yytext+" found"+"\n"+"\n"; yyoutlog<
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
100%
PLEASE READ MY NOTE FIRST.
Note: The following are the contents of a flex file that will be used to implement a lexical analyzer for a subset of the C
Here's the code from the Flex file:
%option noyywrap
%{
#include<bits/stdc++.h>
using namespace std;
ofstream yyoutlog; //output filestream for log file
string loglist = ""; //list of tokens along with line numbers
// define necessary c++ variables and functions here (if any)
int lines = 1;
%}
/* Regular Definitions */
delim [ \t\v\r]
/* write regular expressions for whitespace and newline */
ws
newline
letter_ [A-Za-z_]
digit [0-9]
/* write regular expressions for id, float and integers */
id
%%
{ws} { /* ignore whitespace */ }
{newline} {
/* do necessary bookkeeping (line number tracking etc.) */
}
{id} {
loglist="Line no "+to_string(lines)+": Token <ID> Lexeme "+yytext+" found"+"\n"+"\n";
yyoutlog<<loglist;
}
if {
loglist="Line no "+to_string(lines)+": Token <IF> Lexeme "+yytext+" found"+"\n"+"\n";
yyoutlog<<loglist;
}
while {
loglist="Line no "+to_string(lines)+": Token <WHILE> Lexeme "+yytext+" found"+"\n"+"\n";
yyoutlog<<loglist;
}
"+"|"-" { /* to match two same operators use | */
loglist="Line no "+to_string(lines)+": Token <ADDOP> Lexeme "+yytext+" found"+"\n"+"\n";
yyoutlog<<loglist;
}
%%
int main(int argc, char *argv[])
{
if(argc != 2) //check if file name is given
{
cout<<"Please input file name"<<endl;
return 0;
}
yyin = fopen(argv[1], "r"); //the file to be read must be assigned to yyin
yyoutlog.open("my_log.txt", ios::trunc); // remember to rename the log file as mentioned in the spec
if(yyin == NULL) // file does not exist
{
// print error message and exit
}
yylex(); // start reading the input file in a loop
//print total number of lines found in the input file
yyoutlog.close(); // close the log file
fclose(yyin); // close the input file
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education