A compiler for language L reads a program written in language L and produces a new program in some other lower level language. In our case, an OPL file (called the model) will be translated to HAL. Each model file is divided into 4 sections, namely declarations, instructions, search procedures and display instructions, with the last section being optional. In the declaration section, all the variables used in the model file are declared. Instructions are mainly used in the essence of mathematical programming to solve a certain problem, or to maximize or minimize a certain objective. As for search procedures, these are typically found in constraint programming, where by a preferred search method is provided along side to assist the search of solution. All these different structures will be analysed carefully to ensure the appropriate representation in HAL.
There are 8 phases in a compiler, but not all phases are applicable in this context, since both source language (OPL) and target language (HAL) share some similiarities. Nevertheless, a lexer and a parser are still required in our compiler. Lexer stands for lexical analyser, it reads in input from a file or standard input and use regular expression to break the input into tokens like, identifier, reserved word, arithmetic operators and comments. Regular expressions are the standard way of specifying a certain input pattern, the pattern matching process is performed based on the expression. For example, [0-9][a-zA-Z]* will match any string that has a digit as the first character followed by zero or more occurrences of uppercase or lowercase alphabetic characters. A parser checks all the tokens received from the lexer to make sure it matches the grammar of the language. For instance, below is the grammar for a simple calculator:
expr = expr + term
expr = expr - term
expr = term
term = term * factor
term = term / factor
term = factor
factor = int
The above grammar ensures multiplication and division is performed before addition and subtraction, if both multiplication and division occurs on the same line, it will be performed from left to right. So 3 + 4 * 2 will gives the answer 11, while 3 * 5 / 2 will gives the answer 7 (integer division).