Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple programming language in EBNF form

I'm working on a grammar for a very simple programming language and I need to write it in (E)BNF form. I've seen many examples of simple languages where for example only operations are allowed on numbers/identifiers or examples of a larger complexity such as The Syntax of C in Backus-Naur Form.

What I need (and yes I've searched with Googled quite a bit) is a simpler example that I can take a look at so that I can understand the general hierarchy. Something with variable declarations, function declarations, loops, assignments, operations etc but on a much smaller and simpler scale than all of C written in BNF.

I'm not asking for someone to write one up here since that would be a large job, but if someone could point me towards a resource then I would be very grateful.

I understand the rules for writing a language in BNF; I just don't feel like I have a grasp over where to start and how to order the language.


1 Answers

(It seems you may have found what you're looking for, but you - as well as future readers - may find the following useful.)

The book Basics of Compiler Design by Torben Ægidius Mogensen from the University of Copenhagen contains a very simple language to be used to implement a compiler and an interpreter against. It's BNF is the following:

Program --> Funs

Funs --> Fun
Funs --> Fun Funs

Fun --> TypeId ( TypeIds ) = Exp

TypeId --> *int* **id**
TypeId --> *bool* **id**

TypeIds --> TypeId
TypeIds --> TypeId , TypeIds

Exp --> num
Exp --> id
Exp --> Exp + Exp
Exp --> Exp = Exp
Exp --> if Exp then Exp else Exp
Exp --> id ( Exps ) 
Exp --> let id = Exp in Exp

Exps --> Exp 
Exps --> Exp , Exps

It's a very small and very easy language to write an interpreter against - you can't go smaller and more pragmatic regarding what you want to do. I recommend both the book, and the exercises it contains.

like image 102
NlightNFotis Avatar answered Oct 26 '25 20:10

NlightNFotis