Next: , Previous: , Up: TC-1   [Contents][Index]


4.3.2 TC-1 Samples

The only information the compiler provides is about lexical and syntax errors. If there are no errors, the compiler shuts up, and exits successfully:

/* An array type and an array variable. */
let
  type  arrtype = array of int
  var arr1 : arrtype := arrtype [10] of 0
in
  arr1[2]
end

File 4.4: test01.tig

$ tc -X --parse test01.tig

Example 4.5: tc -X --parse test01.tig

If there are lexical errors, the exit status is 2, and an error message is output on the standard error output. Its format is standard and mandatory: file, (precise) location, and then the message (see Errors in Tiger Compiler Reference Manual).

1
 /* This comments starts at /* 2.2 */

File 4.5: unterminated-comment.tig

$ tc -X --parse unterminated-comment.tig
error→unterminated-comment.tig:2.2-3.0: unexpected end of file in a comment
⇒2

Example 4.6: tc -X --parse unterminated-comment.tig

If there are syntax errors, the exit status is set to 3:

let var a : nil := ()
in
  1
end

File 4.6: type-nil.tig

$ tc -X --parse type-nil.tig
error→type-nil.tig:1.13-15: syntax error, unexpected nil, expecting identifier or _namety
⇒3

Example 4.7: tc -X --parse type-nil.tig

If there are errors which are non lexical, nor syntactic (Windows will not pass by me):

