背景
一图胜千言。有时候用图表表达意图比较直观、容易理解。但是用一般画图工具比较麻烦,也不容易修改,所以用文本描述设计意图,然后自动呈现图表就是一种理想的方案。
本文用EBNF语法图描述了语法图的语法。
参考
语法说明文档:https://plantuml.com/zh/ebnf
在线编辑器:https://editor.plantuml.com/uml/
语法
grammar
有多条规则构成:
@startebnf
grammar = { rule } ;
(*由0条或者多条规则组成*)
@endebnf

rule
每一条规则形如:A=B;
@startebnf
rule = lhs , "=" , rhs , ";" ;
(*规则: A=B; A: 左侧,B: 右侧;*)
@endebnf

lhs:左侧
左侧就是一个标识符。
@startebnf
lhs = identifier ;
(*左侧就是一个标识符*)
identifier = letter , { letter | digit | "_" } ;
digit = ? 0-9 ? ;
letter = ? A-Z or a-z ? ;
@endebnf

rhs:右侧
@startebnf
rhs = identifier(*右侧:可以是标识符*)| terminal(*可以是终结符*)| "[" , rhs (* 也可以是一个可选项 *) , "]"| "{" , rhs (* 也可以是重复项 *), "}"| "(" , rhs (* 也可以是分组 *) , ")"| "(*" , string (* 也可以是注释 *) , "*)"| "?" , rhs (* 虚线方框 *) , "?"| rhs , "|" (* 多选一 *) , rhs| rhs , "," (* 多个右侧的连接 *), rhs ;
terminal = "'" , character , { character } , "'"| '"' , character , { character } , '"' ;
(*终结字符串*)
character = letter | digit | symbol | "_" ;
(*字符*)
symbol = "[" | "]" | "{" | "}" | "(" | ")" | "<" | ">"| "'" | '"' | "=" | "|" | "." | "," | ";" ;
(*符号*)
@endebnf

补充
- 注释即可以修饰标识符和终结字符串,也可以单独一行
- 终结字符串即可以用单引号,也可以是双引号,甚至不使用引号也可以
- 不使用引号的时候,一般表示终结字符串
@startebnf
binaryDigit = 02a | 123;
@endebnf

- 支持title作为标题
title All EBNF elements managed by PlantUML
- 表示一个或者多个的简便用法
@startebnf
one_or_more_ebnf = {a}-;
@endebnf

- 支持‘*’,表示重复多少次
@startebnf
test = 4 * 2;
@endebnf

- 支持自定义样式
@startebnf
<style>
element {ebnf {LineColor blueFontcolor greenBackgroundcolor palegreennote {Backgroundcolor pink}}
}
</style>
title Title
styled_ebnf = {"a", c , "a" (* Note on a *)}
| ? special ?
| "repetition", 4 * '2';
(* Global End Note *)
@endebnf

- 排除符号‘-’
@startebnf
title First [modified] example of §5.8 Syntactic-term of ISO-EBNF
letter = ? "A" - "Z" ?;
vowel = "A" | "E" | "I" | "O" | "U";
consonant = letter - vowel;
@endebnf
