8

Is there a way for us to syntax color our answers that we use pseudocode for. Sometimes there is just not a reason to put an answer in a particular programming language. If the question doesn't specify one, oftentimes the pseudocode is more readable and can avoid the boilerplate of all programming languages.

If not, is there anything in the queue for getting that done?

12
  • 12
    "psuedocode" doesn't have any keywords so I'm not sure how you'd want to highlight it. Do you mean mart it up as if it were a specific language? (in which case this is a duplicate of this question). Commented Oct 20, 2013 at 18:55
  • 2
    No, that's not what I meant. Pseudocode does use keywords (buzzwords). If it fails to find a valid syntax engine, it could at least try coloring these words. There is foreach, obvious declarations, dot syntax, array indexing ([]) syntax. Obviously it changes from code example to code example, but that's why pseudocode syntax doesn't get colored as easily. Commented Oct 20, 2013 at 18:58
  • 2
    @MillieSmith Exactly, as you said, it changes from example code to example code. You'd need to color it especially for that one example.
    – Servy
    Commented Oct 20, 2013 at 19:07
  • 2
    I think you guys are overcomplicating this problem and dismissing it as impossible, but I guess that's the way things go :/. Commented Oct 20, 2013 at 19:10
  • 1
    You are most welcome to write a google code prettify extension that handles pseudocode and contribute it to Stack Exchange. I suggest reading: meta.stackexchange.com/questions/184108/… as well.
    – Oded
    Commented Oct 20, 2013 at 20:59
  • 1
    Have to wonder where the downvote came from. I don't care about my rep, but this is a fair question. The community seems to disagree with whether this a reasonable idea, but at least I asked. Commented Oct 20, 2013 at 21:09
  • My sudo code looks somewhat like simplified java (because thats my language of choice), other peoples sudo code will probably have different structure Commented Oct 20, 2013 at 21:12
  • 1
    @RichardTingle - your unix is showing.
    – Oded
    Commented Oct 21, 2013 at 6:29
  • 1
    I'm using pseudo-PHP and pseudo-SQL when I need it, and they are highlighted pretty well :P
    – Mołot
    Commented Oct 21, 2013 at 6:51
  • 6
    @MillieSmith Downvotes work differently here on meta. Downvotes do not necessarily mean your question lacks quality, they are rather used to express that one does not agree with your proposal. Commented Oct 21, 2013 at 7:00
  • 1
    @MillieSmith Additionally the meta community takes every feature request as a vote; unpopular requests can be -30, popular can be +60. At that scale -4 is mildly mildly unpopular Commented Oct 21, 2013 at 7:40
  • [flippant]Think and write everything in Python. Problem solved.[/flippant] Commented Sep 5, 2018 at 16:05

6 Answers 6

23

No, there isn't, partially since "pseudocode" is ill defined and can be of many different formats.

There are no plans to include anything that cannot be properly defined.

6
  • Take ** for example. That makes something bold. SO doesn't just guess that you want something bold. Perhaps if we could have something similar to surround our pseudocode with if we want it to attempt to color it. Then we look at the preview below and if we don't like it we take the highlighting out. Commented Oct 20, 2013 at 19:00
  • 14
    @MillieSmith - That would require standardizing "pseudo-code". Which kinda defeats the point of it.
    – Oded
    Commented Oct 20, 2013 at 19:03
  • 1
    Well, in general I disagree with that. Though I definitely don't think it defeats the point. Just because you take something that is fairly variable and allow a feature to "define" it when applicable, it doesn't mean you've ruined its point. The point of pseudocode is a quick writeup, not avoiding the whims of the syntax-coloring gods at all costs. (This may be taken as rude over the internet. I'm not trying to disrespect you or anyone else in any way). Commented Oct 20, 2013 at 19:08
  • 3
    @MillieSmith If you want something a bit more rigid; something more refined, then pick a language and use that. If you don't, and you'd rather use pseudocode, that's fine. One of the drawbacks to it is that it can't really be colored. The choice is yours. If you're going to define a set of rules for pseudocode then it's not longer pseudocode, it's just another language.
    – Servy
    Commented Oct 20, 2013 at 19:10
  • 1
    @MillieSmith If you define how pseudo-code should look, it's not pseudo-code anymore but Unimplemented Language X.
    – Stijn
    Commented Oct 21, 2013 at 7:14
  • 1
    You could mention <!-- language: lang-none --> for when intelligent highlighting is inappropriate.
    – philipxy
    Commented Oct 18, 2018 at 23:13
6

I'm using pseudo-PHP or pseudo-SQL when I need it, and they are highlighted pretty well.

If you really can't turn your code into actual code sample (for example, it's not feasible to write Drupal Views' filter configuration logic as an actual code), just select the language that fits your problem best (SQL in my case, with it's WHERE clausule) and make your pseudo-code similar enough to it, so the syntax highlight could work.

1
  • 1
    Good hint ! Works for me too. Commented May 28, 2022 at 1:04
4

Well, you can always use the HTML <pre> tag and then embed HTML formatting tags within it.