$ tc C:/TIGER/SAMPLE.TIG
error→tc: cannot open `C:/TIGER/SAMPLE.TIG': No such file or directory
⇒1

Example 4.8: tc C:/TIGER/SAMPLE.TIG

The option --parse-trace, which relies on Bison’s %debug and %printer directives, must work properly5:

a + "a"

File 4.7: a+a.tig

$ tc -X --parse-trace --parse a+a.tig
error→Parsing file: "a+a.tig"
error→Starting parse
error→Entering state 0
error→Reading a token: Next token is token "identifier" (a+a.tig:1.1: a)
error→Shifting token "identifier" (a+a.tig:1.1: a)
error→Entering state 2
error→Reading a token: Next token is token "+" (a+a.tig:1.3: )
error→Reducing stack 0 by rule 90 (line 585):
error→   $1 = token "identifier" (a+a.tig:1.1: a)
error→-> $$ = nterm varid (a+a.tig:1.1: a)
error→Entering state 33
error→Reducing stack 0 by rule 38 (line 402):
error→   $1 = nterm varid (a+a.tig:1.1: a)
error→-> $$ = nterm lvalue (a+a.tig:1.1: a)
error→Entering state 26
error→Next token is token "+" (a+a.tig:1.3: )
error→Reducing stack 0 by rule 35 (line 395):
error→   $1 = nterm lvalue (a+a.tig:1.1: a)
error→-> $$ = nterm exp (a+a.tig:1.1: a)
error→Entering state 25
error→Next token is token "+" (a+a.tig:1.3: )
error→Shifting token "+" (a+a.tig:1.3: )
error→Entering state 74
error→Reading a token: Next token is token "string" (a+a.tig:1.5-7: a)
error→Shifting token "string" (a+a.tig:1.5-7: a)
error→Entering state 1
error→Reducing stack 0 by rule 4 (line 296):
error→   $1 = token "string" (a+a.tig:1.5-7: a)
error→-> $$ = nterm exp (a+a.tig:1.5-7: "a")
error→Entering state 119
error→Reading a token: Now at end of input.
error→Reducing stack 0 by rule 29 (line 376):
error→   $1 = nterm exp (a+a.tig:1.1: a)
error→   $2 = token "+" (a+a.tig:1.3: )
error→   $3 = nterm exp (a+a.tig:1.5-7: "a")
error→-> $$ = nterm exp (a+a.tig:1.1-7: (a + "a"))
error→Entering state 25
error→Now at end of input.
error→Reducing stack 0 by rule 1 (line 287):
error→   $1 = nterm exp (a+a.tig:1.1-7: (a + "a"))
error→-> $$ = nterm program (a+a.tig:1.1-7: )
error→Entering state 24
error→Now at end of input.
error→Shifting token "end of file" (a+a.tig:2.1: )
error→Entering state 63
error→Cleanup: popping token "end of file" (a+a.tig:2.1: )
error→Cleanup: popping nterm program (a+a.tig:1.1-7: )
error→Parsing string: function _main() = (_exp(0); ())
error→Starting parse
error→Entering state 0
error→Reading a token: Next token is token "function" (:1.1-8: )
error→Shifting token "function" (:1.1-8: )
error→Entering state 8
error→Reading a token: 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: Next token is token "(" (:1.15: )
error→Shifting token "(" (:1.15: )
error→Entering state 93
error→Reading a token: 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: 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: Next token is token "(" (:1.20: )
error→Shifting token "(" (:1.20: )
error→Entering state 12
error→Reading a token: Next token is token "_exp" (:1.21-24: )
error→Shifting token "_exp" (:1.21-24: )
error→Entering state 21
error→Reading a token: Next token is token "(" (:1.25: )
error→Shifting token "(" (:1.25: )
error→Entering state 60
error→Reading a token: Next token is token "integer" (:1.26: 0)
error→Shifting token "integer" (:1.26: 0)
error→Entering state 106
error→Reading a token: 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: (a + "a"))
error→Entering state 48
error→Reading a token: Next token is token ";" (:1.28: )
error→Reducing stack 0 by rule 48 (line 424):
error→   $1 = nterm exp (:1.21-27: (a + "a"))
error→-> $$ = nterm exps.1 (:1.21-27: (a + "a"))
error→Entering state 49
error→Next token is token ";" (:1.28: )
error→Shifting token ";" (:1.28: )
error→Entering state 99
error→Reading a token: Next token is token "(" (:1.30: )
error→Shifting token "(" (:1.30: )
error→Entering state 12
error→Reading a token: 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: Next token is token ")" (:1.32: )
error→Reducing stack 0 by rule 51 (line 431):
error→   $1 = nterm exps.1 (:1.21-27: (a + "a"))
error→   $2 = token ";" (:1.28: )
error→   $3 = nterm exp (:1.30-31: ())
error→-> $$ = nterm exps.2 (:1.21-31: (a + "a"), ())
error→Entering state 50
error→Reducing stack 0 by rule 53 (line 437):
error→   $1 = nterm exps.2 (:1.21-31: (a + "a"), ())
error→-> $$ = nterm exps.0.2 (:1.21-31: (a + "a"), ())
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: (a + "a"), ())
error→   $3 = token ")" (:1.32: )
error→-> $$ = nterm exp (:1.20-32: (
error→  (a + "a");
error→  ()
error→))
error→Entering state 239
error→Reading a token: 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→  (a + "a");
error→  ()
error→))
error→-> $$ = nterm fundec (:1.1-32: 
error→function _main() =
error→  (
error→    (a + "a");
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→    (a + "a");
error→    ()
error→  ))
error→-> $$ = nterm fundecs (:1.1-32: 
error→function _main() =
error→  (
error→    (a + "a");
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→    (a + "a");
error→    ()
error→  ))
error→   $2 = nterm decs (:1.33: )
error→-> $$ = nterm decs (:1.1-32: 
error→function _main() =
error→  (
error→    (a + "a");
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→    (a + "a");
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: )

Example 4.9: tc -X --parse-trace --parse a+a.tig

Note that (i), --parse is needed, (ii), it cannot see that the variable is not declared nor that there is a type checking error, since type checking... is not implemented, and (iii), the output might be slightly different, depending upon the version of Bison you use. But what matters is that one can see the items: ‘"identifier" a’, ‘"string" a’.


Footnotes

(5)

For the time being, forget about -X.


Next: , Previous: , Up: TC-1   [Contents][Index]