Next: PTHL Code to Write, Previous: PTHL Goals, Up: PTHL (TC-0) [Contents][Index]
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
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 error→Parsing file: "simple.tig" error→Starting parse error→Entering state 0 error→Reading a token: --(end of buffer or a NUL) error→--accepting rule at line 196("print") error→Next token is token "identifier" (simple.tig:1.1-5: print) error→Shifting token "identifier" (simple.tig:1.1-5: print) error→Entering state 2 error→Reading a token: --accepting rule at line 138("(") error→Next token is token "(" (simple.tig:1.6: ) error→Reducing stack 0 by rule 100 (line 626): error→ $1 = token "identifier" (simple.tig:1.1-5: print) error→-> $$ = nterm funid (simple.tig:1.1-5: print) error→Entering state 36 error→Next token is token "(" (simple.tig:1.6: ) error→Shifting token "(" (simple.tig:1.6: ) error→Entering state 85 error→Reading a token: --accepting rule at line 197(""") error→--accepting rule at line 266("Hello, World!") error→--accepting rule at line 253("\n") error→--accepting rule at line 228(""") error→Next token is token "string" (simple.tig:1.7-23: Hello, World! error→) error→Shifting token "string" (simple.tig:1.7-23: Hello, World! error→) error→Entering state 1 error→Reducing stack 0 by rule 4 (line 296): error→ $1 = token "string" (simple.tig:1.7-23: Hello, World! error→) error→-> $$ = nterm exp (simple.tig:1.7-23: "Hello, World!\n") error→Entering state 131 error→Reading a token: --accepting rule at line 139(")") error→Next token is token ")" (simple.tig:1.24: ) error→Reducing stack 0 by rule 45 (line 417): error→ $1 = nterm exp (simple.tig:1.7-23: "Hello, World!\n") error→-> $$ = nterm args.1 (simple.tig:1.7-23: "Hello, World!\n") error→Entering state 133 error→Next token is token ")" (simple.tig:1.24: ) error→Reducing stack 0 by rule 44 (line 412): error→ $1 = nterm args.1 (simple.tig:1.7-23: "Hello, World!\n") error→-> $$ = nterm args (simple.tig:1.7-23: "Hello, World!\n") error→Entering state 132 error→Next token is token ")" (simple.tig:1.24: ) error→Shifting token ")" (simple.tig:1.24: ) error→Entering state 174 error→Reducing stack 0 by rule 6 (line 304): error→ $1 = nterm funid (simple.tig:1.1-5: print) error→ $2 = token "(" (simple.tig:1.6: ) error→ $3 = nterm args (simple.tig:1.7-23: "Hello, World!\n") error→ $4 = token ")" (simple.tig:1.24: ) error→-> $$ = nterm exp (simple.tig:1.1-24: print("Hello, World!\n")) error→Entering state 25 error→Reading a token: --(end of buffer or a NUL) error→--accepting rule at line 134(" error→") error→--(end of buffer or a NUL) error→--EOF (start condition 0) error→Now at end of input. error→Reducing stack 0 by rule 1 (line 287): error→ $1 = nterm exp (simple.tig:1.1-24: print("Hello, World!\n")) error→-> $$ = nterm program (simple.tig:1.1-24: ) error→Entering state 24 error→Now at end of input. error→Shifting token "end of file" (simple.tig:2.1: ) error→Entering state 63 error→Cleanup: popping token "end of file" (simple.tig:2.1: ) error→Cleanup: popping nterm program (simple.tig:1.1-24: ) error→Parsing string: function _main() = (_exp(0); ()) error→Starting parse error→Entering state 0 error→Reading a token: --(end of buffer or a NUL) error→--accepting rule at line 164("function") error→Next token is token "function" (:1.1-8: ) error→Shifting token "function" (:1.1-8: ) error→Entering state 8 error→Reading a token: --accepting rule at line 133(" ") error→--accepting rule at line 195("_main") error→Next token is token "identifier" (:1.10-14: _main) error→Shifting token "identifier" (:1.10-14: _main) error→Entering state 43 error→Reading a token: --accepting rule at line 138("(") error→Next token is token "(" (:1.15: ) error→Shifting token "(" (:1.15: ) error→Entering state 93 error→Reading a token: --accepting rule at line 139(")") error→Next token is token ")" (:1.16: ) error→Reducing stack 0 by rule 95 (line 605): error→-> $$ = nterm funargs (:1.16: ) error→Entering state 144 error→Next token is token ")" (:1.16: ) error→Shifting token ")" (:1.16: ) error→Entering state 186 error→Reading a token: --accepting rule at line 133(" ") error→--accepting rule at line 152("=") error→Next token is token "=" (:1.18: ) error→Reducing stack 0 by rule 86 (line 567): error→-> $$ = nterm typeid.opt (:1.17: ) error→Entering state 215 error→Next token is token "=" (:1.18: ) error→Shifting token "=" (:1.18: ) error→Entering state 231 error→Reading a token: --accepting rule at line 133(" ") error→--accepting rule at line 138("(") error→Next token is token "(" (:1.20: ) error→Shifting token "(" (:1.20: ) error→Entering state 12 error→Reading a token: --accepting rule at line 191("_exp") error→Next token is token "_exp" (:1.21-24: ) error→Shifting token "_exp" (:1.21-24: ) error→Entering state 21 error→Reading a token: --accepting rule at line 138("(") error→Next token is token "(" (:1.25: ) error→Shifting token "(" (:1.25: ) error→Entering state 60 error→Reading a token: --accepting rule at line 113("0") error→Next token is token "integer" (:1.26: 0) error→Shifting token "integer" (:1.26: 0) error→Entering state 106 error→Reading a token: --accepting rule at line 139(")") error→Next token is token ")" (:1.27: ) error→Shifting token ")" (:1.27: ) error→Entering state 164 error→Reducing stack 0 by rule 37 (line 397): error→ $1 = token "_exp" (:1.21-24: ) error→ $2 = token "(" (:1.25: ) error→ $3 = token "integer" (:1.26: 0) error→ $4 = token ")" (:1.27: ) error→-> $$ = nterm exp (:1.21-27: print("Hello, World!\n")) error→Entering state 48 error→Reading a token: --accepting rule at line 148(";") error→Next token is token ";" (:1.28: ) error→Reducing stack 0 by rule 48 (line 424): error→ $1 = nterm exp (:1.21-27: print("Hello, World!\n")) error→-> $$ = nterm exps.1 (:1.21-27: print("Hello, World!\n")) error→Entering state 49 error→Next token is token ";" (:1.28: ) error→Shifting token ";" (:1.28: ) error→Entering state 99 error→Reading a token: --accepting rule at line 133(" ") error→--accepting rule at line 138("(") error→Next token is token "(" (:1.30: ) error→Shifting token "(" (:1.30: ) error→Entering state 12 error→Reading a token: --accepting rule at line 139(")") error→Next token is token ")" (:1.31: ) error→Reducing stack 0 by rule 52 (line 436): error→-> $$ = nterm exps.0.2 (:1.31: ) error→Entering state 51 error→Next token is token ")" (:1.31: ) error→Shifting token ")" (:1.31: ) error→Entering state 100 error→Reducing stack 0 by rule 11 (line 321): error→ $1 = token "(" (:1.30: ) error→ $2 = nterm exps.0.2 (:1.31: ) error→ $3 = token ")" (:1.31: ) error→-> $$ = nterm exp (:1.30-31: ()) error→Entering state 153 error→Reading a token: --(end of buffer or a NUL) error→--accepting rule at line 139(")") error→Next token is token ")" (:1.32: ) error→Reducing stack 0 by rule 51 (line 431): error→ $1 = nterm exps.1 (:1.21-27: print("Hello, World!\n")) error→ $2 = token ";" (:1.28: ) error→ $3 = nterm exp (:1.30-31: ()) error→-> $$ = nterm exps.2 (:1.21-31: print("Hello, World!\n"), ()) error→Entering state 50 error→Reducing stack 0 by rule 53 (line 437): error→ $1 = nterm exps.2 (:1.21-31: print("Hello, World!\n"), ()) error→-> $$ = nterm exps.0.2 (:1.21-31: print("Hello, World!\n"), ()) error→Entering state 51 error→Next token is token ")" (:1.32: ) error→Shifting token ")" (:1.32: ) error→Entering state 100 error→Reducing stack 0 by rule 11 (line 321): error→ $1 = token "(" (:1.20: ) error→ $2 = nterm exps.0.2 (:1.21-31: print("Hello, World!\n"), ()) error→ $3 = token ")" (:1.32: ) error→-> $$ = nterm exp (:1.20-32: ( error→ print("Hello, World!\n"); error→ () error→)) error→Entering state 239 error→Reading a token: --(end of buffer or a NUL) error→--EOF (start condition 0) error→Now at end of input. error→Reducing stack 0 by rule 93 (line 598): error→ $1 = token "function" (:1.1-8: ) error→ $2 = token "identifier" (:1.10-14: _main) error→ $3 = token "(" (:1.15: ) error→ $4 = nterm funargs (:1.16: ) error→ $5 = token ")" (:1.16: ) error→ $6 = nterm typeid.opt (:1.17: ) error→ $7 = token "=" (:1.18: ) error→ $8 = nterm exp (:1.20-32: ( error→ print("Hello, World!\n"); error→ () error→)) error→-> $$ = nterm fundec (:1.1-32: error→function _main() = error→ ( error→ print("Hello, World!\n"); error→ () error→ )) error→Entering state 35 error→Now at end of input. error→Reducing stack 0 by rule 91 (line 593): error→ $1 = nterm fundec (:1.1-32: error→function _main() = error→ ( error→ print("Hello, World!\n"); error→ () error→ )) error→-> $$ = nterm fundecs (:1.1-32: error→function _main() = error→ ( error→ print("Hello, World!\n"); error→ () error→ )) error→Entering state 34 error→Now at end of input. error→Reducing stack 0 by rule 54 (line 447): error→-> $$ = nterm decs (:1.33: ) error→Entering state 83 error→Reducing stack 0 by rule 57 (line 451): error→ $1 = nterm fundecs (:1.1-32: error→function _main() = error→ ( error→ print("Hello, World!\n"); error→ () error→ )) error→ $2 = nterm decs (:1.33: ) error→-> $$ = nterm decs (:1.1-32: error→function _main() = error→ ( error→ print("Hello, World!\n"); error→ () error→ )) error→Entering state 27 error→Reducing stack 0 by rule 2 (line 289): error→ $1 = nterm decs (:1.1-32: error→function _main() = error→ ( error→ print("Hello, World!\n"); error→ () error→ )) error→-> $$ = nterm program (:1.1-32: ) error→Entering state 24 error→Now at end of input. error→Shifting token "end of file" (:1.33: ) error→Entering state 63 error→Cleanup: popping token "end of file" (:1.33: ) error→Cleanup: popping nterm program (:1.1-32: )
A lexical error must be properly diagnosed and reported. The following (generated) examples display the location: this is not required for TC-0; nevertheless, an error message on the standard error output is required.
"\z does not exist."
$ tc -X --parse back-zee.tig error→back-zee.tig:1.1-3: unrecognized escape: \z ⇒2
Similarly for syntactical errors.
a++
$ tc -X --parse postinc.tig error→postinc.tig:1.3: syntax error, unexpected + ⇒3
Next: PTHL Code to Write, Previous: PTHL Goals, Up: PTHL (TC-0) [Contents][Index]