I am presently learning Lexical Analysis in Compiler Design. In order to learn how really a lexical analyzer works I am trying to build one myself. I am planning to build it in Java.
The input to the lexical analyzer is a .tex file which is of the following format.
\begin{document}
\chapter{Introduction}
\section{Scope}
Arbitrary text.
\section{Relevance}
Arbitrary text.
\subsection{Advantages}
Arbitrary text.
\subsubsection{In Real life}
\subsection{Disadvantages}
\end{document}
The output of the lexer should be a table of contents possibly with page numbers in another file.
1. Introduction 1
1.1 Scope 1
1.2 Relevance 2
1.2.1 Advantages 2
1.2.1.1 In Real Life 2
1.2.2 Disadvantages 3
I hope that this problem is within the scope of the lexical analysis.
My lexer would read the .tex file and check for '\' and on finding continues reading to check whether it is indeed one of the sectioning commands. A flag variable is set to indicate the type of sectioning. The word in curly braces following the sectioning command is read and written along prefixed with a number (like 1.2.1) depending upon the type and depth.
I hope the above approach would work for building the lexer. How do I go about in adding page numbers to the table of contents if that's possible within the scope of the lexer?