The input to the semantic checker will be a program and the output will be information about the types of the variables in the program or an error message if there is type mismatch or other semantic error. The syntax for type declarations is given by the following grammar:
(3) type_decl_section -> TYPE type_decl_list
(4) type_decl_section -> // epsilon
(5) type_decl_list -> type_decl type_decl_list
(6) type_decl_list -> type_decl
(7) type_decl -> id_list COLON type_name SEMICOLON
(8) type_name -> REAL
(9) type_name -> INT
(10) type_name -> BOOLEAN
(11) type_name -> STRING
(12) type_name -> ID
The language allows the explicit declaration of variables. This is done
in a var_decl_section whose syntax is defined as follows:
(13) var_decl_section -> VAR var_decl_list
(14) var_decl_section -> // epsilon
(15) var_decl_list -> var_decl var_decl_list
(16) var_decl_list -> var_decl
(17) var_decl -> id_list COLON type_name SEMICOLON
(18) id_list -> ID COMMA id_list
(19) id_list -> ID
The body of the program consists of a list of statements between two braces.
The grammar rules are:
(20) body -> LBRACE stmt_list RBRACE
(21) stmt_list -> stmt stmt_list
(22) stmt_list -> stmt
A statement can be either an assignment (assign_stmt) or a while statement
(while_stmt). An assign_stmt assigns an expression to a variable. A while
statement has 3 parts: (1) the WHILE keyword, (2) a condition, and (3) a body
(this is a recursive definition). The grammar rules for stmt are the following:
(23) stmt -> while_stmt
(24) stmt -> assign_stmt
(25) while_stmt -> WHILE condition body
(26) assign_stmt -> ID EQUAL expr SEMICOLON
(27) expr -> term PLUS expr
(28) expr -> term MINUS expr
(29) expr -> term
(30) term -> factor MULT term
(31) term -> factor DIV term
(32) term -> factor
(33) factor -> LPAREN expr RPAREN
(34) factor -> NUM
(35) factor -> REALNUM
(36) factor -> ID
(37) condition -> ID
(38) condition -> primary relop primary
(39) primary -> ID
(40) primary -> NUM
(41) primary -> REALNUM
(42) relop -> GREATER
(43) relop -> GTEQ
(44) relop -> LESS
(45) relop -> NOTEQUAL
(46) relop -> LTEQ
We have the following main rules (MR) to be enforced:
<br>(MR1) Structural equivalence is enforced
<br>(MR2) Each variable (wether or not it is explicitly declared) has a
fixed type that does not change
<br>(MR3) Each type name (wether or not it is explicitly declared) is fixed
and does not change
<br>(MR4) Each type is assigned an integer
<br>(MR5) INT is assigned 10, REAL is assigned 11, STRING is assigned 12, and
BOOLEAN is assigned 13.
<br>(MR6) If a : b; is a type declaration, then #a = #b, where #x is the
number assigned to x.
<br>(MR7) Types that are implicitly declared are assigned unique numbers
when they are introduced. The numbers should be greater than 13.
<br>(MR8) Each variable is assigned an integer (type number)
<br>(MR9) If a : t; is a variable declaration, then #a = #t, where #x is
the number assigned to x.
<br>(MR10) Variables that are implicitly declared are assigned unique numbers
when they are introduced. The numbers should be greater than 13.