Clojure Instaparse Parser Tutorial: Change Start Rule

By Xah Lee. Date:

The functions {parser, parse, parses} have a optional parameter :start :rule_LHS_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
     (insta/parser
      "S=AB*
       AB=A B
       A='a'+
       B='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