Skip to content

Instantly share code, notes, and snippets.

@wking
Created June 2, 2017 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wking/ac0298774c58d448cb9a37913a9da4c8 to your computer and use it in GitHub Desktop.
Save wking/ac0298774c58d448cb9a37913a9da4c8 to your computer and use it in GitHub Desktop.
ABNF with APG
$ git clone git://github.com/ldthomas/apg-6.3.git
$ cd apg-6.3
$ ./configure
$ make
$ cat <<EOF >>ref.abnf
> ref = component *("/" component)
> component = alphanum *(separator alphanum)
> alphanum = 1*(ALPHA / DIGIT)
> separator = "--" / "-" / "." / "_" / ":" / "@" / "+"
> ALPHA = %x41-5A / %x61-7A
> DIGIT = %x30-39
> EOF
$ ./apg /in:ref.abnf /C:ref
*** C parser generated
ref.h
ref.c
$ cat main.c
$ cat <<EOF >>main.c
> #include <stdio.h>
> #include "Apg.h"
> #include "ref.h"
>
> int main(void) {
> apg_achar s[100];
> fgets(s, 100, stdin);
> void *parser = vpParserCtor(vpParserInit_ref, NULL);
> vTraceCtor(parser);
> apg_uint rc = uiParserSyntaxAnalysis(parser, RULE_REF_REF, s, strnlen(s, 100), NULL);
> vTraceDtor(parser);
> vParserDtor(parser);
> if (rc) {
> printf("success\n");
> } else {
> printf("failure\n");
> }
> return !rc;
> }
> EOF
$ cc -D_APG_CFG_TRACE -o ref main.c ref.c ApgLib/*.c -I ApgLib
$ echo -n foo/bar--baz | ./ref | grep ':M:.*component\|success\|failure'
76: 3: 2:M:--RNM(component):3:foo
210: 81: 2:M:--RNM(component):8:bar--baz
success
$ echo -n ~foo | ./ref | grep ':M:.*component\|success\|failure'
failure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment