7

How can I construct a parse tree from a string and a simple grammar? Semantic seems very tricky, and SMIE does not seem to produce a parse tree.

Here's an example, assuming a language that looks like this:

BLAH [ FOO "aaa"; "bbb" | BAR [ "ccc" | "ddd" ] ]

How can I simply transform that string into the following sexp, or something close?

(seq (ident "BLAH")
     (choice (cons (seq (ident "FOO") (str "aaa"))
                   (seq (str "bbb")))
             (seq  (ident "BAR")
                   (choice (seq (str "ccc")) (seq (str "ddd"))))))

(essentially there are sequences seq (no delimiters), grouping choices choice ([]), strings "", identifiers ident (capitals), and delimited sequences cons (;-delimited))

Scott Weldon
  • 2,695
  • 1
  • 17
  • 31
Clément
  • 3,924
  • 1
  • 22
  • 37
  • 1
    Semantic is the thing that properly produces a language parse tree in Emacs. See [Adding support for a new language](http://cedet.sourceforge.net/addlang.shtml) (a bit outdated, but a good starting point if you put a bit of effort). Essentially, all you need is supplied a language grammar file. Or, you can reinvent the lexing/parsing process and manually produce the parse tree. – Tu Do May 08 '15 at 18:12
  • @TuDo Is there a semantic function that yields a full parse tree? – Clément May 08 '15 at 18:21
  • Yes, assume that the current major mode is already supported by Semantic (which means a grammar file was written and compiled for it), then you use `(semantic-fetch-tags)` to get the tree. Try it in C/C++ mode or Emacs Lisp mode. Here is an example of using manipulating the tags in the function [srefactor--fetch-candidates-helper](https://github.com/tuhdo/semantic-refactor/blob/master/srefactor.el#L1332) of my package `srefactor` to produce [a navigation tree](http://i.imgur.com/ewJWwt9.gifv). – Tu Do May 08 '15 at 18:30
  • @TuDo Interesting. That always returns `nil` for me, even after `semantic-force-refresh`. – Clément May 08 '15 at 20:50
  • You must run `(semantic-default-elisp-setup)` and then enable Semantic with `(semantic-mode 1)` before fetching tags. – Tu Do May 08 '15 at 20:55
  • If you run `semantic-fetch-tags` in C/C++, you only need to enable `semantic-mode`. – Tu Do May 08 '15 at 21:08
  • [Here](https://github.com/politza/pdf-tools/blob/master/lisp/tablist-filter.el) I've used wisent (semantic's parser) to parse a simple grammar. – politza May 09 '15 at 04:54
  • Published some library which might be helpful https://github.com/andreas-roehler/werkstatt/blob/master/misc/transform.el Please open issues there maybe. – Andreas Röhler May 12 '15 at 20:15
  • 1
    Potentially relevant: https://github.com/cute-jumper/parsec.el (A parser-combinator library) – PythonNut Jun 18 '18 at 21:14

0 Answers0