Using a flowchart as a state machine: PlantUML and Mermaid
简介 / Introduction
State machines have a dedicated diagram type, so drawing one should obviously use state diagram syntax. In practice, three things keep pushing you back to flowchart: the toolchain only accepts flowchart, you want the flowchart layout engine to arrange the nodes, or you simply have a flowchart on hand and want to add state semantics to it.
The one question that matters is: how much state machine semantics can flowchart syntax actually express? The two engines answer differently, and the gap is not “does it support state diagrams” — it is whether the two syntaxes can be mixed in one file. That is where the real traps are, and it is the part most easily missed.
This post uses a connection lifecycle state machine (idle → connecting → connected → reconnecting → closing → closed) as the vehicle. We draw it with each engine’s flowchart syntax, then compare against each engine’s native state diagram syntax. Every claim below comes from rendering against the live backend and the actual bundled libraries — not from reading the docs.
PlantUML 版
PlantUML’s native state diagram syntax is the state keyword:
1 | @startuml |
But if what you actually want is flowchart syntax, PlantUML has a way: rectangle for the boxes, <<state>> for the UML stereotype:
1 | @startuml |
Measured against the live backend, this version renders, but at three visible costs:
- The
«state»stereotype gets drawn. Every box carries an extra stereotype caption above the name — the label sequence is«state»/Idle/«state»/Connecting/ … six boxes, six stereotype captions. It is a semantic marker saying “this is a UML state”, but from a flowchart perspective it is pure noise. - The boxes are square-cornered, not rounded. The rendered SVG shows
rx="2.5"on therectangleelements, while nativestateelements userx="10"/"11"/"12.5"— rounded. The two visual languages differ: native states read as “states”, flowchart boxes read as “processing steps”. - There is no
[*]. Flowchart syntax has no pseudo-state, so you hand-roll one withcircle "Start" as Start(rendered as a filled circle with no text) and wire the edge yourself.
The good news is that self-loops are fine: Running --> Running : heartbeat returns 200 and draws the loop correctly.
The real trap is mixing. PlantUML recognizes exactly one diagram type per file, so state diagram syntax and activity diagram syntax together is an immediate 400:
1 | @startuml |
1 | Syntax Error? (Assumed diagram type: state) |
Reversing the order (start before state) fails the same way, only the message changes to Assumed diagram type: activity. Put plainly: PlantUML guesses the diagram type from the first keyword it sees, and after that guess the whole file is locked to one syntax. If you want state semantics, write state; if you want a flowchart, do not touch state.
Incidentally, stateDiagram-v2 does not exist in PlantUML at all (that is Mermaid’s spelling). Writing it yields Syntax Error? (Assumed diagram type: class) plus the full PlantUML getting-started welcome page.
Mermaid 版
Mermaid’s native state diagram syntax is stateDiagram-v2:
1 | stateDiagram-v2 |
And Mermaid’s flowchart syntax version of the same machine looks like this:
1 | flowchart TD |
There is one hard limitation you must know: Mermaid’s flowchart has no [*].
1 | flowchart TD |
1 | Parse error on line 2: |
Writing A --> [*] fails the same way, also got 'SQS'. This limitation is identical on the editor’s bundled Mermaid 10.9.1 and the theme’s 11.4.1 — it is not a version bug. The token simply does not exist in the flowchart grammar.
In other words: [*] works only inside stateDiagram-v2. Ported to flowchart syntax you must supply the two pseudo-states by hand — ((Start)) / ((End)) double circles (rendered as circular nodes in testing), with the states themselves as ([State]) stadiums.
The second trap is subtler: when Mermaid mixes the two syntaxes, it behaves in exactly the opposite way to PlantUML.
Put stateDiagram-v2 first and flowchart second, and there is no error — it renders “successfully”, but the content is wrong. The rendered labels are:
1 | ['Alpha', 'Beta', 'flowchart', 'TD', 'Gamma', 'Delta'] |
Look closely: flowchart and TD have been taken as state names, and Gamma --> Delta was taken as a transition between states. You thought you drew two diagrams; what you actually got is one corrupted diagram. Silent wrongness is far more dangerous than an error.
Reversing the order (flowchart first, stateDiagram-v2 second) does produce a clean parse error, pointing at the stateDiagram-v2 line.
对比 / Side-by-side
| Dimension | PlantUML | Mermaid |
|---|---|---|
| Native state diagram syntax | state "Label" as ID |
stateDiagram-v2 (the older stateDiagram also works) |
| Drawing states in flowchart syntax | rectangle "Label" as ID <<state>> |
([Label]) stadiums, no UML semantic marker |
| Initial / terminal pseudo-state in flowchart | No [*]; hand-write circle "Start" as Start |
Not supported at all — [*] fails with got 'SQS' |
| Stereotype rendering | <<state>> is drawn (6 boxes, 6 stereotype captions) |
No equivalent concept |
| Box shape | flowchart square rx="2.5" vs native state rounded rx="10"–"12.5" |
Stadium / circle / rectangle, chosen by bracket syntax |
| Mixing the two syntaxes | 400 either way: Syntax Error? (Assumed diagram type: state / activity) |
State first → silent wrong diagram (flowchart/TD become state names); flowchart first → parse error |
The stateDiagram-v2 keyword |
Unsupported, 400 (Assumed diagram type: class) |
It is the native keyword |
Four measured conclusions:
- Mermaid’s flowchart syntax loses
[*]; PlantUML does not have this problem (at leastcircleis available as a workaround). This is the biggest practical gap between the two on “flowchart as a state machine” — the question is not whether you can draw it, but whether a machine can still recognize the start and end points afterwards. - PlantUML’s flowchart style renders
<<state>>visibly, effectively announcing “this is really a state diagram”. You can buy semantic traceability at the cost of visual noise, or buy clean visuals at the cost of UML semantics. - The error behavior when mixing is exactly opposite, and Mermaid’s side is the dangerous one. PlantUML stops you with a 400. Mermaid, when
stateDiagram-v2comes first, hands you a diagram that “rendered fine” but is wrong. When writing in Mermaid, guaranteeing only one syntax per file is your job. - Structured state semantics (composite states, choice, fork, concurrent regions) are out of reach for flowchart syntax in both engines. In testing,
stateDiagram-v2supports<<choice>>,<<fork>>, the--concurrency separator,direction LR,note right of, andclassDef; PlantUML’sstateside has<<choice>>,<<fork>>, composite states, andleft to right direction. None of that exists in the flowchart dialect.
小结 / Wrap-up
Here is where the real boundary of “drawing a state machine with a flowchart” lands, measured on both engines:
PlantUML has the better native state diagram syntax, and its flowchart style is a degraded fallback — <<state>> gets drawn and [*] has to be hand-built. What it buys you is that mixing is safe: a mistake fails loudly with a 400 instead of quietly handing you a wrong diagram to present.
Mermaid‘s flowchart style is visually cleaner (nice stadiums, no stereotype noise), but it has no [*] — a hard limitation, not a style preference. And its tolerance of mixed syntax (no error when stateDiagram-v2 comes first) is the real landmine.
Selection advice: use the native state diagram syntax whenever you can — both engines’ native forms are far more complete than any flowchart simulation. If the toolchain forces you into flowchart: on the Mermaid side add ((Start)) / ((End)) by hand for the pseudo-states, and make sure only one syntax appears in a given file; on the PlantUML side rectangle ... <<state>> buys a little semantic traceability, but never mix state or if/else into the same file — that is a guaranteed 400.
- Title: Using a flowchart as a state machine: PlantUML and Mermaid
- Author: puml.online
- Created at : 2026-12-04 01:00:00
- Updated at : 2026-12-05 02:08:58
- Link: https://puml.online/blog/flowchart-as-state-machine-en/
- License: This work is licensed under CC BY-NC-SA 4.0.