A parser for Epsilon
To save you some time, we provide you with a parser for Epsilon. The parser takes Epsilon code as input and produces a parse tree.
Getting started
The newest version is always available on CSIL in the directory /cs/class/cs162/parser under the name EpsilonParser.hs. If you work on CSIL, we suggest that you not copy the file to your home directory, but leave it where it is so you will always use the newest version. You can tell hugs to look in /cs/class/cs162/parser with the -P option.
The following example creates a small program that uses the parser, then runs it:
csil:~$ mkdir foo csil:~$ cd foo csil:~/foo$ cat > Main.hs module Main where import EpsilonParser main = getContents >>= print . parser . lexer 1 csil:~/foo$ cat > foo.epsilon var a; begin a := 3 end. csil:~/foo$ runhugs -P/cs/class/cs162/parser: Main < foo.epsilon Program [VarDecl [NonArrayIdentDecl "a"]] (CompoundStatement [Assignment (NonArrayIdentExpr "a") (Integer 3)]) csil:~/foo$
The type of the parse tree
The parser returns a parse tree of type ParseTree. This data type is similar to the one defined in a previous homework, but extended to describe the complete Epsilon language.
data ParseTree
= Program [Declaration] ParseTree
| VarDecl [IdentDecl]
| Proc Head Body
| ArrayIdentDecl String Int
| NonArrayIdentDecl String
| CompoundStatement [Statement]
| Assignment Expression Expression
| ProcedureCall String [Expression]
| If Condition Statement
| IfElse Condition Statement Statement
| Write Expression
| Read Expression
| Return Expression
| While Condition Statement
| Odd Expression
| Equal Expression Expression
| NotEqual Expression Expression
| LessThan Expression Expression
| GreaterThan Expression Expression
| LessEqual Expression Expression
| GreaterEqual Expression Expression
| Integer Int
| NonArrayIdentExpr String
| ArrayIdentExpr String Expression
| Addition Expression Expression
| Subtraction Expression Expression
| Multiplication Expression Expression
| Division Expression Expression
| Negation Expression
| FunctionCall String [Expression]
| Body Declaration Statement
| ProcedureHead String [IdentDecl]
| FunctionHead String [IdentDecl]
| Identifier String
deriving Show
type Declaration = ParseTree
type IdentDecl = ParseTree
type Statement = ParseTree
type Condition = ParseTree
type Expression = ParseTree
type Body = ParseTree
type Head = ParseTree
Downloading the parser
If you want to work at home (or somewhere else that is not CSIL), you can download the parser here: EpsilonParser.hs (current version: -- $Id: EpsilonParser.y,v 1.3 2007/02/21 00:52:10 cs162 Exp $ )
Modifying the parser
Although it shouldn't be necessary, you are welcome to modify the parser in any way you'd like. To modify it, you will need to install the parser generator Happy and download the grammar EpsilonParser.y (current version: -- $Id: EpsilonParser.y,v 1.3 2007/02/21 00:52:10 cs162 Exp $ ).