You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flex C++生成代码实现着色 输出Pascal词法分析结果到LaTeX的问题

核心问题梳理

  • 现有代码仅完成了if关键字计数和少量关键字打印,没有缓存所有解析后的token内容和对应类型,无法把完整代码插入生成的.tex文件
  • 生成LaTeX时没有引入着色所需依赖包,也没有对应不同token的颜色渲染逻辑
  • 词法规则覆盖不全,遗漏了标识符、数字、运算符、注释等Pascal语法元素,也没有处理LaTeX特殊字符转义,会导致编译失败

修改后完整代码

将以下内容保存为file.l即可:

%{ 
#include <iostream>   
#include <fstream>   
#include <string>   
using namespace std;   

int token_if = 0;
// 全局缓冲区存储带颜色的代码片段
string latex_code;

// 转义LaTeX特殊字符的辅助函数
string escape_latex(string s) {
    string res;
    for (char c : s) {
        switch(c) {
            case '&': res += "\\&"; break;
            case '%': res += "\\%"; break;
            case '$': res += "\\$"; break;
            case '#': res += "\\#"; break;
            case '_': res += "\\_"; break;
            case '{': res += "\\{"; break;
            case '}': res += "\\}"; break;
            case '~': res += "\\textasciitilde{}"; break;
            case '^': res += "\\textasciicircum{}"; break;
            case '\\': res += "\\textbackslash{}"; break;
            default: res += c;
        }
    }
    return res;
}
%}

%%
/* 关键字匹配,蓝色显示 */
if                  { token_if++; latex_code += "\\textcolor{blue}{if} "; }
then|begin|end|procedure|function|var|const|type|integer|real|char|boolean|program|while|do|for|to|downto|repeat|until|case|of|else    {
    latex_code += "\\textcolor{blue}" + string("{") + escape_latex(yytext) + "} ";
}
/* 数字匹配,橙色显示 */
[0-9]+(\.[0-9]+)?   { latex_code += "\\textcolor{orange}" + string("{") + escape_latex(yytext) + "} "; }
/* 字符串匹配,绿色显示 */
'[^']*'             { latex_code += "\\textcolor{green}" + string("{") + escape_latex(yytext) + "} "; }
/* 单行注释匹配,灰色显示 */
"//".*              { latex_code += "\\textcolor{gray}" + string("{") + escape_latex(yytext) + "} "; }
/* 多行注释匹配,灰色显示 */
"{"[^}]*"}"         { latex_code += "\\textcolor{gray}" + string("{") + escape_latex(yytext) + "} "; }
/* 运算符和分隔符,紫色显示 */
[+\-*/=<>(),:;.]    { latex_code += "\\textcolor{purple}" + string("{") + escape_latex(yytext) + "} "; }
/* 标识符,黑色默认显示 */
[a-zA-Z_][a-zA-Z0-9_]*  { latex_code += escape_latex(yytext) + " "; }
/* 换行和空格,原样保留 */
[ \t\n]+            { latex_code += yytext; }
/* 剩余未匹配字符,原样输出 */
.                   { latex_code += escape_latex(string(1, yytext)); }
%%

int main(int argc, char *argv[]) 
{
    FILE *fp;
    char filename[50];
    printf("Enter the filename: \n");
    scanf("%s",filename);
    fp = fopen(filename,"r");
    if (fp == NULL)
    {
        printf("file null");
        exit(0);
    }
    yyin = fp;
    yylex();    
    
    ofstream myPDF("PDF.tex");
    myPDF  << "\\documentclass{article} "
            << "\\usepackage{xcolor}"
            << "\\usepackage{alltt}"
            << "\\title{Scanner}"
            << "\\author{Andres}"
            << "\\date{III}"
            << "\\begin{document} "
            << "\\maketitle"
            << "\\newpage"
            << "\\section{统计结果}"
            << "if 关键字的数量:" << token_if
            << "\\section{带语法高亮的Pascal代码}"
            << "\\begin{alltt}"
            << latex_code
            << "\\end{alltt}"
            << "\\end{document} ";
    myPDF.close();
    fclose(fp);
    return 0;
}

运行步骤

  • 生成词法分析代码:flex file.l
  • 编译生成可执行文件:g++ lex.yy.c -lfl -o scanner
  • 运行程序输入Pascal文件名:执行./scanner后输入test.pascal
  • 编译生成PDF:pdflatex PDF.tex

扩展说明

你可以自行修改各token对应的颜色值,xcolor包支持的基础颜色直接写名称即可,也可以自定义RGB颜色。如果需要增加更多Pascal语法的识别规则,直接在%%分隔的规则段新增匹配规则和对应颜色渲染逻辑即可。

内容的提问来源于stack exchange,提问作者AIAM2601

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 16:06:01