我正在尝试将 flex 读取的代码插入到我的 .tex 文件中,这个控制台应用程序应该采用 .pascal 并对其进行分析,然后生成一个 .tex 文件,但我无法将代码传递给 . tex 文件,然后我需要为它读取的每个标记添加颜色,我需要帮助!编译它的命令 flex file.l, g++ lex.yy.c, ./a.out, test.pascal, pdflatex PDF.tex
%{
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int token_if = 0;
%}
%%
if ++token_if;
then|begin|end|procedure|function {
printf( "A keyword: %s\n", yytext );
}
%%
int main(int argc, char *argv[])
{
//read file
FILE *fp;
char filename[50], c;
printf("Enter the filename: \n");
scanf("%s",filename);
fp = fopen(filename,"r");
if (fp == NULL)
{
printf("file null");
exit(0);
} else
{
yyin = fp;
//start of lex
yylex();
// counter
//get_token(token_if);
//create latex file
ofstream myPDF("PDF.tex");
myPDF << " \\documentclass{article} "
<< "\\title{Scanner}"
<< "\\author{Andres}"
<< "\\date{III}"
<< " \\begin{document} "
<< "\\maketitle"
<< "\\newpage"
<< "\\section{¿Q?}"
<< " number of if's "
<< token_if
<< " \\end{document} ";
myPDF.close();
}
// printf("# of if's = %d", token_if);
return 0;
}