Book Image

Clojure Data Structures and Algorithms Cookbook

By : Rafik Naccache
Book Image

Clojure Data Structures and Algorithms Cookbook

By: Rafik Naccache

Overview of this book

Table of Contents (14 chapters)
Clojure Data Structures and Algorithms Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Building a recursive descent parser using trampoline


A recursive descent parser, as the name implies, makes heavy use of recursion in order to process some input tokens and be able to tell if they comply with a particular grammar code. For each of the grammar rules, such a parser will have to fire a function that proceeds with the verification of the input according to this very rule.

Let's consider, for the purposes of this recipe, a very simple grammar code: one that allows describing the structure of a symbolic expression that is very broadly speaking a set of elements confined between parentheses:

S = sexp
sexp =  "(" element* ")"
element = term | sexp
term = [a-zA-Z0-9]*'

Our parser recognizes as valid structures those which are symbolic expressions, and such expressions contain a list of elements delimited by left and right parentheses. An element is either a term or a symbolic expression itself, and a term is an alphanumeric string.

You may notice the mutual recursion present in this...