Semantic ErrorsΒΆ

The SemErrors module allows the compiler to record errors, along with related information (such as the line where they happened).

In this way, errors do not need to be emitted when they are found (which may lead to confusing order in the errors), but they can be printed ordered according to the line where they occur, once the analysis is completed.

Also, the class takes care of generating the error with the appropriate text, which would ease e.g. translation of error messages to languages other than English.

 1////////////////////////////////////////////////////////////////
 2// Class SemErrors: this class contains methods that emit
 3// semantic error messages with their localization.
 4// It is used by the semantic visitors:
 5//   - SymbolsVisitor
 6//   - TypeCheckVisitor
 7// Semantic errors emitted are kept in a vector and when the
 8// typecheck finishes they will be printed (sorted by line/column number)
 9
10class SemErrors {
11
12public:
13
14  // Constructor
15  SemErrors() = default;
16
17  // Write the semantic errors ordered by line number
18  void print ();
19
20  // Accessor to get the number of semantic errors
21  std::size_t getNumberOfSemanticErrors () const;
22
23  // Methods that store the error messages
24  //   node is the terminal node correspondig to the token IDENT in a declaration
25  void declaredIdent                (antlr4::tree::TerminalNode *node);
26  //   node is the terminal node correspondig to the token IDENT in an expression
27  void undeclaredIdent              (antlr4::tree::TerminalNode *node);
28  //   node is the terminal node correspondig to the token ASSIG
29  void incompatibleAssignment       (antlr4::tree::TerminalNode *node);
30  //   ctx is the node corresponding to the left expression
31  void nonReferenceableLeftExpr     (antlr4::ParserRuleContext *ctx);
32  //   node is the labeled operator token (referenced by op in the grammar)
33  void incompatibleOperator         (antlr4::Token *node);
34  //   ctx is the node corresponding to an array access
35  void nonArrayInArrayAccess        (antlr4::ParserRuleContext *ctx);
36  //   ctx is the node corresponding to the index expression in an array access
37  void nonIntegerIndexInArrayAccess (antlr4::ParserRuleContext *ctx);
38  //   ctx is the node corresponding to the expression
39  void booleanRequired              (antlr4::ParserRuleContext *ctx);
40  //   ctx is the node corresponding to the function identifier 
41  void isNotCallable                (antlr4::ParserRuleContext *ctx);
42  //   ctx is the node corresponding to the function identifier
43  //   This error will not be emitted (productive functions can be called as procedures)
44  void isNotProcedure               (antlr4::ParserRuleContext *ctx);
45  //   ctx is the node corresponding to the identifier
46  void isNotFunction                (antlr4::ParserRuleContext *ctx);
47  //   ctx is the node corresponding to the function identifier
48  void numberOfParameters           (antlr4::ParserRuleContext *ctx);
49  //   pCtx is actual parameter node
50  //   n is the number of argument starting from 1
51  //   cCtc is the call node
52  void incompatibleParameter        (antlr4::ParserRuleContext *pCtx,
53				     unsigned int n,
54				     antlr4::ParserRuleContext *cCtx);
55  //   pCtx is actual parameter node
56  //   n is the number of argument starting from 1
57  //   cCtc is the call node
58  void referenceableParameter       (antlr4::ParserRuleContext *pCtx,
59				     unsigned int n,
60				     antlr4::ParserRuleContext *cCtx);
61  //   node is the terminal node correspondig to the token RETURN
62  void incompatibleReturn           (antlr4::tree::TerminalNode *node);
63  //   ctx is the read or write instruction
64  void readWriteRequireBasic        (antlr4::ParserRuleContext *ctx);
65  //   ctx is the instruction that needs a referenceable expression
66  void nonReferenceableExpression   (antlr4::ParserRuleContext *ctx);
67  //   ctx is the program node (grammar start symbol) 
68  void noMainProperlyDeclared       (antlr4::ParserRuleContext *ctx);
69
70};  // class SemErrors