Questions tagged [peg]

A “Parsing Expression Grammar” (“PEG”) is a formal language to describe formal languages.

A Parsing Expression Grammar (“PEG”) is a formal language that defines a set of rules to describe a certain class of formal languages similar to context-free grammars.

In comparison to context-free grammars, PEGs have the advantage of always being unambiguous, and of mapping naturally to recursive descent parsers, which makes them easy to implement.

288 questions
5
votes
0 answers

Parsing significant whitespace with a PEG in Python (specificly Parsley)

I'm creating a syntax that supports significant whitespace (most like the "Z" lisp variant than Python or yaml, but same idea) I came across this article on how to do significant whitespace parsing in a pegasus a PEG parser for C# But I've been less…
Mark Harviston
  • 660
  • 4
  • 18
5
votes
3 answers

Eliminate Left Recursion on this PEG.js grammar

(Note: I've read other questions like this, but I haven't been able to figure this out). I wrote this grammar: start = call ident = [a-z]+ spaces = [ ]+ call = f:ident spaces g:(call / ident) { return f + "(" + g + ")"; } With this input a b…
user1527166
  • 3,229
  • 5
  • 26
  • 26
5
votes
1 answer

Trouble with PEG.js end of input

I am trying to write a simple grammer for PEG.js that would match something like this: some text; arbitrary other text that can also have µnicode; different expression; let's escape the \; semicolon, and \not recognized escapes are not a…
RushPL
  • 4,732
  • 1
  • 34
  • 44
4
votes
1 answer

Left factorisation in Parsing Expression Grammar

I’m trying to write a grammar for a language which allows the following expressions: Function calls of the form f args (note: no parentheses!) Addition (and more complex expressions but that’s not the point here) of the form a + b For example: f…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
4
votes
1 answer

simplest rules in treetop not working

I have a treetop grammar with only two rules: grammar RCFAE rule num [0-9]+ end rule identifier [a-zA-Z] [a-zA-Z]* end end I'm trying to parse simple strings ("A" and "5"). The "5" is recognized as a…
timichanga
  • 53
  • 3
4
votes
1 answer

Using Pest.rs how can I manage a multi-line syntax where a line ends in "\"?

A common idiom for bash is is to use \ to escape the newline at the end of the line, If a \ pair appears, and the backslash is not itself quoted, the \ is treated as a line continuation (that is, it is removed from the input…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
4
votes
1 answer

Ambiguity of PEG grammar with PEST parser

I'm trying to write a PEG for an old file format that has about 100 keywords which can't be used as identifiers. Here's an example of a keyword rule: IN = { ^"in" } // Caret means case insensitivity keyword = { IN } // plus others The identifier…
oorst
  • 909
  • 2
  • 7
  • 29
4
votes
2 answers

Implementation differences between parser combinators and packrat algorithm

In order to have a better understanding of packrat I've tried to have a look at the provided implementation coming with the paper (I'm focusing on the bind): instance Derivs d => Monad (Parser d) where -- Sequencing combinator (Parser p1) >>=…
GlinesMome
  • 1,549
  • 1
  • 22
  • 35
4
votes
2 answers

What is the best way to define grammars for a text editor?

I'm masochistically writing an open-source text editor for Mac and have finally reached the point at which I want to add syntax highlighting. I've been going back and forth on various solutions for the past few days, and I've finally decided to open…
Lytol
  • 970
  • 6
  • 15
4
votes
2 answers

Using PEG.js for simple search/replace

Im trying to understand how to use PEG.js for simple search/replace in a text. Surely this is not the intended use for a parser but anyway Im curious about the logic behind these kind of languages to produce some search/replace. The problem that Im…
Masacroso
  • 249
  • 2
  • 12
4
votes
1 answer

What is 'memoize' in PEG parsers (e.g. Pegasus) and when should it be used?

Here is an example from Pegasus: additive -memoize = left:additive "+" right:multiplicative { left + right } / left:additive "-" right:multiplicative { left - right } / multiplicative What is memoize in this context and when should I use…
Andrey Shchekin
  • 21,101
  • 19
  • 94
  • 162
4
votes
3 answers

How to make optional word in PEG.js

I'm trying to build a simple parser with PEG.js. I want the user to be able to input a series of keywords, with an optional "AND" between them, but I can't seem to get the optional and working. It always expects it, even though I've marked it with…
T Nguyen
  • 3,309
  • 2
  • 31
  • 48
4
votes
2 answers

lpeg parse first-order logic term

As the title says, I'm trying to parse for example term(A, b, c(d, "e", 7)) in a Lua table like {term, {A, b, {c, {d, "e", 7}}}} This is the grammar I built: local pattern = re.compile[=[ term <- variable / function argument <- variable…
キキジキ
  • 1,443
  • 1
  • 25
  • 44
4
votes
1 answer

Parsing trouble with amotoen

I'm trying to write a grammar to parse a simple language to describe drum loops, using Clojure and amotoen. The language looks like this:– # Test Loop # this is a comment BPM: 100 Samples: BD: bd.wav SD: sd.wav CHH: chh.wav CSH: csh.wav Body: …
loomcore
  • 43
  • 1
  • 4
4
votes
1 answer

Parsing a reserved word in Parsing Expressive Grammar (PEG.js)

The similar question and the author's website give me solutions like this: Identifier "identifier" = !ReservedWord [A-Za-z_]+ ReservedWord = "test" / "abc" This solution can't parse an identifier like this "test_var". In this example, the…
PG_
  • 129
  • 2
  • 5