Code Monkey home page Code Monkey logo

Comments (7)

petrolifero avatar petrolifero commented on August 25, 2024
#lang peg

braceLanguage <- item+;

item <- braceExpansion / simpleItem;
braceExpansion <- '{' braceLanguage (',' braceLanguage)* ','? '}';

simpleItem <- [^{,}]+;

this works on that example

from racket-peg.

petrolifero avatar petrolifero commented on August 25, 2024
#lang peg

(struct seq (a b) #:transparent);
(struct brace (l) #:transparent);
(struct idt (a) #:transparent);

braceLanguage <- v:item+ -> (let loop ((l v))
                              (if (null? (cdr l))
                                  (car l)
                                  (loop (cons (seq (car l) (cadr l)) (cddr l)))));

item <- braceExpansion / simpleItem;
braceExpansion <- ~'{' l:(braceLanguage (~',' braceLanguage)* ~(','?)) '}' -> l;

simpleItem <- v:[^{,}]+ -> (idt v);

with semantic actions

from racket-peg.

petrolifero avatar petrolifero commented on August 25, 2024

With semantic actions, we really need to use this (a . (b . c)) thing? Is not simpler to create structs?

from racket-peg.

petrolifero avatar petrolifero commented on August 25, 2024
#lang peg

(struct seq (a b) #:transparent);
(struct brace (l) #:transparent);
(struct idt (a) #:transparent);

braceLanguage <- v:item+ -> (let loop ((l v))
                              (if (null? (cdr l))
                                  (car l)
                                  (loop (cons (seq (car l) (cadr l)) (cddr l)))));

item <- braceExpansion / simpleItem;
braceExpansion <- ~'{' l:(braceLanguage (~',' braceLanguage)* ~(','?)) '}' -> l;

simpleItem <- v:[^{,}]+ -> (idt v);

with semantic actions

To me, the answer from this grammar is not confusing. (list a b) represent {a,b},
(seq a b) represent ab where a and b are not both simpleItems

from racket-peg.

petrolifero avatar petrolifero commented on August 25, 2024

Whats some precise metrics of what you want?

from racket-peg.

rain-1 avatar rain-1 commented on August 25, 2024

I'm happy with this solution:

#lang racket

;; list-monad.rkt

(provide liftm2 sequence
         concatenate string-concatenate)

(define (concatenate lists)
  (apply append lists))

(define (string-concatenate strings)
  (apply string-append strings))

(define (>>= m f)
  (apply append (map f m)))

(define (liftm2 f a b)
  (>>= a (lambda (x) (>>= b (lambda (y) (list (f x y)))))))

(define (sequence lsts)
  (if (null? lsts)
      (list '())
      (liftm2 cons (car lsts) (sequence (cdr lsts)))))
#lang peg

(require "list-monad.rkt");

text <- str:[^{},]+ -> (list str);
items <- xs:(text / choice)+ -> (map string-concatenate (sequence xs));
choice <- '{' x:items xs:(~',' items)* '}' -> (concatenate (cons x xs));

from racket-peg.

petrolifero avatar petrolifero commented on August 25, 2024

Cool. Using monads

from racket-peg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.