From df5294e88cc409d7adf3e10666bfcb40e34472b9 Mon Sep 17 00:00:00 2001 From: Dibyendu Majumdar Date: Sun, 1 Oct 2023 18:01:54 +0100 Subject: Chapter 3 - add vars and lexical scope. (#7) * Chapter 3 - add vars and lexical scope. * Fix typo * Overhaul graph viz Makes the scopes pointing into the graph being build more visually appealing. Constants are a printed with just the constant value. * Fix control edge color * Fix missing grouping operator parse Add a parens test. Shorten names 'Identifier' to 'Id' _lexer.match -> match require() passes a Node thru, so can be flow-coded. Uniform syntax error messages. Handy Lexer.toString for debugging. Drop Optional for null returns; less total code; less objects; less confusion. camelCase some fcns. * All parser helpers return Node, not void. Drop _ret. parseBlock and parseStatement now the same. * CLosely align chap2 and chap3 parsers Mostly whitespace aligned; dragged a few functions around to make more sense. I think the diff from Ch2 and Ch3 is down a few differences about what is a statement, plus all the symbol stuff for scopes and variables. * Add examples illustrating the symbol table creation. * Update docs and comments --------- Co-authored-by: Cliff Click --- chapter03/docs/03-grammar.md | 72 ++++++++++++++++++++++++ chapter03/docs/03-graph1.gv | 33 +++++++++++ chapter03/docs/03-graph1.svg | 131 +++++++++++++++++++++++++++++++++++++++++++ chapter03/docs/03-graph2.gv | 29 ++++++++++ chapter03/docs/03-graph2.svg | 130 ++++++++++++++++++++++++++++++++++++++++++ chapter03/docs/03-graph3.gv | 28 +++++++++ chapter03/docs/03-graph3.svg | 120 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 543 insertions(+) create mode 100644 chapter03/docs/03-grammar.md create mode 100644 chapter03/docs/03-graph1.gv create mode 100644 chapter03/docs/03-graph1.svg create mode 100644 chapter03/docs/03-graph2.gv create mode 100644 chapter03/docs/03-graph2.svg create mode 100644 chapter03/docs/03-graph3.gv create mode 100644 chapter03/docs/03-graph3.svg (limited to 'chapter03/docs') diff --git a/chapter03/docs/03-grammar.md b/chapter03/docs/03-grammar.md new file mode 100644 index 00000000..2a6d4c1b --- /dev/null +++ b/chapter03/docs/03-grammar.md @@ -0,0 +1,72 @@ +# Grammar for Chapter 1 + +```antlrv4 +grammar SimpleLanguage; + +program + : statement+ EOF + ; + +statement + : returnStatement + | declStatement + | blockStatment + | expressionStatement + | metaStatement + ; + +metaStatement + : '#showGraph' ';' + ; + +expressionStatement + : IDENTIFIER '=' expression ';' + ; + +blockStatement + : '{' statement+ '}' + ; + +declStatement + : 'int' IDENTIFIER '=' expression ';' + ; + +returnStatement + : 'return' expression ';' + ; + +expression + : additiveExpression + ; + +additiveExpression + : multiplicativeExpression (('+' | '-') multiplicativeExpression)* + ; + +multiplicativeExpression + : unaryExpression (('*' | '/') unaryExpression)* + ; + +unaryExpression + : ('-') unaryExpression + | primaryExpression + ; + +primaryExpression + : IDENTIFIER + | INTEGER_LITERAL + | '(' expression ')' + ; + +INTEGER_LITERAL + : [1-9][0-9]* + | [0] + ; + +IDENTIFIER + : NON_DIGIT (NON_DIGIT | DIGIT)* + ; + +NON_DIGIT: [a-zA-Z_]; +DEC_DIGIT: [0-9]; +``` \ No newline at end of file diff --git a/chapter03/docs/03-graph1.gv b/chapter03/docs/03-graph1.gv new file mode 100644 index 00000000..5983f622 --- /dev/null +++ b/chapter03/docs/03-graph1.gv @@ -0,0 +1,33 @@ +digraph chapter03 { + rankdir=BT; + subgraph cluster_Nodes { + Start1 [ shape=box style=filled fillcolor=yellow label="Start" ]; + Con_2 [ label="#1" ]; + Con_3 [ label="#2" ]; + Con_5 [ label="#3" ]; + Add6 [ label="Add" ]; + } + node [shape=plaintext]; + subgraph cluster_0 { + scope0 [label=< + + +
0abc
>]; + subgraph cluster_1 { + scope1 [label=< + + +
1b
>]; + } + } + Con_2 -> Start1 [style=dotted]; + Con_3 -> Start1 [style=dotted]; + Con_5 -> Start1 [style=dotted]; + Add6 -> Con_2; + Add6 -> Con_5; + edge [style=dashed color=cornflowerblue]; + scope0:scope0_a -> Con_2; + scope0:scope0_b -> Con_3; + scope0:scope0_c -> Add6; + scope1:scope1_b -> Con_5; +} diff --git a/chapter03/docs/03-graph1.svg b/chapter03/docs/03-graph1.svg new file mode 100644 index 00000000..364cd42c --- /dev/null +++ b/chapter03/docs/03-graph1.svg @@ -0,0 +1,131 @@ + + + + + + +chapter03 + + +cluster_Nodes + + + +cluster_0 + + + +cluster_1 + + + + +Start1 + +Start + + + +Con_2 + +#1 + + + +Con_2->Start1 + + + + + +Con_3 + +#2 + + + +Con_3->Start1 + + + + + +Con_5 + +#3 + + + +Con_5->Start1 + + + + + +Add6 + +Add + + + +Add6->Con_2 + + + + + +Add6->Con_5 + + + + + +scope0 + + +0 + +a + +b + +c + + + +scope0:scope0_a->Con_2 + + + + + +scope0:scope0_b->Con_3 + + + + + +scope0:scope0_c->Add6 + + + + + +scope1 + + +1 + +b + + + +scope1:scope1_b->Con_5 + + + + + diff --git a/chapter03/docs/03-graph2.gv b/chapter03/docs/03-graph2.gv new file mode 100644 index 00000000..c6c55028 --- /dev/null +++ b/chapter03/docs/03-graph2.gv @@ -0,0 +1,29 @@ +digraph chapter03 { + rankdir=BT; + subgraph cluster_Nodes { + Start1 [ shape=box style=filled fillcolor=yellow label="Start" ]; + Con_2 [ label="#1" ]; + Con_3 [ label="#2" ]; + Con_5 [ label="#3" ]; + Add6 [ label="Add" ]; + Return7 [ shape=box style=filled fillcolor=yellow label="Return" ]; + } + node [shape=plaintext]; + subgraph cluster_0 { + scope0 [label=< + + +
0abc
>]; + } + Con_2 -> Start1 [style=dotted]; + Con_3 -> Start1 [style=dotted]; + Con_5 -> Start1 [style=dotted]; + Add6 -> Con_2; + Add6 -> Con_5; + Return7 -> Start1 [color=red]; + Return7 -> Add6; + edge [style=dashed color=cornflowerblue]; + scope0:scope0_a -> Con_2; + scope0:scope0_b -> Con_3; + scope0:scope0_c -> Add6; +} diff --git a/chapter03/docs/03-graph2.svg b/chapter03/docs/03-graph2.svg new file mode 100644 index 00000000..4ab5a8b6 --- /dev/null +++ b/chapter03/docs/03-graph2.svg @@ -0,0 +1,130 @@ + + + + + + +chapter03 + + +cluster_Nodes + + + +cluster_0 + + + + +Start1 + +Start + + + +Con_2 + +#1 + + + +Con_2->Start1 + + + + + +Con_3 + +#2 + + + +Con_3->Start1 + + + + + +Con_5 + +#3 + + + +Con_5->Start1 + + + + + +Add6 + +Add + + + +Add6->Con_2 + + + + + +Add6->Con_5 + + + + + +Return7 + +Return + + + +Return7->Start1 + + + + + +Return7->Add6 + + + + + +scope0 + + +0 + +a + +b + +c + + + +scope0:scope0_a->Con_2 + + + + + +scope0:scope0_b->Con_3 + + + + + +scope0:scope0_c->Add6 + + + + + diff --git a/chapter03/docs/03-graph3.gv b/chapter03/docs/03-graph3.gv new file mode 100644 index 00000000..da30744b --- /dev/null +++ b/chapter03/docs/03-graph3.gv @@ -0,0 +1,28 @@ +digraph chapter03 { + rankdir=BT; + subgraph cluster_Nodes { + Start1 [ shape=box style=filled fillcolor=yellow label="Start" ]; + Con_2 [ label="#1" ]; + Con_19 [ label="#8" ]; + Con_3 [ label="#2" ]; + Return20 [ shape=box style=filled fillcolor=yellow label="Return" ]; + Con_4 [ label="#3" ]; + Con_5 [ label="#4" ]; + } + node [shape=plaintext]; + subgraph cluster_0 { + scope0 [label=< + + +
0y0x0y1x1
>]; + } + Con_19 -> Start1 [style=dotted]; + Return20 -> Start1 [color=red]; + Return20 -> Con_19; + edge [style=dashed color=cornflowerblue]; + scope0:scope0_y0 -> Con_3; + scope0:scope0_x0 -> Con_2; + scope0:scope0_y1 -> Con_5; + scope0:scope0_x1 -> Con_4; +} + diff --git a/chapter03/docs/03-graph3.svg b/chapter03/docs/03-graph3.svg new file mode 100644 index 00000000..3900b93d --- /dev/null +++ b/chapter03/docs/03-graph3.svg @@ -0,0 +1,120 @@ + + + + + + +chapter03 + + +cluster_Nodes + + + +cluster_0 + + + + +Start1 + +Start + + + +Con_2 + +#1 + + + +Con_19 + +#8 + + + +Con_19->Start1 + + + + + +Con_3 + +#2 + + + +Return20 + +Return + + + +Return20->Start1 + + + + + +Return20->Con_19 + + + + + +Con_4 + +#3 + + + +Con_5 + +#4 + + + +scope0 + + +0 + +y0 + +x0 + +y1 + +x1 + + + +scope0:scope0_x0->Con_2 + + + + + +scope0:scope0_y0->Con_3 + + + + + +scope0:scope0_x1->Con_4 + + + + + +scope0:scope0_y1->Con_5 + + + + + -- cgit v1.2.3