diff options
| author | Tamas Hadhazy <helsgithub15@gmail.com> | 2024-09-14 21:49:25 +0100 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2024-09-14 15:25:02 -0700 |
| commit | b4f3ba2bf3565eebeee3190e827d5402d8f153fd (patch) | |
| tree | 1e5c6681028dc9756b18207156619505662a4e55 | |
| parent | 4be43794169610d45e47468702c63cdfbc5deb5a (diff) | |
| download | Simple-b4f3ba2bf3565eebeee3190e827d5402d8f153fd.tar.gz Simple-b4f3ba2bf3565eebeee3190e827d5402d8f153fd.zip | |
extend explanation on scopes
| -rw-r--r-- | chapter03/README.md | 34 | ||||
| -rw-r--r-- | chapter03/docs/03-graph4.svg | 127 | ||||
| -rw-r--r-- | chapter03/docs/03-graph5.svg | 127 |
3 files changed, 288 insertions, 0 deletions
diff --git a/chapter03/README.md b/chapter03/README.md index cfb75d58..4f46f50e 100644 --- a/chapter03/README.md +++ b/chapter03/README.md @@ -127,6 +127,40 @@ Each table is annotated by its scope level, and shows the variables defined in t As we see, only 1 symbol table is present at this point. +We can also have scopes at the same level during the execution of the program +but only one will be alive at a time as the symbol table only capable of holding one symbol table for one scope level. + +For example: +``` +01 int a=1; +02 int b=2; +03 int c=0; +04 { +05 int b=3; +06 c=a+b; +08 } +09 { +10 int e = 3; +11 c = a+e; +13 } +14 return c; +``` +Here both scopes starting from `(04-08)` and `(09-13)` are at the same level: +```java +public final Stack<HashMap<String, Integer>> _scopes; +``` +The `_scopes` stack holds nested scopes, with index 0 representing the highest (global) +scope and deeper indices representing more nested scopes. +Popping returns the current scope and moves to the outer one. +There is a direct one-to-one mapping between each index and its corresponding scope. + +The first nested scope(04-08) + + +The second nested scope(09-13) + + + - In both cases the index which represents the deepness of the scope is 1. ## Enabling peephole optimizations Once we enable peephole optimizations, the parser is able to reduce all the expressions down to a diff --git a/chapter03/docs/03-graph4.svg b/chapter03/docs/03-graph4.svg new file mode 100644 index 00000000..23b48069 --- /dev/null +++ b/chapter03/docs/03-graph4.svg @@ -0,0 +1,127 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> +<!-- Generated by graphviz version 2.40.1 (20161225.0304) + --> +<!-- Title: chapter03 Pages: 1 --> +<svg width="310pt" height="228pt" viewBox="0.00 0.00 310.00 228.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> +<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 224)"> +<title>chapter03</title> +<polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-224 306,-224 306,4 -4,4"/> +<g id="clust1" class="cluster"> +<title>cluster_Nodes</title> +<polygon fill="none" stroke="#000000" points="8,-88 8,-212 294,-212 294,-88 8,-88"/> +</g> +<g id="clust2" class="cluster"> +<title>cluster_Scope1_0</title> +<polygon fill="none" stroke="#000000" points="97,-8 97,-76 259,-76 259,-8 97,-8"/> +</g> +<g id="clust3" class="cluster"> +<title>cluster_Scope1_1</title> +<polygon fill="none" stroke="#000000" points="181,-16 181,-68 251,-68 251,-16 181,-16"/> +</g> +<!-- Start2 --> +<g id="node1" class="node"> +<title>Start2</title> +<polygon fill="#ffff00" stroke="#000000" points="178,-204 124,-204 124,-168 178,-168 178,-204"/> +<text text-anchor="middle" x="151" y="-181.8" font-family="Times,serif" font-size="14.00" fill="#000000">Start</text> +</g> +<!-- Con_3 --> +<g id="node2" class="node"> +<title>Con_3</title> +<ellipse fill="none" stroke="#000000" cx="43" cy="-114" rx="27" ry="18"/> +<text text-anchor="middle" x="43" y="-109.8" font-family="Times,serif" font-size="14.00" fill="#000000">#1</text> +</g> +<!-- Con_3->Start2 --> +<g id="edge1" class="edge"> +<title>Con_3->Start2</title> +<path fill="none" stroke="#000000" stroke-dasharray="1,5" d="M62.3082,-126.8722C77.012,-136.6747 97.6668,-150.4445 115.3448,-162.2299"/> +<polygon fill="#000000" stroke="#000000" points="113.4829,-165.195 123.7449,-167.8299 117.3658,-159.3707 113.4829,-165.195"/> +<text text-anchor="middle" x="64.5318" y="-129.2722" font-family="Helvetica,sans-Serif" font-size="8.00" fill="#000000">0</text> +</g> +<!-- Con_4 --> +<g id="node3" class="node"> +<title>Con_4</title> +<ellipse fill="none" stroke="#000000" cx="115" cy="-114" rx="27" ry="18"/> +<text text-anchor="middle" x="115" y="-109.8" font-family="Times,serif" font-size="14.00" fill="#000000">#2</text> +</g> +<!-- Con_4->Start2 --> +<g id="edge2" class="edge"> +<title>Con_4->Start2</title> +<path fill="none" stroke="#000000" stroke-dasharray="1,5" d="M123.7146,-131.4292C127.8228,-139.6456 132.8196,-149.6392 137.4089,-158.8179"/> +<polygon fill="#000000" stroke="#000000" points="134.387,-160.6004 141.9897,-167.9794 140.648,-157.4699 134.387,-160.6004"/> +<text text-anchor="middle" x="121.491" y="-133.8292" font-family="Helvetica,sans-Serif" font-size="8.00" fill="#000000">0</text> +</g> +<!-- Con_6 --> +<g id="node4" class="node"> +<title>Con_6</title> +<ellipse fill="none" stroke="#000000" cx="259" cy="-114" rx="27" ry="18"/> +<text text-anchor="middle" x="259" y="-109.8" font-family="Times,serif" font-size="14.00" fill="#000000">#3</text> +</g> +<!-- Con_6->Start2 --> +<g id="edge3" class="edge"> +<title>Con_6->Start2</title> +<path fill="none" stroke="#000000" stroke-dasharray="1,5" d="M239.6918,-126.8722C224.988,-136.6747 204.3332,-150.4445 186.6552,-162.2299"/> +<polygon fill="#000000" stroke="#000000" points="184.6342,-159.3707 178.2551,-167.8299 188.5171,-165.195 184.6342,-159.3707"/> +<text text-anchor="middle" x="237.4682" y="-129.2722" font-family="Helvetica,sans-Serif" font-size="8.00" fill="#000000">0</text> +</g> +<!-- Con_8 --> +<g id="node5" class="node"> +<title>Con_8</title> +<ellipse fill="none" stroke="#000000" cx="187" cy="-114" rx="27" ry="18"/> +<text text-anchor="middle" x="187" y="-109.8" font-family="Times,serif" font-size="14.00" fill="#000000">#4</text> +</g> +<!-- Con_8->Start2 --> +<g id="edge4" class="edge"> +<title>Con_8->Start2</title> +<path fill="none" stroke="#000000" stroke-dasharray="1,5" d="M178.2854,-131.4292C174.1772,-139.6456 169.1804,-149.6392 164.5911,-158.8179"/> +<polygon fill="#000000" stroke="#000000" points="161.352,-157.4699 160.0103,-167.9794 167.613,-160.6004 161.352,-157.4699"/> +<text text-anchor="middle" x="176.0618" y="-133.8292" font-family="Helvetica,sans-Serif" font-size="8.00" fill="#000000">0</text> +</g> +<!-- Scope1_0 --> +<g id="node6" class="node"> +<title>Scope1_0</title> +<polygon fill="#00ffff" stroke="transparent" points="113,-31 113,-53 126,-53 126,-31 113,-31"/> +<polygon fill="none" stroke="#000000" points="113,-31 113,-53 126,-53 126,-31 113,-31"/> +<text text-anchor="start" x="116" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">0</text> +<polygon fill="none" stroke="#000000" points="126,-31 126,-53 138,-53 138,-31 126,-31"/> +<text text-anchor="start" x="128.8927" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">a</text> +<polygon fill="none" stroke="#000000" points="138,-31 138,-53 151,-53 151,-31 138,-31"/> +<text text-anchor="start" x="141" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">b</text> +<polygon fill="none" stroke="#000000" points="151,-31 151,-53 163,-53 163,-31 151,-31"/> +<text text-anchor="start" x="153.8927" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">c</text> +</g> +<!-- Scope1_0->Con_3 --> +<g id="edge5" class="edge"> +<title>Scope1_0:Scope1_0_a->Con_3</title> +<path fill="none" stroke="#6495ed" stroke-dasharray="5,2" d="M132,-53C132,-81.2283 102.2249,-71.955 79,-88 75.8998,-90.1418 72.6712,-92.4134 69.4762,-94.6873"/> +<polygon fill="#6495ed" stroke="#6495ed" points="67.2901,-91.9482 61.2087,-100.6239 71.373,-97.6342 67.2901,-91.9482"/> +</g> +<!-- Scope1_0->Con_4 --> +<g id="edge6" class="edge"> +<title>Scope1_0:Scope1_0_b->Con_4</title> +<path fill="none" stroke="#6495ed" stroke-dasharray="5,2" d="M145,-53C145,-65.7791 139.6328,-78.661 133.4388,-89.2726"/> +<polygon fill="#6495ed" stroke="#6495ed" points="130.3276,-87.6415 127.8968,-97.9538 136.2279,-91.4082 130.3276,-87.6415"/> +</g> +<!-- Scope1_0->Con_8 --> +<g id="edge7" class="edge"> +<title>Scope1_0:Scope1_0_c->Con_8</title> +<path fill="none" stroke="#6495ed" stroke-dasharray="5,2" d="M157,-53C157,-65.7791 162.3672,-78.661 168.5612,-89.2726"/> +<polygon fill="#6495ed" stroke="#6495ed" points="165.7721,-91.4082 174.1032,-97.9538 171.6724,-87.6415 165.7721,-91.4082"/> +</g> +<!-- Scope1_1 --> +<g id="node7" class="node"> +<title>Scope1_1</title> +<polygon fill="#00ffff" stroke="transparent" points="203,-31 203,-53 216,-53 216,-31 203,-31"/> +<polygon fill="none" stroke="#000000" points="203,-31 203,-53 216,-53 216,-31 203,-31"/> +<text text-anchor="start" x="206" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">1</text> +<polygon fill="none" stroke="#000000" points="216,-31 216,-53 229,-53 229,-31 216,-31"/> +<text text-anchor="start" x="219" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">b</text> +</g> +<!-- Scope1_1->Con_6 --> +<g id="edge8" class="edge"> +<title>Scope1_1:Scope1_1_b->Con_6</title> +<path fill="none" stroke="#6495ed" stroke-dasharray="5,2" d="M223,-53C223,-66.7784 229.8964,-80.1812 237.6523,-90.8977"/> +<polygon fill="#6495ed" stroke="#6495ed" points="235.0006,-93.1875 243.9413,-98.8721 240.497,-88.8528 235.0006,-93.1875"/> +</g> +</g> +</svg>
\ No newline at end of file diff --git a/chapter03/docs/03-graph5.svg b/chapter03/docs/03-graph5.svg new file mode 100644 index 00000000..346b08f2 --- /dev/null +++ b/chapter03/docs/03-graph5.svg @@ -0,0 +1,127 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> +<!-- Generated by graphviz version 2.40.1 (20161225.0304) + --> +<!-- Title: chapter03 Pages: 1 --> +<svg width="310pt" height="228pt" viewBox="0.00 0.00 310.00 228.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> +<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 224)"> +<title>chapter03</title> +<polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-224 306,-224 306,4 -4,4"/> +<g id="clust1" class="cluster"> +<title>cluster_Nodes</title> +<polygon fill="none" stroke="#000000" points="8,-88 8,-212 294,-212 294,-88 8,-88"/> +</g> +<g id="clust2" class="cluster"> +<title>cluster_Scope1_0</title> +<polygon fill="none" stroke="#000000" points="97,-8 97,-76 259,-76 259,-8 97,-8"/> +</g> +<g id="clust3" class="cluster"> +<title>cluster_Scope1_1</title> +<polygon fill="none" stroke="#000000" points="181,-16 181,-68 251,-68 251,-16 181,-16"/> +</g> +<!-- Start2 --> +<g id="node1" class="node"> +<title>Start2</title> +<polygon fill="#ffff00" stroke="#000000" points="178,-204 124,-204 124,-168 178,-168 178,-204"/> +<text text-anchor="middle" x="151" y="-181.8" font-family="Times,serif" font-size="14.00" fill="#000000">Start</text> +</g> +<!-- Con_3 --> +<g id="node2" class="node"> +<title>Con_3</title> +<ellipse fill="none" stroke="#000000" cx="43" cy="-114" rx="27" ry="18"/> +<text text-anchor="middle" x="43" y="-109.8" font-family="Times,serif" font-size="14.00" fill="#000000">#1</text> +</g> +<!-- Con_3->Start2 --> +<g id="edge1" class="edge"> +<title>Con_3->Start2</title> +<path fill="none" stroke="#000000" stroke-dasharray="1,5" d="M62.3082,-126.8722C77.012,-136.6747 97.6668,-150.4445 115.3448,-162.2299"/> +<polygon fill="#000000" stroke="#000000" points="113.4829,-165.195 123.7449,-167.8299 117.3658,-159.3707 113.4829,-165.195"/> +<text text-anchor="middle" x="64.5318" y="-129.2722" font-family="Helvetica,sans-Serif" font-size="8.00" fill="#000000">0</text> +</g> +<!-- Con_4 --> +<g id="node3" class="node"> +<title>Con_4</title> +<ellipse fill="none" stroke="#000000" cx="115" cy="-114" rx="27" ry="18"/> +<text text-anchor="middle" x="115" y="-109.8" font-family="Times,serif" font-size="14.00" fill="#000000">#2</text> +</g> +<!-- Con_4->Start2 --> +<g id="edge2" class="edge"> +<title>Con_4->Start2</title> +<path fill="none" stroke="#000000" stroke-dasharray="1,5" d="M123.7146,-131.4292C127.8228,-139.6456 132.8196,-149.6392 137.4089,-158.8179"/> +<polygon fill="#000000" stroke="#000000" points="134.387,-160.6004 141.9897,-167.9794 140.648,-157.4699 134.387,-160.6004"/> +<text text-anchor="middle" x="121.491" y="-133.8292" font-family="Helvetica,sans-Serif" font-size="8.00" fill="#000000">0</text> +</g> +<!-- Con_9 --> +<g id="node4" class="node"> +<title>Con_9</title> +<ellipse fill="none" stroke="#000000" cx="259" cy="-114" rx="27" ry="18"/> +<text text-anchor="middle" x="259" y="-109.8" font-family="Times,serif" font-size="14.00" fill="#000000">#3</text> +</g> +<!-- Con_9->Start2 --> +<g id="edge3" class="edge"> +<title>Con_9->Start2</title> +<path fill="none" stroke="#000000" stroke-dasharray="1,5" d="M239.6918,-126.8722C224.988,-136.6747 204.3332,-150.4445 186.6552,-162.2299"/> +<polygon fill="#000000" stroke="#000000" points="184.6342,-159.3707 178.2551,-167.8299 188.5171,-165.195 184.6342,-159.3707"/> +<text text-anchor="middle" x="237.4682" y="-129.2722" font-family="Helvetica,sans-Serif" font-size="8.00" fill="#000000">0</text> +</g> +<!-- Con_11 --> +<g id="node5" class="node"> +<title>Con_11</title> +<ellipse fill="none" stroke="#000000" cx="187" cy="-114" rx="27" ry="18"/> +<text text-anchor="middle" x="187" y="-109.8" font-family="Times,serif" font-size="14.00" fill="#000000">#4</text> +</g> +<!-- Con_11->Start2 --> +<g id="edge4" class="edge"> +<title>Con_11->Start2</title> +<path fill="none" stroke="#000000" stroke-dasharray="1,5" d="M178.2854,-131.4292C174.1772,-139.6456 169.1804,-149.6392 164.5911,-158.8179"/> +<polygon fill="#000000" stroke="#000000" points="161.352,-157.4699 160.0103,-167.9794 167.613,-160.6004 161.352,-157.4699"/> +<text text-anchor="middle" x="176.0618" y="-133.8292" font-family="Helvetica,sans-Serif" font-size="8.00" fill="#000000">0</text> +</g> +<!-- Scope1_0 --> +<g id="node6" class="node"> +<title>Scope1_0</title> +<polygon fill="#00ffff" stroke="transparent" points="113,-31 113,-53 126,-53 126,-31 113,-31"/> +<polygon fill="none" stroke="#000000" points="113,-31 113,-53 126,-53 126,-31 113,-31"/> +<text text-anchor="start" x="116" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">0</text> +<polygon fill="none" stroke="#000000" points="126,-31 126,-53 138,-53 138,-31 126,-31"/> +<text text-anchor="start" x="128.8927" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">a</text> +<polygon fill="none" stroke="#000000" points="138,-31 138,-53 151,-53 151,-31 138,-31"/> +<text text-anchor="start" x="141" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">b</text> +<polygon fill="none" stroke="#000000" points="151,-31 151,-53 163,-53 163,-31 151,-31"/> +<text text-anchor="start" x="153.8927" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">c</text> +</g> +<!-- Scope1_0->Con_3 --> +<g id="edge5" class="edge"> +<title>Scope1_0:Scope1_0_a->Con_3</title> +<path fill="none" stroke="#6495ed" stroke-dasharray="5,2" d="M132,-53C132,-81.2283 102.2249,-71.955 79,-88 75.8998,-90.1418 72.6712,-92.4134 69.4762,-94.6873"/> +<polygon fill="#6495ed" stroke="#6495ed" points="67.2901,-91.9482 61.2087,-100.6239 71.373,-97.6342 67.2901,-91.9482"/> +</g> +<!-- Scope1_0->Con_4 --> +<g id="edge6" class="edge"> +<title>Scope1_0:Scope1_0_b->Con_4</title> +<path fill="none" stroke="#6495ed" stroke-dasharray="5,2" d="M145,-53C145,-65.7791 139.6328,-78.661 133.4388,-89.2726"/> +<polygon fill="#6495ed" stroke="#6495ed" points="130.3276,-87.6415 127.8968,-97.9538 136.2279,-91.4082 130.3276,-87.6415"/> +</g> +<!-- Scope1_0->Con_11 --> +<g id="edge7" class="edge"> +<title>Scope1_0:Scope1_0_c->Con_11</title> +<path fill="none" stroke="#6495ed" stroke-dasharray="5,2" d="M157,-53C157,-65.7791 162.3672,-78.661 168.5612,-89.2726"/> +<polygon fill="#6495ed" stroke="#6495ed" points="165.7721,-91.4082 174.1032,-97.9538 171.6724,-87.6415 165.7721,-91.4082"/> +</g> +<!-- Scope1_1 --> +<g id="node7" class="node"> +<title>Scope1_1</title> +<polygon fill="#00ffff" stroke="transparent" points="204,-31 204,-53 217,-53 217,-31 204,-31"/> +<polygon fill="none" stroke="#000000" points="204,-31 204,-53 217,-53 217,-31 204,-31"/> +<text text-anchor="start" x="207" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">1</text> +<polygon fill="none" stroke="#000000" points="217,-31 217,-53 229,-53 229,-31 217,-31"/> +<text text-anchor="start" x="219.8927" y="-37.4" font-family="Times,serif" font-size="14.00" fill="#000000">e</text> +</g> +<!-- Scope1_1->Con_9 --> +<g id="edge8" class="edge"> +<title>Scope1_1:Scope1_1_e->Con_9</title> +<path fill="none" stroke="#6495ed" stroke-dasharray="5,2" d="M223,-53C223,-66.7784 229.8964,-80.1812 237.6523,-90.8977"/> +<polygon fill="#6495ed" stroke="#6495ed" points="235.0006,-93.1875 243.9413,-98.8721 240.497,-88.8528 235.0006,-93.1875"/> +</g> +</g> +</svg>
\ No newline at end of file |
