.. index:: Auxiliary modules Auxilary Compiler Modules ========================= We will describe the modules you'll need to use following a skeleton code for your compiler The main program of your compiler will look like the following code. You are supposed to read the comments and understand each step. .. literalinclude:: ../../practica/asl/main.cpp :language: c++ :linenos: :lines: 56- Code generated by ANTLR4 ------------------------ ANTLR4 is a parser generator that will create many of the code needed to build the compiler. Given a grammar, (file *Asl.g4*) ANTLR4 will generate classes ``AslLexer`` and a ``AslParser`` that can be directly called from our main to obtain the parse tree of the target program. Once we have the parse tree, we need to traverse it to perform type checking and code generation. ANTLR4 will also generate one abstract class called ``AslVisitor`` that has one ``visit`` method for each rule (or rule label) in the grammar. The *derived* class ``AslBaseVisitor`` implements these methods that walk the entire tree. The ``SymbolVisitor``, ``TypeCheckVisitor``, and ``CodeGenVisitor`` are classes derived from ``ASLBaseVisitor``. In each of them, the ``visit`` method for nodes that we want to process have been written, thus overwritting the empty methods in the base class. Each visitor will deal with some nodes and ignore some others, since each will do different things. For instance: - ``SymbolVisitor`` will only declare ``visit`` methods for nodes related to variable, parameter, or function declarations, and ignore all the rest. - ``TypeCheckVisitor`` will declare ``visit`` methods for nodes related to expressions, assignments, and parameter passing, but ignore nodes about variable or function declarations. - ``CodeGenVisitor`` will declare ``visit`` methods for nodes related to instructions and expressions, but ignore others. Thus, to build a compiler with ANTLR4, we only need to write the grammar, a short main program like the previous example, and the needed ``visit`` methods of one (or more) derived ``Visitor`` classes that deal with the tree nodes we want to process. Auxiliary Modules ----------------- The classes ``TypesMgr``, ``SymTable``, ``TreeDecoration``, ``SemErrors``, and the *Code Manager* module, containing class ``code`` and other related subclasses are used by our ASL compiler to store data about the program being compiled, and to propagate this information from one traversal to the following (e.g. Symbol declaration traversal will store information about which variables are declared and which type each of them has. Type checking traversal will use the type information to verify that operations are correctly performed, and code generation traversal will use information about the names and sizes of the variables to produce the right low-level code). Class ``code`` will be useful to contain the partially generated code, extend it with new instructions, and print it when it is complete. Class ``counters`` provides counters to keep track of used labels and temporals. Type Manager ^^^^^^^^^^^^ The Type Manager stores which data types have been seen in the target program (e.g. *bool*, *array of 10 char*, *function receiving one int and returning bool*) and offers some methods to manipulate them. More information about :doc:`Type Manager `. Symbol Table ^^^^^^^^^^^^ The Symbol Table stores which identifiers have been seen in the target program (variable names, parameter names, function names), in which scope have they appeared (i.e. in which function or code block), and associates them with a type (stored in Type Manager) More information about :doc:`Symbol Table `. Tree Decoration ^^^^^^^^^^^^^^^ The Tree Decoration module allows to store some information associated to specific nodes in the parse tree. More information about :doc:`Tree Decoration `. Code Manager ^^^^^^^^^^^^ The Code Manager module contains several classes that ease the handling and combination of code fragments. More information about :doc:`Code Manager `. Semantic Errors ^^^^^^^^^^^^^^^ Semantic Errors module simplifies the handling of errors, associating errors to nodes in the tree. More information about :doc:`Semantic Errors `.