4

My languages uses s-expressions with one additional feature - a dot operator for accessing elements from an array or struct.

Currently, my parser works on this code using the access operator -

; Parition a sequence into a pair of sequences.
; NOTE: currently not tail-recursive.
[defRec partition [pred seq]
  (if (isDone seq)
      (pair (list) (list))
      (let (value (peek seq))
           (nextSeq (next seq))
           (nextResult (partition pred nextSeq))
           (nextResultFirst (access :m:first nextResult))
           (nextResultSecond (access :m:second nextResult))
           (if (pred value)
               (pair (cons value nextResultFirst) nextResultSecond)
               (pair nextResultFirst (cons value nextResultSecond)))))]

However, I want to add alternate parsing using a dot operator like so -

; Parition a sequence into a pair of sequences.
; NOTE: currently not tail-recursive.
[defRec partition [pred seq]
  (if (isDone seq)
      (pair (list) (list))
      (let (value (peek seq))
           (nextSeq (next seq))
           (nextResult (partition pred nextSeq))
           (nextResultFirst nextResult.first)
           (nextResultSecond nextResult.second)
           (if (pred value)
               (pair (cons value nextResultFirst) nextResultSecond)
               (pair nextResultFirst (cons value nextResultSecond)))))]

They will both parse out to an equivalent AST. Left recursion matters here because an expression like (f x).y should parse out just like (access :m:y (f x))

However, I don't know how to make FParsec deal with this type of left-recursive parsing, or what alternatives I have to left-recursion.

Bryan Edds
  • 1,696
  • 12
  • 28
  • 1
    You could parse compound expressions like `exp1 . exp2` as a sequence of expressions separated by a dots, for example with the `sepBy` combinator, see http://www.quanttec.com/fparsec/reference/parser-overview.html#parsing-sequences It's easier to answer such questions if you either give a formal grammar definition or provide minimal and concrete example inputs together with the expected AST representations. – Stephan Tolksdorf Jan 01 '12 at 10:51
  • Like the other things I've tried, sepBy sends me into a stack overflow. I'll have to provide more detail once I get time. – Bryan Edds Jan 02 '12 at 15:12

0 Answers0