这是indexloc提供的服务,不要输入任何密码
Skip to content

Does the order of definitions matter in languages? #144

@IngwiePhoenix

Description

@IngwiePhoenix

Hello!

After having tried and waited for several other parsing libraries to provide a HTML and CSS parser, and having gotten nothing out of it other than some knowledge, I have given up and want to write my own parsers using MPC.

Now, I am far from an expert in parsing and really just getting started - and, out of frustration, no less. ;)

So I wanted to start with something very, very simple: In CSS, seeing .foo > .bar is not uncommon, so I wanted to implement a basic version of that in mpc to learn how to start with the very basics:

#include "mpc.h"

int main() {
  char* input = "a>b";
  mpc_parser_t* ident = mpc_new("ident");
  mpc_parser_t* desc = mpc_new("desc");
  mpc_parser_t* sel = mpc_new("sel");
  mpca_lang(MPCA_LANG_DEFAULT,
    "ident = /[0-9a-zA-Z]+/ ;"
    "desc = '>' ;"
    "sel = <ident> | (<ident> <desc> <ident>)+ ;",
    ident, desc, sel, NULL
  );
  mpc_result_t r;
  if (mpc_parse("input", input, sel, &r)) {
    mpc_ast_print(r.output);
    mpc_ast_delete(r.output);
  } else {
    mpc_err_print(r.error);
    mpc_err_delete(r.error);
  }
}

This, however, tells me:

./foo
input: error: Parser Undefined!

My goal was to parse either "a" or "a>b" for now, and later add subsequent further depths with the > operator, like a>b>c>d.... What is the problem here, exactly? Did I order the rules incorrectly or something?

Thanks and kind regards!

(PS. my idea is to parse basic CSS to extract properties and feed it to LVGL widget properties and use HTML as a "commonly known way" to describe a UI. Think Electron, but much, much more minimal...intended for tiny tools at best.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions