Transforming lists from CST to AST

27 views
Skip to first unread message

Oskar

unread,
Nov 14, 2012, 2:37:58 PM11/14/12
to sab...@googlegroups.com

Hi!

I have the following grammar:
Tokens

    event = '{' clojurecode '}';
    osq = '[';
    csq = ']';
    osqneg = '[^';
    bar = '|';
    plus = '+';
    star = '*';
    qmark = '?';
    opar = '(';
    cpar = ')';
    dot = '.';
    whitespace = blank;


Ignored Tokens

    whitespace;


Productions

    union {-> exp} = {concat} concat {-> concat.exp}
        | {union} union bar concat {-> New exp.union(union.exp, concat.exp)};
    concat {-> exp} = {closure} closure {-> closure.exp}
        | {concat} concat closure {-> New exp.concat(concat.exp, closure.exp)};
    closure {-> exp} = {atom} atom {-> atom.exp}
        | {qmark} atom qmark {-> New exp.qmark(atom.exp)}
        | {star} atom star {-> New exp.star(atom.exp)}
        | {plus} atom plus {-> New exp.plus(atom.exp)};
    atom {-> exp} = {dot} dot {-> New exp.dot()}
        | {event} event {-> New exp.event(event)}
        | {paren} opar union cpar {-> New exp.paren(union.exp)}
        | {set} osq event* csq ????????????
        | {negset} osqneg event* csq ????????????



Abstract Syntax Tree

    exp = {union} [left]:exp [right]:exp
        | {concat} [left]:exp [right]:exp 
        | {qmark} exp
        | {star} exp
        | {plus} exp
        | {paren} exp
        | {event} event
        | {set} event* ????????????
        | {negset} event* ????????????
        | {dot};

As you can see, this is a grammar for a kind of regular expressions. The terminals are not letters, though, but "events" that look like this: {key value, key value...}. Now I would like to add "sets" as [abc] in regular regular expressions, that match any of the characeters in the backets. I read here http://www.natpryce.com/articles/000531.html in the last section about how one can make lists of arbitrary length of tokens/symbols. But, in that article he/she does not use the * operator, which I find simpler, but an additional production is used. Can I use the * operator and still get a list of events in my AST?

Also, in that article, they don't really say what kind of AST node the arg_list becomes. I mean, it is a bit unclear as it is not a complete grammar in the example. Now, the important part for me, is that the events, {...}, in a set, is not the same kind of node as events by themselves; I need to be able to tell the difference. Or maybe I can handle the event list in my AST node "set" differently, because I know I'm coming from a "set" node. I've marked the lines that I'm unsure about in the grammar with trailing question marks, "????????????". I hope you can understand what I'm after. How can I achieve what I want? If it's unclear what I mean, let me know. 

Bui Hong Phuc

unread,
Nov 14, 2012, 3:46:25 PM11/14/12
to sab...@googlegroups.com
Hi,
how about the token "clojurecode"? Can you give some example strings, which are valid for the grammar you write?
--
-- You received this message because you are subscribed to the SableCC group. To post to this group, send email to sab...@googlegroups.com. To unsubscribe from this group, send email to sablecc+u...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/sablecc?hl=en
 
 

Oskar

unread,
Nov 14, 2012, 4:16:16 PM11/14/12
to sab...@googlegroups.com
The clojurecode token can be almost anything. But as the longest token is chosen first, what is inside an event matters little. It is not a problem. I have used the grammar successfully, before I tried adding these sets. In summary: the clojurecode token has nothing to do with my current problem.

Etienne Gagnon

unread,
Nov 14, 2012, 4:33:47 PM11/14/12
to sab...@googlegroups.com
Hi Oskar,

Your grammar looks OK to me. Yes, you can get a list in your AST as follows:
    atom {-> exp} = ...
        ...
        | {set} osq event* csq {->new exp.set([event]) }
        | {negset} osqneg event* csq {->new exp.negset([event]) }
The CST->AST syntax to create a list uses brackets: [elem1 elem2 list1] would create a list containing elem1, elem2, and the elements of list1.

I Hope this helps.

Have fun!

Etienne
Etienne Gagnon, Ph.D.
http://sablecc.org
Le 12-11-14 14:37, Oskar a écrit :

Hi!

I have the following grammar:
...
Reply all
Reply to author
Forward
0 new messages