Creating a Language Project¶
This guide will get you started with language development in Spoofax, within an Eclipse environment.
First follow the Installation Guide for instructions on how to download, install, and run Spoofax in Eclipse.
Hello World Language¶
To get you started, let’s do the ‘hello world’ of language development; the hello world language.
In Eclipse, open the new project dialog by choosing Next to open the wizard for creating a Spoofax language specification project.
As project name, choose
helloworld, which will automatically fill in the identifier, name, and extension of the language.
Keep the defaults for the other fields and press Finish to create the project.
Once the project has been created, open and expand it in the package or project explorer view.
The syntax for the language is specified in the
syntax/helloworld.sdf3 SDF3 file.
SDF3 is our syntax definition language, from which we derive a parser, pretty-printer, and syntactic completions from your language.
Currently, the syntax contains a single start symbol
Start, and a production that accepts an empty program:
Start.Empty = <>.
Remove that production and replace it with the following productions:
Start.Program = <<Word> <Word>> Word.Hello = <hello> Word.World = <world>
This grammar accepts a program consisting of 2 words, where the words can be
world, with any number of layout characters (whitespace, tabs, empty lines, comments, etc.) in between.
To observe our changes to the grammar, we must first rebuild the project by selecting. If this is greyed out, make sure that the project is selected in the project explorer.
Create a new file by choosing
Open that file and try out the parser by typing
hello world, any combinations of the 2 words, and with or without layout between words.
If everything went well, the syntax highlighter will highlight the words in purple, which is the default highlighting color for keywords.
To see the abstract syntax tree that the parser derives from your program, select
hello worl, an error message will show up indicating where the error is.
How to proceed?¶
Guides for developing a language with Spoofax:
- Declare Your Language - This book has not been updated for Spoofax 2.0 yet, but most content still applies.
Reference manuals for our meta-languages:
Example language specifications: