This file is a compiler for the Minisculus language (as described below), written in Haskell using Alex, which produces basic stack machine code for a Minisculus program. The compiler works as follows
- Produce a list of tokens, each token associated with an AlexPosn and potentially with a value.
- If there is an error with the syntax, then print out an error message with the AlexPosn of where the error occured and end the program.
- Read the token list using the rules of the grammar.
- If the tokens do not abide by the rules, then print out an error message and end the program.
- Print out the abstract syntax tree for the program.
- Produce the stack machine code and output it to a file named 'machine_code'.
After sourcing the 'bashstack' file, sourcing the 'machine_code' file will execute the Minisculus program.
prog -> stmt.
stmt -> IF expr THEN stmt ELSE stmt
| WHILE expr DO stmt
| INPUT ID
| ID ASSIGN expr
| WRITE expr
| BEGIN stmtlist END.
stmtlist -> stmtlist stmt SEMICOLON
|.
expr -> expr addop term
| term.
addop -> ADD
| SUB.
term -> term mulop factor
| factor.
mulop -> MUL
| DIV.
factor -> LPAR expr RPAR
| ID
| NUM
| SUB NUM.
The tokens stand for the following lexemes:
"if" => IF
"then" => THEN
"while" => WHILE
"do" => DO
"input" => INPUT
"else" => ELSE
"begin" => BEGIN
"end" => END
"write" => WRITE
{alpha}[{digit}{alpha}]\* => ID (identifier)
{digit}+ => NUM (positive integer)
"+" => ADD
":=" => ASSIGN
"-" => SUB
"\*" => MUL
"/" => DIV
"(" => LPAR
")" => RPAR
";"=> SEMICOLON
Assure that Haskell and the Alex module, as well as the chs shell are installed.
To compile and use the program:
- Run alex m_compiler.x
- Run ghc -main-is M_Compiler -o compiler m_compiler.hs
- Run ./compiler <input file> where <input file> is the file with the contents you want to test
- Enter into the chs shell with the command 'csh'
- Run the ./bashstack script to setup the environment for the compiler
- To execute the program from the <input file>, run source machine_code