0

I'm trying to write a grammar so I can parse a specific type of input file. I've started with the most basic grammar possible, but guile just about melts my computer when trying to match a pattern with this grammar.

I'm wondering if there is something that is ambiguous about my grammar? Here is a sample input I'm trying to parse:

input.txt

[HELLO]
  var = 123
[]

And here is my current script:

(use-modules (ice-9 peg))
(use-modules (ice-9 textual-ports))

(define *input*
  (call-with-input-file
      "test.txt" get-string-all))

(define-peg-string-patterns
  "block <-- block_header param block_closer
block_header <-- LB text RB SP
block_closer <-- LB RB SP
param <-- text SP EQ param_v SP
param_v <-- NUM
text <-- [a-zA-Z]+
NUM <-- [0-9]+
EQ < '='
LB < '['
RB < ']'
SP < [ \t\n]*")

(peg:tree (match-pattern block *input*))

The jist is that a file is made up of blocks that contain variables. (The next step is adding nested blocks to this grammar.)

Is there something particularly wrong with this grammar?

dylanjm
  • 2,011
  • 9
  • 21
  • I'm not familiar with scheme's PEG library, but that grammar looks harmless enough to me. Are you giving it the right file? Your question text says input.txt, but the code says test.txt. – amalloy Apr 01 '20 at 02:39

2 Answers2

0

I don't know about why it's locking up on you, but you are missing an SP for that input to parse correctly.

This:

param <-- text SP EQ param_v SP

should be:

param <-- text SP EQ SP param_v SP
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43
0

I found the reason why it wasn't working. Apparently the PEG library for guile doesn't take rules with underscores in the name. After changing all the rules like block_header to bheader it worked.

dylanjm
  • 2,011
  • 9
  • 21