Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

CFG grammar parser

Name: Anonymous 2008-04-13 18:52

I know there's gonna be some witty regular expression faggot posting here, but whatever.
Let's see your language do this.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

/* CFG grammar */

typedef struct {
    int c;
    const char *str;
} cfg_t;

int eval(const char *buf, const cfg_t *, char *res, size_t sz);

int main(void)
{

    char buf[BUFSIZ];
    char res[BUFSIZ];
    char *p, *s, *tmp;
    int ret;
    size_t n;
    const cfg_t cfg[26] = {
    {'A', "hello"},
    {'B', "world"},
    {'C', "A B"},
    {'D', "A D"}
    };

    while (fgets(buf, sizeof buf, stdin) != NULL) {

    if (*buf && buf[strlen(buf) - 1] == '\n')
        buf[strlen(buf) - 1] = 0;

    p = buf;
    s = res;
    n = 0;

    while ((ret = eval(p, cfg, s, BUFSIZ)) == 1) {
        printf("%lu ==> %s\n", (unsigned long) n, s);
        tmp = p;
        p = s;
        s = tmp;
        n++;
    }

    if (ret == -1)
        printf("'%s': expansion too big to fit in %d buf\n",
           p, BUFSIZ);
    else
        printf("result: '%s'\n", s);

    }

    if (ferror(stdin)) {
    perror("stdin");
    return EXIT_FAILURE;
    }

    return 0;
}

int eval(const char *buf, const cfg_t * cfg, char *res, size_t sz)
{

    size_t i;
    int done = 0;

    while (*buf != 0) {
    if (isupper((unsigned char) *buf)) {
        for (i = 0; i < 26; i++)
        if (*buf == cfg[i].c)
            break;
        if (i != 26) {
        done = 1;
        if (strlen(cfg[i].str) >= sz)
            return -1;
        strcpy(res, cfg[i].str);
        res += strlen(cfg[i].str);
        sz -= strlen(cfg[i].str);
        }
    } else {
        if (sz <= 1)
        return -1;
        *res = *buf;
        res++;
        sz--;
    }
    buf++;
    }
    *res = 0;

    return done;
}


example

$ ./cfg
A
0 ==> hello
result: 'hello'
A B
0 ==> hello world
result: 'hello world'
BB
0 ==> worldworld
result: 'worldworld'
C
0 ==> A B
1 ==> hello world
result: 'hello world'
^D
$

Name: Anonymous 2008-04-14 17:44

>>10
Do you have something against GJS Jay Sussman?

Name: Anonymous 2008-04-14 17:57

>>40
IP protocol.

Internet protocol protocol.

Read >>10 again.

Name: Anonymous 2008-04-14 17:58

>>40
I'm proud to announce that you have been successfully trolled. How do you feel?

Name: 39 2008-04-14 18:08

>>43
I wasn't even trolling, he just completely missed the RAS Syndrome joke I made. Considering I was replying to >>10, who made 3 of those, you can say that this was a severe case of JOKE OVER HEAD.

Which makes him a dumbass, because he's acting like he's an EXPERT NETWORKING PROGRAMMER, yet he didn't realize that IP stands for Internet Protocol.

Name: Anonymous 2008-04-14 18:46

>>44
FUQING BULL
ASK ME ANYTHING ABOUT NETWORQING
OR CAN YOU ONLY MUMBO JUMBO ABOUT 3WAY HANDSHAKE MOTHERFUCKER

Name: Anonymous 2008-04-14 18:48

>>43
kind of bad :(

Name: Anonymous 2008-04-14 19:02

I hate people who whine about RAS syndrome.

For talking to people who might not know what an acronym means, it can be useful to reduce ambiguity in such a way. Language is around to further communication, not to pedantize about stupid details.

>>1-46 are the same person and IHBTC.

Name: Anonymous 2008-04-14 19:03

>>47
I was trolled as well, but in O(log n)

Name: Anonymous 2008-04-14 19:27

>>47
Don't use the acronym if it will cause confusion.

Name: Anonymous 2008-04-14 19:46

>>49
That's unwieldy.

Name: Anonymous 2008-04-14 20:11

DUTAIIWCC confusion

Name: Anonymous 2008-04-15 1:31

>>40
[b][o][i]DO YOU MOTHERFUCKER?[/b][/o][/i]

Name: Anonymous 2008-04-15 3:23

>>52
Use a stack, not a queue.

Name: Anonymous 2008-04-15 9:00

>>53
I lol'd.

Name: Anonymous 2008-04-15 9:10

>>53
:)

Name: Anonymous 2008-04-15 12:09

+<section
+ title="News: new WineTricks, 1.0 status updates and more"
+ subject="Wine"
+ archive="http://www.xkcd.com"
+ posts="0"
+>
How is the url above related to the title?
It isn't, I had nothing else to put.  I generally leave them blank but
I for some reason decided to throw xkcd in there this time.

Name: Anonymous 2008-04-15 17:34

>>52
BBCODE MAH [b][o][i]

Name: Anonymous 2008-04-15 17:45

>>57
Back to /b/, please

Name: Anonymous 2008-04-15 18:28

>>1
Clever, good job.

Name: Anonymous 2008-04-16 5:19

>>50
PROTIP: When you set the context, you can use the acronym form as there shouldn't be any more abiguity

Name: Anonymous 2008-04-17 11:52

gtfo

Name: Anonymous 2008-04-17 16:44

>>61
No.

Name: Anonymous 2008-04-17 17:31

>>61
saw that reply when I mentioned I'm the OP of this thread.
Obviously >>61,62 is the same person.
You fail, so far I've seen every fucking thing you've tried to do.

Name: Anonymous 2008-04-17 18:09

>>63
Actually no, I am >>61 but not >>62.
Obviously, >>63 and >>62 are the same person since >>63 is the OP.
gtfo

Name: Anonymous 2008-04-17 19:43

Obviously, >>63 and >>62 are the same person since >>63 is the OP.
No that doesn't make sense, but since you sage you are an ally.

Name: Anonymous 2008-04-17 20:27

>>65
That makes sense, and since you sage you are an ally.

Name: Anonymous 2011-02-02 22:58

Name: Sgt.Kabukiman 2012-05-22 3:23

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List