<p>In the syntax highlighter I have implemented in my <a href="https://flatassembler.github.io/PicoBlaze/PicoBlaze.html" rel="nofollow noreferrer">PicoBlaze Simulator in JavaScript</a>, the assembly code</p>
<pre><code>address 0
load s0, 0
load s0, a
regbank a
</code></pre>
<p>is highlighted as follows:</p>
<img src="https://user-images.githubusercontent.com/46264381/253376969-7aeeb1d4-2a65-433b-a9fd-c19fb5907812.png">
<p>But it shouldn't be. The <code>a</code> in <code>load s0, a</code> is a hexadecimal constant, and it should be highlighted the same way <code>0</code> is.</p>
<p>How could I implement this? In general, I don’t know how I should reliably distinguish uses of <code>a</code> as a hexadecimal constant (rather than belonging to some other class of identifier).</p>

<parsing><assembly><syntax-highlighting>
How do I explain to a syntax highlighter to highlight `a` differently in `load s0, a` (where it is a hexadecimal constant) and `regbank a`?
CC BY-SA 4.0
2023-07-13T18:57:41.617Z
I feel like this question belongs on StackOverflow, not here.
Bbrk24
CC BY-SA 4.0
2023-07-14T08:57:29.180Z
Do not tokenise then, use a proper parser and do semantic highlighting. PEG would have been a good choice here.
SK-logic
CC BY-SA 4.0
2023-07-14T11:57:33.677Z
Highlighting based on context might make your parser more complex. Other languages have special characters to tokenize hexadecimals, like `0xa` in `C`, although that might change how your language looks.
Tom Tsagkatos
CC BY-SA 4.0
2023-07-14T12:27:12.357Z
@TomTsagk I need to maintain the compatibility with the official Xilinx'es assembler for PicoBlaze.
FlatAssembler
CC BY-SA 4.0
2023-07-14T17:03:15.417Z
I'm voting to reopen this. On second thought, and considering our consensus regarding tooling other than compilers, I think this is on-topic, as it involves similar considerations to those which would be needed when parsing or building an AST.
rydwolf
<p>Your question doesn’t really describe what your actual challenge is. Where did you get stuck? Why are you unsure how to implement the behavior you’re looking for? In the absence of that, I have to guess.</p>
<p>However, a standard approach here would be to write a very simple parser for your language, then perform syntax coloring based on the parsed instructions. If, for example, the <a href="https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form" rel="noreferrer">EBNF grammar</a> of your language is something like</p>
<pre><code>&lt;immediate&gt;   ::= &lt;hex digit&gt;+
&lt;register&gt;    ::= 's0' | 's1' | 's2' | 's3'

&lt;instruction&gt; ::= 'address' &lt;immediate&gt;
               |  'load' &lt;register&gt; ',' &lt;immediate&gt;
               |  'regbank' &lt;immediate&gt;
               |  ...

&lt;program&gt;     ::= &lt;instruction&gt;*
</code></pre>
<p>then the parse tree produced by parsing your program will provide the information you need to perform syntax coloring. The parser itself could be implemented via any of the usual parsing techniques, but you could also use something like <a href="https://tree-sitter.github.io/tree-sitter/" rel="noreferrer">tree-sitter</a>, which is specifically designed for this sort of task.</p>
<p>Another, simpler option would be to do ad-hoc analysis using regular expressions, which is possible if your language is a <a href="https://en.wikipedia.org/wiki/Regular_language" rel="noreferrer">regular language</a>. Most programming languages aren’t, but many assembly languages are, so you might be able to get away with a simpler implementation strategy.</p>

CC BY-SA 4.0
2023-07-14T17:41:14.197Z
2023-07-14T17:41:14.197Z
Alexis King
# How do I explain to a syntax highlighter to highlight `a` differently in `load s0, a` (where it is a hexadecimal constant) and `regbank a`?
Posted by: username_0 on 2023-07-13
In the syntax highlighter I have implemented in my PicoBlaze Simulator in JavaScript, the assembly code
address 0
load s0, 0
load s0, a
regbank a

is highlighted as follows:

But it shouldn't be. The a in load s0, a is a hexadecimal constant, and it should be highlighted the same way 0 is.
How could I implement this? In general, I don’t know how I should reliably distinguish uses of a as a hexadecimal constant (rather than belonging to some other class of identifier).
**Tags:** parsing assembly syntax-highlighting
> **Comments:**
> **username_1**: I feel like this question belongs on StackOverflow, not here.  
> **username_2**: Do not tokenise then, use a proper parser and do semantic highlighting. PEG would have been a good choice here.  
> **username_3**: Highlighting based on context might make your parser more complex. Other languages have special characters to tokenize hexadecimals, like `0xa` in `C`, although that might change how your language looks.  
> **username_0**: @TomTsagk I need to maintain the compatibility with the official Xilinx'es assembler for PicoBlaze.  
> **username_4**: I'm voting to reopen this. On second thought, and considering our consensus regarding tooling other than compilers, I think this is on-topic, as it involves similar considerations to those which would be needed when parsing or building an AST.  
---
## 1 answers
### Answer by username_5 on 2023-07-14. Score: 5 | Accepted answer
Your question doesn’t really describe what your actual challenge is. Where did you get stuck? Why are you unsure how to implement the behavior you’re looking for? In the absence of that, I have to guess.
However, a standard approach here would be to write a very simple parser for your language, then perform syntax coloring based on the parsed instructions. If, for example, the EBNF grammar of your language is something like
   ::= +
    ::= 's0' | 's1' | 's2' | 's3'

 ::= 'address' 
               |  'load'  ',' 
               |  'regbank' 
               |  ...

     ::= *

then the parse tree produced by parsing your program will provide the information you need to perform syntax coloring. The parser itself could be implemented via any of the usual parsing techniques, but you could also use something like tree-sitter, which is specifically designed for this sort of task.
Another, simpler option would be to do ad-hoc analysis using regular expressions, which is possible if your language is a regular language. Most programming languages aren’t, but many assembly languages are, so you might be able to get away with a simpler implementation strategy.
---
