Home CPSC 401

Sloth Parser


Due: February 19



This is the second phase of the Sloth interpreter assignment. In this phase, you will be writing a grammar for Sloth and using ANTLR to convert it into a parser for Sloth input files.


Test Programs

You can use the same set of input files to test your parser:

hello.slPrints hello world
simple.slPrints 1 + 1.
area.slInputs the radius of a circle and computes its area.
fact.slInputs a number and computes the factorial of that number.
fibs.slInputs a number N and displays the first N Fibonacci numbers.
minmax.slInputs a number N, then inputs N numbers, then displays the ones with the least and greatest values.
guess.slA guess the number game
table.slPrints a 2D multiplication table
acid.slA program that tests out most aspects of the language, and simply outputs 42.


Syntax Analysis

The syntactic details of Sloth are described here, and will help you write the parser.

Programs in Sloth consist of zero or more statements.

Each statement can be:


Expressions are the other major part of Sloth syntax. An expression consists of literal values, identifiers and input expressions combined with parenthesis and the following operators (in order of precedence):

The "input expressions" mentioned above are one of the following:

All operators are binary except for ! which is unary. The operators each have the same meaning as they do in C-based languages.

Your program will parse Sloth programs using the ANTLR generated grammar. If an input program does not fit the grammar you specify, Bison will produce an error.


Building the Parser

Begin by making two small changes to your "SlothLexer.g4" file from part 1:

  1. Change the line which says grammar SlothLexer to say lexer grammar SlothLexer instead.
  2. Remove the line which reads program:;.

These changes will make your lexer work with the parser we will write instead of be a stand-alone grammar.

Now create the file "SlothParser.g4" and begin it with the line parser grammar SlothParser. Include this line so that it pulls its toekns from our lexer:

options {
    tokenVocab = SlothLexer;

The rest of this file should contain the syntax rules for Sloth programs. Call your top-level syntax rule program.

You can use the following build and clean scripts to build and clean (remove generated files) the parser and lexer. These rely on having completed the ANTLR setup instructions.



We can again use the grun test tool in order to test our grammar. The following command should run the fact.sl Sloth program through the parser:

grun Sloth program -tree < fact.sl

This should produce something like the following output, showing the structure of our code:

(program (statement print ( (expression (term "Please enter a number: ")) ) ;) (statement x := (expression (term readInt ( ))) ;) (statement counter := (expression (term x)) ;) (statement fact := (expression (term 1)) ;) (statement while (expression (expression (term counter)) > (expression (term 1))) do (statement begin (statement fact := (expression (expression (term fact)) * (expression (term counter))) ;) (statement counter := (expression (expression (term counter)) - (expression (term 1))) ;) end)) (statement println ( (expression (expression (expression (term x)) + (expression (term "! = "))) + (expression (term fact))) ) ;))

We can also use the following to generate a graphical version of the tree:

grun Sloth program -ps tree.ps < fact.sl

This creates a Postscript file called "tree.ps" with the graphical representation. This looks like this:

You may need to install a program that can read Postscript files to view them.



Submit your SlothParser.g4 file to Canvas for this assignment.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.