Clojure Instaparse Parser Tutorial: Change Start Rule

By Xah Lee. Date:

The functions {parser, parse, parses} have a optional parameter :start :rule left-hand-side name. This lets you change the start rule. (by default, the start rule is the first rule.)

;; example of changing the start rule

(ns example.core (:require [instaparse.core :as insta]))

(def p8
       AB=A B

(insta/parse p8 "aabb")
;; returns
;; [:S [:AB [:A "a" "a"] [:B "b" "b"]]]

(insta/parse p8 "aabb" :start :A)
;; returns failure object. prints:
;; Parse error at line 1, column 3:
;; aabb
;;   ^
;; Expected:
;; "a"

(insta/parse p8 "aa" :start :A)
;; returns
;; [:A "a" "a"]

back to Clojure Instaparse Parser Tutorial

If you have a question, put $5 at patreon and message me.

Clojure Instaparse