Jim's
Tutorials

Fall 2016
course
navigation

Graphs, graphs, graphs

So I will be honest: I didn't get much work done over Hendricks.
But I did get some interesting stuff done, so I will go over that.
I went made a racket-syntax-object to graphviz coverter: #lang racket #| dotgraph.rkt TO-USE: This script is currently not in an optimal state to be used, but if you must: 1. Generate a datum using the BRAG library (http://docs.racket-lang.org/brag/) 2. Change the second to last expression starting with "resolve-node") 3. copy and paste the resulting output into a graphviz generator (IE http://cs.marlboro.edu//on-campus/graphviz/dot.cgi) TODO: Create an actual interface for this. 10/18/16 | Racket 6.3 | MIT License |# (define dot-graph "") (define node-number 0) (define resolve-node #| Consumes a datum of a syntax object and prints a graphviz spec for the ast. |# (lambda (datnum) (define node-name (number->string node-number)) (define grammar-rule (symbol->string (first datnum))) (define raw-content (rest datnum)) (define named-content '()) (set! node-number (+ node-number 1)) (for ([item raw-content]) (cond [(list? item) (and (set! named-content (append named-content (list (list (number->string node-number) item)))) (resolve-node item))] [(string? item) (set! named-content (append named-content (list (list (number->string node-number) item))))] [else (set! named-content (append named-content (list (list (number->string node-number) (symbol->string item)))))]) (set! node-number (+ node-number 1))) (for ([item named-content]) (set! dot-graph (string-append dot-graph node-name " -> " (first item) ";\n" (first item)"[label=\"" (~a (second item)) "\"]" ";\n"))))) (resolve-node '(program (lang-name "#lang" "racket") (s-exp "(" "+" "6" (s-exp "(" "-" "7" "8" ")") ")"))) ;; edit this thing (printf "~a" dot-graph)
The resulting spec was fed into this page: http://cs.marlboro.edu//on-campus/graphviz/dot.cgi
I used the same simple test program with two different grammars to see the difference in parsing. Here is the sample program: #lang racket (+ 6 (- 7 8))
This is the simpler of the two grammars I used:
#lang brag program: lang-name s-exp* s-exp: LPAREN (KEYWORD | ID | s-exp| QUOTE s-exp | LIT | SYM | COMMENT)* RPAREN lang-name: META-WORD ID
Which produces this graph:
The other grammar was this:
#lang brag program: lang-name (s-exp| comment)* s-exp: (list | quoted-list)+ quoted-list: QUOTE list list: LPAREN (list | quoted-list | single-value)* RPAREN single-value: (data | symbol) data: (LIT | unresolved-name) unresolved-name: (KEYWORD | ID) symbol: SYM comment: COMMENT lang-name: META-WORD ID
Which made this graph:

This is the product of a full racket program (the prime number finder that I used as an example last week).
I also wrote some other grammars that I need to test.
http://cs.marlboro.edu/ courses/ fall2016/jims_tutorials/ ldavis/ Oct_19
last modified Wednesday October 19 2016 12:23 am EDT

attachments [paper clip]

     name last modified size
[IMG]complicatedgrammar.png Oct 19 2016 12:06 am 158kB    dot.bin Oct 19 2016 12:06 am 1.12MB [IMG]simplegrammar.png Oct 19 2016 12:06 am 51.8kB