Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Writing your first Domain Specific Language, Part 2 of 2

, 3 Sep 2008
A guide to writing a compiler in .NET for beginners, using Irony.
FreightLanguageCompiler.zip
FreightLanguageCompiler
bin
Debug
FreightLanguageCompiler.dll
Irony.dll
Nodes
Properties
ExampleSite
App_Data
Bin
FreightLanguageCompiler.dll
Irony.dll
FLSnippets
IronySnippets.snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Irony.Compiler;
using FreightLanguageCompiler.Nodes;

namespace FreightLanguageCompiler
{
	public class FLGrammar : Grammar
	{

		public FLGrammar()
		{

			#region Initial setup of the grammar

			// turn off case sensitivity
			this.CaseSensitive = false;

			// define all the non-terminals
			var program = new NonTerminal("program", typeof(ProgramNode));
			var statementList = new NonTerminal("statementList", typeof(StatementListNode));
			var freightDeclaration = new NonTerminal("freightDeclaration", typeof(FreightDeclarationNode));
			var statement = new NonTerminal("statement", typeof(StatementNode));
			var setVariable = new NonTerminal("setVariable", typeof(SetVariableNode));
			var ifStatement = new NonTerminal("ifStatement", typeof(IfStatementNode));
			var orderLoop = new NonTerminal("orderLoop", typeof(OrderLoopNode));
			var expression = new NonTerminal("expression", typeof(ExpressionNode));
			var binaryOperator = new NonTerminal("binaryOperator", typeof(BinaryOperatorNode));

			// define all the terminals
			var variable = new IdentifierTerminal("variable");
			variable.AddKeywords("set", "to", "if", "freight", "cost", "is", "loop", "through", "order");
			var number = new NumberLiteral("number");
			var stringLiteral = new StringLiteral("string", "\"", ScanFlags.None);

			// remove uninteresting nodes from the AST (note: in current version of Irony,
			// keywords added to the variable cannot be registered as punctuation).
			this.RegisterPunctuation(";", "[", "]", "(", ")");

			// specify the non-terminal which is the root of the AST
			this.Root = program;

			#endregion

			#region Define the grammar

			//<Program> ::= <StatementList> <FreightDeclaration>
			program.Rule = statementList + freightDeclaration;

			//<StatementList> ::= <Statement>*
			statementList.Rule = MakeStarRule(statementList, null, statement);

			//<Statement> ::= <SetVariable> ";" | <IfStatement> | <OrderLoop> | <Expression> ";"
			statement.Rule = setVariable + ";" | ifStatement | orderLoop | expression + ";";

			//<SetVariable> ::= "set" <Variable> "to" <Expression>
			setVariable.Rule = Symbol("set") + variable + "to" + expression;

			//<IfStatement> ::= "if" <Expression> "[" <StatementList> "]"
			ifStatement.Rule = Symbol("if") + expression + "[" + statementList + "]";

			//<OrderLoop> ::= "loop" "through" "order" "[" <StatementList> "]"
			orderLoop.Rule = Symbol("loop") + "through" + "order" + "[" + statementList + "]";

			// <FreightDeclaration> ::= "freight" "cost" "is" <Expression> ";"
			freightDeclaration.Rule = Symbol("freight") + "cost" + "is" + expression + ";";

			//<Expression> ::= <number> | <variable> | <string>
			//  | <Expression> <BinaryOperator> <Expression>
			//  | "(" <Expression> ")"
			expression.Rule = number | variable | stringLiteral
				| expression + binaryOperator + expression
				| "(" + expression + ")";

			//<BinaryOperator> ::= "+" | "-" | "*" | "/" | "<" | ">" | "<=" | ">=" | "is"
			binaryOperator.Rule = Symbol("+") | "-" | "*" | "/" | "<" | ">" | "<=" | ">=" | "is";

			#endregion

		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Daniel Flower
Software Developer
China China
Daniel has a Bachelor of Science with First Class Honours from the University of Auckland, and has designed and developed software in companies large and small.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 3 Sep 2008
Article Copyright 2008 by Daniel Flower
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid