# Grammar questions

**URL:** <https://forum.graphviz.org/t/grammar-questions/346>\
**Category:** Help\
**Created:** [September 13, 2020, 2:35pm UTC](https://forum.graphviz.org/t/grammar-questions/346 "2020-09-13T14:35:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DerMolly](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/dermolly/32/225_2.png) [@DerMolly](https://forum.graphviz.org/u/DerMolly)\
**Post date:** [September 13, 2020, 2:35pm UTC](https://forum.graphviz.org/t/grammar-questions/346/1 "2020-09-13T14:35:17Z")

</div>

Hey,

I’m currently working on a parser for the DOT Language. For that I used the language info page[1](https://www.graphviz.org/doc/info/lang.html) and it seems to work all right. Then I searched for some examples to test the parser and I found some examples that work with the `dot` cli tool, but seems to be wrong when looking at the grammar[1](https://www.graphviz.org/doc/info/lang.html).

1. 

```auto
digraph D {
    node [fontname="Arial"];

    node_A [shape=record label="shape=record|{above|middle|below}|right"];
    node_B [shape=plaintext label="shape=plaintext|{curly|braces and|bars without}|effect"];
}

```

which outputs

 ![grafik](https://global.discourse-cdn.com/graphviz/original/1X/8a6470f41be0a30ff07ccd4d38b1909a934ded14.png)

I’m puzzled by the `node_A` and `node_B` as `node` is stated to be a keyword and

> Obviously, to use a keyword as an ID, it must be quoted.

My Parser identifies `node` as the keyword and does not know what to do with the ID `_A` or `_B` as the rules specifies `[` after the keyword `node`. I guess the whole things get’s interpreted as a `node_stmt` ( _node\_stmt_ : _node\_id_ [_attr\_list_] ) and node\_A`and`node\_B`are`node\_id`s.

1. 

```auto
digraph D {
  A -> {B, C, D} -> {F}
}

```

which outputs

```auto
     A
  / | \
 B C D
  \ | /
     F

```

(new user can’t upload more than one image)

Here the problem seems to be the `,` separating the different `node_id`s in the `subgraph` (`{B, C, D}`). According to the grammar]1] the only separator in between `stmt`s (or `node_id`s in this case) should be `;` (or just a space). Commas are only for separating `a_list`s (alongside `;`) (_a\_list_ : _ID_ ‘ **=** ’ _ID_ [(‘ **;** ’ | ‘ **,** ’)] [_a\_list_]).

Am I missing something or is either the grammar outdated or the dot cli tool a bit leaner with the grammar?

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [September 13, 2020, 6:35pm UTC](https://forum.graphviz.org/t/grammar-questions/346/2 "2020-09-13T18:35:55Z")

</div>

I’m not sure I fully understand your questions, but I’ll attempt an answer.

1. `node` is a keyword when it appears as a word itself, not as a prefix of other identifiers. There is nothing special about the identifiers `node_A` and `node_B`. You could equally well call these `foo_A` and `bar_B`.

2. I think the webpage is inaccurate about the grammar. The [source itself](https://gitlab.com/graphviz/graphviz/-/blob/master/lib/cgraph/grammar.y#L125-137) contains the following rules:

```c
stmtlist :	stmtlist stmt |	stmt ;

optsemi : ';' | ;

stmt : attrstmt optsemi
			| compound optsemi
			;

compound :	simple rcompound optattr
					{if ($2) endedge(); else endnode();}
			;

simple :	nodelist | subgraph ;

```

So I think `B, C, D` in your example is parsed as a `nodelist`.

I’m curious about your parser. Could you say more about what you’re building and/or your aims?

---

<div class="post-metadata">

**Author:** ![DerMolly](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/dermolly/32/225_2.png) [@DerMolly](https://forum.graphviz.org/u/DerMolly)\
**Post date:** [September 13, 2020, 7:28pm UTC](https://forum.graphviz.org/t/grammar-questions/346/3 "2020-09-13T19:28:03Z")

</div>

1. Discussing it with the rest of the team we came to the same conclusion and are currently trying to fix our lexer to support keyword prefixes.
2. What is a `nodelist`? Is this a nonterminal missing from the grammar? The example you copied also shows that between statements there should only be a ‘;’ and not a ‘,’. Digging deeper into the source, there seems to be a much different grammar in the source file than on the website. Did you by any chance change your grammar in an update and forget to update the documentation on the page?

> I’m curious about your parser. Could you say more about what you’re building and/or your aims?

I’m part of the team working on ~~CodiMD~~ [HedgeDoc](https://github.com/codimd/react-client) - a collaborative markdown editor. We’re currently in the process of rebuilding our frontend for 2.0. In that process we are examining all the diagrams we support. Until 1.6 one of them was graphviz, but as the JS-Lib to generate the svg can be called a hack at the best of times, we want to remove that if possible. Unfortunately there are no good TypeScript Libs to generate images from dot, so we’re trying to parse the dot langugae and generate mermaid code (annother diagram type we support) to minimized the amount of breaking in 2.0.

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [September 13, 2020, 8:26pm UTC](https://forum.graphviz.org/t/grammar-questions/346/4 "2020-09-13T20:26:51Z")

</div>

Any changes to the grammar in either the source or website occurred before my time. I’ll have to leave it to one of the more senior maintainers to comment on the situation there. However, as a reference for building your own parser, I would use the Graphviz source code, not the website.

> [@DerMolly](#):
>
> I’m part of the team working on ~~CodiMD~~ [HedgeDoc](https://github.com/codimd/react-client)…

Interesting. Maybe @magjac can comment on the solution space here? I know he did some nice work to make this forum able to directly render DOT sources as SVGs.

---

<div class="post-metadata">

**Author:** ![magjac](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/magjac/32/16_2.png) [@magjac](https://forum.graphviz.org/u/magjac)\
**Post date:** [September 13, 2020, 8:50pm UTC](https://forum.graphviz.org/t/grammar-questions/346/5 "2020-09-13T20:50:57Z")

</div>

I have no idea if this is helpful or not, but…

I use my own library [d3-graphviz](https://github.com/magjac/d3-graphviz) which is based on [@hpcc-js/wasm](https://github.com/hpcc-systems/hpcc-js-wasm). It’s JavaScript, not TypeScript though, although there is [@types/d3-graphviz](https://libraries.io/npm/@types%2Fd3-graphviz).

In the [graphviz-visual-editor](https://github.com/magjac/graphviz-visual-editor) react app there’s a [parser](https://github.com/magjac/graphviz-visual-editor/blob/master/src/dotGrammar.pegjs) based on [pegjs](https://pegjs.org/) which is an extended version of [dotparser](https://github.com/anvaka/dotparser).

> [@smattr](#):
>
> this forum able to directly render DOT sources as SVGs.

Here’s an example of that:

[dot verbose=true]

```auto
digraph D {
    node [fontname="Arial"];

    node_A [shape=record label="shape=record|{above|middle|below}|right"];
    node_B [shape=plaintext label="shape=plaintext|{curly|braces and|bars without}|effect"];
}

```

[/dot]

---

<div class="post-metadata">

**Author:** ![DerMolly](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/dermolly/32/225_2.png) [@DerMolly](https://forum.graphviz.org/u/DerMolly)\
**Post date:** [September 13, 2020, 9:16pm UTC](https://forum.graphviz.org/t/grammar-questions/346/6 "2020-09-13T21:16:21Z")

</div>

> [@smattr](#):
>
> However, as a reference for building your own parser, I would use the Graphviz source code, not the website.

Yeah you’re right. The websites definition looked so good, precise and complete, that I thought that it would suffice.

@magjac I will need to have a look at those, maybe there’s something we can use. Thanks!

---

<div class="post-metadata">

**Author:** ![erg](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/erg/32/34_2.png) [@erg](https://forum.graphviz.org/u/erg)\
**Post date:** [September 14, 2020, 9:30pm UTC](https://forum.graphviz.org/t/grammar-questions/346/7 "2020-09-14T21:30:12Z")

</div>

The grammar description in the website documentation is meant to be an easily understandable grammar, unlike the real yacc grammar. Anything you write using the former will be valid dot, and it probably captures most of what’s doable using the yacc grammar. One might consider adding commas as well as semicolons in stmt\_list, but that would not give legal dot. To get a comma-separated node list, one would have to start making the grammar more complicated, which would defeat its pedagogical purpose. The bottom line: if you want to capture the complete dot grammar, you’ll have to use the yacc grammar, as noted.
