## [CS143-PA2] Parser

### Target

write a cool parser using bison

### 1. Type Declaration

temporary types

• class_list (for program) : [[class;]]+
feature_list (for class) : [[feature;]]*
method (for feature) : ID( [ formal [[, formal]]* ] ) : TYPE { expr }
attribute (for feature) : ID : TYPE [ <- expr ]
formal_list (for method in feature) : [formal [[,formal]]* ]
expression_list (for dispatch in expression) : [expr [[,expr]]* ]
expression_blocks (for blocks in expression) : [[expr;]]+
let_expr (for let in expression) : ID : TYPE [ <- expr ] [[,ID : TYPE [ <- expr ] ]]* in expr
branch (for branch in case in expression) : ID : TYPE => expr;
case_list (for case in expression) : [[ID : TYPE => expr; ]]+

### 2. Precedence

All binary operations are left-associative, with the exception of assignment, which is right-associative,
and the three comparison operations, which do not associate.

### 3. Write rules using Tree package

6 Abstract Syntax Trees

cool-tour.pdf

• The AST data type provides, for each kind of Cool construct, a class for representing expressions of that kind. Objects of these classes are nodes in Cool abstract syntax trees.

• Find constructor declarations in cool-tree.aps or include/cool-tree.h
See 6.5 The Constructors for the usage

constructor of lists

• self : use idtable to convert into Symbol, use object function to convert into Expression

### 3.Errors

5 Error Handling

PA2.pdf

• If there is an error in a class definition but the class is terminated properly and the next class is syntactically correct, the parser should be able to restart at the next class definition.
• Similarly, the parser should recover from errors in features (going on to the next feature), a let binding (going on to the next variable), and an expression inside a {…} block.