Here’s the Trabb Pardo–Knuth algorithm for example (slightly modified from Wikipedia):

ask for 11 numbers to be read into a sequence S
reverse sequence S
for each item in sequence S
    call a function on item, returning result
    if result overflows
        alert user
    else
        print result

The amount of formatting possible with this method is limited (the Markdown parser strips the style= attribute as usual), but for pseudocode it may be just enough.

3

Sometimes there is just not a reason to put an answer in a particular programming language. If the question doesn't specify one, oftentimes the pseudocode is more readable and can avoid the boilerplate of all programming languages.

That would require that you can kind of customize the syntax highlighter on the fly. It would also be additional work for you to define that. The current highlighter highlight.js does not allow customization on the fly (I think).

But if one of the existing supported languages matches your version of pseudocode closely why not using that and specifying that language manually in a code block.

Example:

<print "hello world">,<exit>

An alternative would be to use the CSS classes of the highlighter manually in code blocks like for example

<span class="str">"literal"</span>

but that doesn't work unfortunately.

2
  • 1
    It would not necessarily require customizing the syntax highlighter; it would be enough if it were possible to manually mark parts of the (pseudo)code as keywords, variables, constants, and whatnot. (So, it wouldn’t really use the highlighter as such, but only the CSS classes defining the various colours.) This might be also useful to override the highlighter if it gets complicated code (in a real language) wrong. Commented Sep 16, 2020 at 13:01
  • @EmilJeřábek You're right. That's unfortunately not possible right now. Commented Sep 16, 2020 at 13:13
0

As others have mentioned, pseudocode is not well defined. But to say that makes the task infeasible is pretty closed-minded - after all, a human looking at pseudo-code could highlight it decently, and surely that proves that it's possible. At least there could be a feature to highlight pseudocode poorly. In fact, many people write pseudocode in a language close to that which they are familiar, and using the syntax highlighter for that language should work pretty well. Other than that, I think syntax highlighting with lang-default does an ok job, you can judge for yourself:

def a():
    map = {x: 1.2f, y='hi'}
    return map
class MyClass{
   a = 4,
   b = 3,
}

function foo(int a, double b, MyClass c){
    print("This highlights ok I guess");
    for each x in the set:
        x+=1
}
FOR EACH vertex in stack
    IF !seen[vertex] THEN
        FOR edge in E[vertex]
            add vertex to seen 
            add vertex to stack
    ELSE
        do nothing
    ENDIF

Of course, some pseudocode styles are highlighted better than others.

4
  • 1
    Well written pseudocode is not a mish-mash of keywords and syntax from random programming languages. Good pseudocode doesn't look anything like what you've written as your example - there are no braces or semicolons or function definitions. It looks more like what's written in this article on writing good psuedocode. Before syntax highlighting, SE would need to recommend we use a particular pseudocode syntax syntax.
    – ColleenV
    Commented Mar 8, 2021 at 17:36
  • @ColleenV Surely it depends on your definition of pseudocode. Not everyone uses the 'research quality' psuedocode - at the very least, if you stop random CS students from my university and ask them to write psuedocode, it will be in the language they're familiar with. Though, if we are restricting psuedocode to the 'good subset' of pesudocode that you're talking about, then it should be even more feasible. The last couple lines in my code example used that style, and the keywords were highlighted.
    – Alex Li
    Commented Mar 9, 2021 at 0:05
  • My point is that it is impossible to syntax highlight both well-written pseudocode and the mish-mash that some people think is pseudocode and have it be meaningful. Before syntax highlighting can be useful, there needs to be a syntax to highlight. Partial or inconsistent highlighting is worse than none at all.
    – ColleenV
    Commented Mar 9, 2021 at 11:51
  • Well, I'm sure it's possible (albeit not by a simple one-size-fits-all parsing algorithm, maybe you need some ML ideas). But it's likely too hard of a problem for it too be worth it for stackoverflow to implement it. Hence why I think the best answer to the given question is to see if the way you write pseudocode is good enough for the default syntax highlighting to work.
    – Alex Li
    Commented Mar 9, 2021 at 21:35
-2

Maybe not in StackExchange. But definitely possible in general, provided that the given pseudocode is well-structured, much like the input to the algorithm packages of LaTeX.

To have pseudocode rendered in HTML as beautifully as LaTeX, try pseudocode.js.

2
  • 2
    This contributes nothing, and misses the point. You can't expect all users posting code to use the same exact pseudocode "structure", so you have no way of properly highlighting it. So, you may suggest "make it mandatory to format correctly" - but if it now fits an exact format, what's the point of it being pseudocode at that point? Commented Mar 26, 2015 at 19:39
  • Every author who typesets their algorithms in LaTeX uses the same exact pseudocode format. The format has been time tested by countless LaTeX users. So I don't see why it can't fit the needs of Web users. Another point I must stress is that the format is only required for the input. The output looks just like any pseudocode written in its most natural and intuitive way. Commented Mar 27, 2015 at 1:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .