NTU Compiler Technology Project 2

NTU Compiler Technology Project 2

tags: NTU_CT NTU Compiler Techonology of Programming Language Scanner

:::spoiler [TOC] :::

Objective

This is compiler project 2 that aim to construct the scanner by handmade with some sample code. The output is symbol table that the frequency of each identifier and the result is sorted.

Run and Test

1
2
$ make /*use make in src folder*/
$ ./scanner ../test/sample1.ac

Output of each sample

  • For sample1.ac
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
      /* 1: this is just a sample */
      /* 2: this is a 3-line
          comment
          */
      /* 3: this is /* the end ***/
      Frequency of identifiers:
      a			3 
      b			2 
      c			3 
      d			4 
      e			1 
      f			2 
      func		2 
      g			2 
      main		3 
      read		1 
      write		1 
    
  • For sample2.ac

    1
    2
    3
    4
    5
    6
    7
      Frequency of identifiers:
      loopvar		6 
      main		1 
      n			3 
      read		1 
      sum			5 
      write		3
    
  • For sample3.ac

    1
    2
    3
    4
    5
    6
    7
    Frequency of identifiers:
    fact		3 
    main		1 
    n			9 
    read		1 
    result		4 
    write		3
    
  • For sample4.ac

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
      /* 1: this is * just a / sample */
      /* 2: this is a 3-line
      	comment
      	*/
      /* 3: this is /* the end ***/
      Frequency of identifiers:
      a1			1 
      abc			4 
      b1			1 
      def			3 
      main		1 
      main_1		1 
      n			3 
      read		1 
      write		2 
      z_123_x_45	1
    

Reference