在列表中添加自定义语言导致关键字问题

在列表中添加自定义语言导致关键字问题

我正在尝试通过定义如下语言来为 ARM 汇编制作自己的语法突出显示:

\lstdefinelanguage{ASM}{
    morekeywords={b, ble, blt, bne, 
        ldr, str, 
        r0, r1, r2, r3, r4, r5, r6, r7, rr8, r9, r10, r11, r12
        sp  },
    sensitive=false, % keywords are not case-sensitive
    morecomment=[l]{//}, % l is for line comment
    morecomment=[s]{/*}{*/}, % s is for start and end delimiter
    morestring=[b]" % defines that strings are enclosed in double quotes
} % 

但是当我这样做时:

\lstset{language=ASM}
\begin{lstlisting}[style = ASM]
    .section .init
    .global _start

    _start:
        ldr sp, =8000
        b kernel_main
\end{lstlisting}

我被告知“未定义的控制序列 ldr”和“未定义的控制序列 b”。从关键字中删除它们可以解决问题,但我没有语法突出显示。我做错了什么?

答案1

查看您所拥有的内容,您似乎在行尾缺少逗号...r11, r12,并且您的lstlisting环境应该是\begin{lstlisting}[language = ASM],而不是使用style。通过这两个修复,您的代码会产生:

在此处输入图片描述

以下是完整内容最小工作示例

\documentclass{article}
\usepackage{listings}

\lstdefinelanguage{ASM}{
    morekeywords={b, ble, blt, bne,
        ldr, str,
        r0, r1, r2, r3, r4, r5, r6, r7, rr8, r9, r10, r11, r12,
        sp  },
    sensitive=false, % keywords are not case-sensitive
    morecomment=[l]{//}, % l is for line comment
    morecomment=[s]{/*}{*/}, % s is for start and end delimiter
    morestring=[b]" % defines that strings are enclosed in double quotes
} %

\begin{document}

\begin{lstlisting}[language = ASM]
    .section .init
    .global _start

    _start:
        ldr sp, =8000
        b kernel_main
\end{lstlisting}

\end{document}

相关内容