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))

9
  • 1
    Semantic is the thing that properly produces a language parse tree in Emacs. See Adding support for a new language (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 8, 2015 at 18:12
  • @TuDo Is there a semantic function that yields a full parse tree?
    – Clément
    May 8, 2015 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 of my package srefactor to produce a navigation tree.
    – Tu Do
    May 8, 2015 at 18:30
  • @TuDo Interesting. That always returns nil for me, even after semantic-force-refresh.
    – Clément
    May 8, 2015 at 20:50
  • 1
    Potentially relevant: github.com/cute-jumper/parsec.el (A parser-combinator library)
    – PythonNut
    Jun 18, 2018 at 21:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.