PTHL Samples
First, please note that all the samples, including in this section, are
generated with a TC-1+ compliant compiler: its behavior differs
from that of a TC-0 compiler. In particular, for the time being,
forget about the options -X
and --parse
.
Running TC-0 basically consists in looking at exit values:
print("Hello, World!\n")
$ tc simple.tig
$ echo $?
0
The following example demonstrates the scanner and parser tracing. The glyphs error→ and ⇒ are typographic conventions to specify respectively the standard error stream and the exit status. They are not part of the output per se.
$ SCAN=1 PARSE=1 tc -X --parse simple.tig
Parsing file: "simple.tig"
Starting parse
Entering state 0
Reading a token
--rule src/parse/scantiger.ll:201 start(0) 1,0:"print"
Next token is token identifier (simple.tig:1.1-5: print)
Shifting token identifier (simple.tig:1.1-5: print)
Entering state 2
Reading a token
--rule src/parse/scantiger.ll:142 start(0) 1,5:"("
Next token is token ( (simple.tig:1.1-6: )
Reducing stack 0 by rule 102 (line 653):
$1 = token identifier (simple.tig:1.1-5: print)
-> $$ = nterm funid (simple.tig:1.1-5: print)
Entering state 38
Next token is token ( (simple.tig:1.1-6: )
Shifting token ( (simple.tig:1.1-6: )
Entering state 87
Reading a token
--rule src/parse/scantiger.ll:202 start(0) 1,6:"""
--rule src/parse/scantiger.ll:273 start(2) 1,7:"Hello, World!"
--rule src/parse/scantiger.ll:260 start(2) 1,20:"\n"
--rule src/parse/scantiger.ll:235 start(2) 1,22:"""
Next token is token string (simple.tig:1.1-23: Hello, World!
)
Shifting token string (simple.tig:1.1-23: Hello, World!
)
Entering state 1
Reducing stack 0 by rule 4 (line 300):
$1 = token string (simple.tig:1.1-23: Hello, World!
)
-> $$ = nterm exp (simple.tig:1.1-23: "Hello, World!\n")
Entering state 134
Reading a token
--rule src/parse/scantiger.ll:143 start(0) 1,23:")"
Next token is token ) (simple.tig:1.1-24: )
Reducing stack 0 by rule 47 (line 427):
$1 = nterm exp (simple.tig:1.1-23: "Hello, World!\n")
-> $$ = nterm args.1 (simple.tig:1.1-23: "Hello, World!\n")
Entering state 136
Next token is token ) (simple.tig:1.1-24: )
Reducing stack 0 by rule 46 (line 422):
$1 = nterm args.1 (simple.tig:1.1-23: "Hello, World!\n")
-> $$ = nterm args (simple.tig:1.1-23: "Hello, World!\n")
Entering state 135
Next token is token ) (simple.tig:1.1-24: )
Shifting token ) (simple.tig:1.1-24: )
Entering state 178
Reducing stack 0 by rule 6 (line 308):
$1 = nterm funid (simple.tig:1.1-5: print)
$2 = token ( (simple.tig:1.1-6: )
$3 = nterm args (simple.tig:1.1-23: "Hello, World!\n")
$4 = token ) (simple.tig:1.1-24: )
-> $$ = nterm exp (simple.tig:1.1-24: print("Hello, World!\n"))
Entering state 25
Reading a token
--rule src/parse/scantiger.ll:139 start(0) 1,24:"
"
--EOF start(0)
Now at end of input.
Reducing stack 0 by rule 1 (line 289):
$1 = nterm exp (simple.tig:1.1-24: print("Hello, World!\n"))
-> $$ = nterm program (simple.tig:1.1-24: )
Entering state 24
Now at end of input.
Shifting token end of file (:2.0: )
Entering state 65
Cleanup: popping token end of file (:2.0: )
Cleanup: popping nterm program (simple.tig:1.1-24: )
Parsing string: function _main() = (_exp(0); ())
Starting parse
Entering state 0
Reading a token
--rule src/parse/scantiger.ll:168 start(0) 1,0:"function"
Next token is token function (:1.1-8: )
Shifting token function (:1.1-8: )
Entering state 8
Reading a token
--rule src/parse/scantiger.ll:138 start(0) 1,8:" "
--rule src/parse/scantiger.ll:200 start(0) 1,9:"_main"
Next token is token identifier (:1.10-14: _main)
Shifting token identifier (:1.10-14: _main)
Entering state 45
Reading a token
--rule src/parse/scantiger.ll:142 start(0) 1,14:"("
Next token is token ( (:1.10-15: )
Shifting token ( (:1.10-15: )
Entering state 96
Reading a token
--rule src/parse/scantiger.ll:143 start(0) 1,15:")"
Next token is token ) (:1.10-16: )
Reducing stack 0 by rule 97 (line 632):
-> $$ = nterm funargs (:1.16: )
Entering state 148
Next token is token ) (:1.10-16: )
Shifting token ) (:1.10-16: )
Entering state 191
Reading a token
--rule src/parse/scantiger.ll:138 start(0) 1,16:" "
--rule src/parse/scantiger.ll:156 start(0) 1,17:"="
Next token is token = (:1.18: )
Reducing stack 0 by rule 88 (line 594):
-> $$ = nterm typeid.opt (:1.17: )
Entering state 220
Next token is token = (:1.18: )
Shifting token = (:1.18: )
Entering state 236
Reading a token
--rule src/parse/scantiger.ll:138 start(0) 1,18:" "
--rule src/parse/scantiger.ll:142 start(0) 1,19:"("
Next token is token ( (:1.20: )
Shifting token ( (:1.20: )
Entering state 12
Reading a token
--rule src/parse/scantiger.ll:196 start(0) 1,20:"_exp"
Next token is token _exp (:1.20-24: )
Shifting token _exp (:1.20-24: )
Entering state 20
Reading a token
--rule src/parse/scantiger.ll:142 start(0) 1,24:"("
Next token is token ( (:1.20-25: )
Shifting token ( (:1.20-25: )
Entering state 61
Reading a token
--rule src/parse/scantiger.ll:119 start(0) 1,25:"0"
Next token is token integer (:1.20-26: 0)
Shifting token integer (:1.20-26: 0)
Entering state 108
Reading a token
--rule src/parse/scantiger.ll:143 start(0) 1,26:")"
Next token is token ) (:1.20-27: )
Shifting token ) (:1.20-27: )
Entering state 167
Reducing stack 0 by rule 38 (line 402):
$1 = token _exp (:1.20-24: )
$2 = token ( (:1.20-25: )
$3 = token integer (:1.20-26: 0)
$4 = token ) (:1.20-27: )
-> $$ = nterm exp.1 (:1.20-27: print("Hello, World!\n"))
Entering state 26
Reducing stack 0 by rule 35 (line 395):
$1 = nterm exp.1 (:1.20-27: print("Hello, World!\n"))
-> $$ = nterm exp (:1.20-27: print("Hello, World!\n"))
Entering state 50
Reading a token
--rule src/parse/scantiger.ll:152 start(0) 1,27:";"
Next token is token ; (:1.20-28: )
Reducing stack 0 by rule 50 (line 434):
$1 = nterm exp (:1.20-27: print("Hello, World!\n"))
-> $$ = nterm exps.1 (:1.20-27: print("Hello, World!\n"))
Entering state 51
Next token is token ; (:1.20-28: )
Shifting token ; (:1.20-28: )
Entering state 102
Reading a token
--rule src/parse/scantiger.ll:138 start(0) 1,28:" "
--rule src/parse/scantiger.ll:142 start(0) 1,29:"("
Next token is token ( (:1.30: )
Shifting token ( (:1.30: )
Entering state 12
Reading a token
--rule src/parse/scantiger.ll:143 start(0) 1,30:")"
Next token is token ) (:1.30-31: )
Reducing stack 0 by rule 54 (line 446):
-> $$ = nterm exps.0.2 (:1.31: )
Entering state 53
Next token is token ) (:1.30-31: )
Shifting token ) (:1.30-31: )
Entering state 103
Reducing stack 0 by rule 11 (line 325):
$1 = token ( (:1.30: )
$2 = nterm exps.0.2 (:1.31: )
$3 = token ) (:1.30-31: )
-> $$ = nterm exp (:1.30-31: ())
Entering state 157
Reading a token
--rule src/parse/scantiger.ll:143 start(0) 1,31:")"
Next token is token ) (:1.30-32: )
Reducing stack 0 by rule 53 (line 441):
$1 = nterm exps.1 (:1.20-27: print("Hello, World!\n"))
$2 = token ; (:1.20-28: )
$3 = nterm exp (:1.30-31: ())
-> $$ = nterm exps.2 (:1.20-31: print("Hello, World!\n"), ())
Entering state 52
Reducing stack 0 by rule 55 (line 447):
$1 = nterm exps.2 (:1.20-31: print("Hello, World!\n"), ())
-> $$ = nterm exps.0.2 (:1.20-31: print("Hello, World!\n"), ())
Entering state 53
Next token is token ) (:1.30-32: )
Shifting token ) (:1.30-32: )
Entering state 103
Reducing stack 0 by rule 11 (line 325):
$1 = token ( (:1.20: )
$2 = nterm exps.0.2 (:1.20-31: print("Hello, World!\n"), ())
$3 = token ) (:1.30-32: )
-> $$ = nterm exp (:1.20-32: (
print("Hello, World!\n");
()
))
Entering state 244
Reading a token
--EOF start(0)
Now at end of input.
Reducing stack 0 by rule 95 (line 625):
$1 = token function (:1.1-8: )
$2 = token identifier (:1.10-14: _main)
$3 = token ( (:1.10-15: )
$4 = nterm funargs (:1.16: )
$5 = token ) (:1.10-16: )
$6 = nterm typeid.opt (:1.17: )
$7 = token = (:1.18: )
$8 = nterm exp (:1.20-32: (
print("Hello, World!\n");
()
))
-> $$ = nterm fundec (:1.1-32:
function _main() =
(
print("Hello, World!\n");
()
))
Entering state 37
Now at end of input.
Reducing stack 0 by rule 93 (line 620):
$1 = nterm fundec (:1.1-32:
function _main() =
(
print("Hello, World!\n");
()
))
-> $$ = nterm funchunk (:1.1-32:
function _main() =
(
print("Hello, World!\n");
()
))
Entering state 36
Now at end of input.
Reducing stack 0 by rule 56 (line 466):
-> $$ = nterm chunks (:1.33: )
Entering state 85
Reducing stack 0 by rule 59 (line 470):
$1 = nterm funchunk (:1.1-32:
function _main() =
(
print("Hello, World!\n");
()
))
$2 = nterm chunks (:1.33: )
-> $$ = nterm chunks (:1.1-32:
function _main() =
(
print("Hello, World!\n");
()
))
Entering state 29
Reducing stack 0 by rule 2 (line 292):
$1 = nterm chunks (:1.1-32:
function _main() =
(
print("Hello, World!\n");
()
))
-> $$ = nterm program (:1.1-32: )
Entering state 24
Now at end of input.
Shifting token end of file (:1.32: )
Entering state 65
Cleanup: popping token end of file (:1.32: )
Cleanup: popping nterm program (:1.1-32: )
$ echo $?
0
A lexical error must be properly diagnosed and reported. The following examples display the location: this is not required for |COMPILER|-0, nevertheless, an error message on the standard error output is required.
"\z does not exist."
$ tc -X --parse back-zee.tig
back-zee.tig:1.1-3: unrecognized escape: \z
$ echo $?
2
Similarly for syntactical errors.
a++
$ tc -X --parse postinc.tig
postinc.tig:1.1-3: syntax error, unexpected +
$ echo $?
3