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:

simple.tig
print("Hello, World!\n")
tc simple.tig
$ 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
$ SCAN=1 PARSE=1 tc -X --parse simple.tig
Parsing file: "simple.tig"
Starting parse
Entering state 0
Reading a token: --(end of buffer or a NUL)
--accepting rule at line 197("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: --accepting rule at line 139("(")
Next token is token "(" (simple.tig:1.6: )
Reducing stack 0 by rule 102 (line 633):
   $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.6: )
Shifting token "(" (simple.tig:1.6: )
Entering state 87
Reading a token: --accepting rule at line 198(""")
--accepting rule at line 267("Hello, World!")
--accepting rule at line 254("\n")
--accepting rule at line 229(""")
Next token is token "string" (simple.tig:1.7-23: Hello, World!
)
Shifting token "string" (simple.tig:1.7-23: Hello, World!
)
Entering state 1
Reducing stack 0 by rule 4 (line 298):
   $1 = token "string" (simple.tig:1.7-23: Hello, World!
)
-> $$ = nterm exp (simple.tig:1.7-23: "Hello, World!\n")
Entering state 134
Reading a token: --accepting rule at line 140(")")
Next token is token ")" (simple.tig:1.24: )
Reducing stack 0 by rule 47 (line 424):
   $1 = nterm exp (simple.tig:1.7-23: "Hello, World!\n")
-> $$ = nterm args.1 (simple.tig:1.7-23: "Hello, World!\n")
Entering state 136
Next token is token ")" (simple.tig:1.24: )
Reducing stack 0 by rule 46 (line 419):
   $1 = nterm args.1 (simple.tig:1.7-23: "Hello, World!\n")
-> $$ = nterm args (simple.tig:1.7-23: "Hello, World!\n")
Entering state 135
Next token is token ")" (simple.tig:1.24: )
Shifting token ")" (simple.tig:1.24: )
Entering state 178
Reducing stack 0 by rule 6 (line 306):
   $1 = nterm funid (simple.tig:1.1-5: print)
   $2 = token "(" (simple.tig:1.6: )
   $3 = nterm args (simple.tig:1.7-23: "Hello, World!\n")
   $4 = token ")" (simple.tig:1.24: )
-> $$ = nterm exp (simple.tig:1.1-24: print("Hello, World!\n"))
Entering state 25
Reading a token: --(end of buffer or a NUL)
--accepting rule at line 135("
")
--(end of buffer or a NUL)
--EOF (start condition 0)
Now at end of input.
Reducing stack 0 by rule 1 (line 287):
   $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" (simple.tig:2.1: )
Entering state 65
Cleanup: popping token "end of file" (simple.tig:2.1: )
Cleanup: popping nterm program (simple.tig:1.1-24: )
Parsing string: function _main() = (_exp(0); ())
Starting parse
Entering state 0
Reading a token: --(end of buffer or a NUL)
--accepting rule at line 165("function")
Next token is token "function" (:1.1-8: )
Shifting token "function" (:1.1-8: )
Entering state 8
Reading a token: --accepting rule at line 134(" ")
--accepting rule at line 196("_main")
Next token is token "identifier" (:1.10-14: _main)
Shifting token "identifier" (:1.10-14: _main)
Entering state 45
Reading a token: --accepting rule at line 139("(")
Next token is token "(" (:1.15: )
Shifting token "(" (:1.15: )
Entering state 96
Reading a token: --accepting rule at line 140(")")
Next token is token ")" (:1.16: )
Reducing stack 0 by rule 97 (line 612):
-> $$ = nterm funargs (:1.16: )
Entering state 148
Next token is token ")" (:1.16: )
Shifting token ")" (:1.16: )
Entering state 191
Reading a token: --accepting rule at line 134(" ")
--accepting rule at line 153("=")
Next token is token "=" (:1.18: )
Reducing stack 0 by rule 88 (line 574):
-> $$ = nterm typeid.opt (:1.17: )
Entering state 220
Next token is token "=" (:1.18: )
Shifting token "=" (:1.18: )
Entering state 236
Reading a token: --accepting rule at line 134(" ")
--accepting rule at line 139("(")
Next token is token "(" (:1.20: )
Shifting token "(" (:1.20: )
Entering state 12
Reading a token: --accepting rule at line 192("_exp")
Next token is token "_exp" (:1.21-24: )
Shifting token "_exp" (:1.21-24: )
Entering state 21
Reading a token: --accepting rule at line 139("(")
Next token is token "(" (:1.25: )
Shifting token "(" (:1.25: )
Entering state 62
Reading a token: --accepting rule at line 114("0")
Next token is token "integer" (:1.26: 0)
Shifting token "integer" (:1.26: 0)
Entering state 109
Reading a token: --accepting rule at line 140(")")
Next token is token ")" (:1.27: )
Shifting token ")" (:1.27: )
Entering state 168
Reducing stack 0 by rule 38 (line 399):
   $1 = token "_exp" (:1.21-24: )
   $2 = token "(" (:1.25: )
   $3 = token "integer" (:1.26: 0)
   $4 = token ")" (:1.27: )
-> $$ = nterm exp.1 (:1.21-27: print("Hello, World!\n"))
Entering state 26
Reducing stack 0 by rule 35 (line 392):
   $1 = nterm exp.1 (:1.21-27: print("Hello, World!\n"))
-> $$ = nterm exp (:1.21-27: print("Hello, World!\n"))
Entering state 50
Reading a token: --accepting rule at line 149(";")
Next token is token ";" (:1.28: )
Reducing stack 0 by rule 50 (line 431):
   $1 = nterm exp (:1.21-27: print("Hello, World!\n"))
-> $$ = nterm exps.1 (:1.21-27: print("Hello, World!\n"))
Entering state 51
Next token is token ";" (:1.28: )
Shifting token ";" (:1.28: )
Entering state 102
Reading a token: --accepting rule at line 134(" ")
--accepting rule at line 139("(")
Next token is token "(" (:1.30: )
Shifting token "(" (:1.30: )
Entering state 12
Reading a token: --accepting rule at line 140(")")
Next token is token ")" (:1.31: )
Reducing stack 0 by rule 54 (line 443):
-> $$ = nterm exps.0.2 (:1.31: )
Entering state 53
Next token is token ")" (:1.31: )
Shifting token ")" (:1.31: )
Entering state 103
Reducing stack 0 by rule 11 (line 323):
   $1 = token "(" (:1.30: )
   $2 = nterm exps.0.2 (:1.31: )
   $3 = token ")" (:1.31: )
-> $$ = nterm exp (:1.30-31: ())
Entering state 157
Reading a token: --(end of buffer or a NUL)
--accepting rule at line 140(")")
Next token is token ")" (:1.32: )
Reducing stack 0 by rule 53 (line 438):
   $1 = nterm exps.1 (:1.21-27: print("Hello, World!\n"))
   $2 = token ";" (:1.28: )
   $3 = nterm exp (:1.30-31: ())
-> $$ = nterm exps.2 (:1.21-31: print("Hello, World!\n"), ())
Entering state 52
Reducing stack 0 by rule 55 (line 444):
   $1 = nterm exps.2 (:1.21-31: print("Hello, World!\n"), ())
-> $$ = nterm exps.0.2 (:1.21-31: print("Hello, World!\n"), ())
Entering state 53
Next token is token ")" (:1.32: )
Shifting token ")" (:1.32: )
Entering state 103
Reducing stack 0 by rule 11 (line 323):
   $1 = token "(" (:1.20: )
   $2 = nterm exps.0.2 (:1.21-31: print("Hello, World!\n"), ())
   $3 = token ")" (:1.32: )
-> $$ = nterm exp (:1.20-32: (
  print("Hello, World!\n");
  ()
))
Entering state 244
Reading a token: --(end of buffer or a NUL)
--EOF (start condition 0)
Now at end of input.
Reducing stack 0 by rule 95 (line 605):
   $1 = token "function" (:1.1-8: )
   $2 = token "identifier" (:1.10-14: _main)
   $3 = token "(" (:1.15: )
   $4 = nterm funargs (:1.16: )
   $5 = token ")" (:1.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 600):
   $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 454):
-> $$ = nterm chunks (:1.33: )
Entering state 85
Reducing stack 0 by rule 59 (line 458):
   $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 290):
   $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.33: )
Entering state 65
Cleanup: popping token "end of file" (:1.33: )
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.

back-zee.tig
"\z does not exist."
tc -X --parse back-zee.tig
$ tc -X --parse back-zee.tig
back-zee.tig:1.1-3: unrecognized escape: \z
$ echo $?
2

Similarly for syntactical errors.

postinc.tig
a++
tc -X --parse postinc.tig
$ tc -X --parse postinc.tig
postinc.tig:1.3: syntax error, unexpected +
$ echo $?
3