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
2
votes
1 answer

pyPEG - data identified by a `flag()` function are returned incorrectly by `compose()` function

I'm in a situation where I need to parse a legacy format. What I want to do is to write a parser that recognizes the format and transform it to an object which is easier to work with. I managed to parse the input, the problem is when I want to…
Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
2
votes
0 answers

In a PEG, process the input twice

Background I'm using PEG.js online. I have a PEG that takes a function (only math assignments and expressions are valid) and returns a literal math expression equivalent, like 3+2*5. Example input (function): main() { a = 1 b = a return…
JoseHdez_2
  • 4,040
  • 6
  • 27
  • 44
2
votes
1 answer

How to perform lookaheads in a PEG without making the rule too greedy?

I'm writing a parser in Parslet. In the file I want to parse, I have the following structure: Item #1 denis calls 20 anna raises 60 denis calls 40 Item #2 another player raises 60 anna calls 60 ... To parse the actions, I did the following: As I…
Denis Lins
  • 816
  • 7
  • 22
2
votes
2 answers

Backtracking Recursive Descent Parser for the following grammar

I am trying to figure out some details involving parsing expression grammars, and am stuck on the following question: For the given grammar: a = b Z b = Z Z | Z (where lower-case letters indicate productions, and uppercase letters indicate…
Patrick Li
  • 672
  • 4
  • 11
2
votes
1 answer

How can I handle previously declared constants with a parser expression grammar?

Say I have the following, in a toy DSL: int foo(int bar = 0); With a tool such as rust-peg, I could define some simple parser expression grammar (PEG) rules to match it (assume appropriate structs FnProto and 'Arg'): function -> FnProto = t:type…
Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60
2
votes
1 answer

How to implement combination of rules without repeatings in ebnf grammar?

I am using Grako EBNF/PEG parser. I know that EBNF syntax allows to grab expressions that satisfy one of given options: (a | b | c) Is there a similar part of syntax or a workaround that allows to grab expessions that are any combinations of a, b…
Sashko Lykhenko
  • 1,554
  • 4
  • 20
  • 36
2
votes
1 answer

Is there a rule to match unicode printable characters in parboiled2?

As part of a larger parser, I am writing a rule to match strings like the following using parboiled2: Italiana Relè I would like to use something simple like the following: CharPredicate.Printable But the parser is failing with an…
David
  • 13,133
  • 1
  • 30
  • 39
2
votes
2 answers

Parsing special cases

If I understand correctly, parsing turns a sequence of symbols into a tree. My question is, is it possible to use some standard procedure (LR, LL, PEG, ..?) to parse the following two examples or is it necessary to write a specialized parser by…
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117
2
votes
1 answer

Return key, value object with dynamic key name

In PEG.js I have the following rule label = l:[a-zA-Z\$\#\% ]* { return word(l); } block = "[" l:label "]" { return l; } option = "\n"* key:block value:label "\n"? {return {key : value}; } If it parses [hello] world it results in: {"key":…
Tessmore
  • 1,054
  • 1
  • 9
  • 23
2
votes
1 answer

Why does this peg grammar not recognisze 42?

Using the grammar start = b / a a = "4" "2" b = "4" with peg.js recognizes 4 but not 42 in which case the error "Line 1, column 2: Expected end of input but "2" found." is reported. Obviously the parser completes the start -> b rule…
citykid
  • 9,916
  • 10
  • 55
  • 91
2
votes
2 answers

Escaped Strings in Parsing Expression Grammars

I am attempting to write a grammar for a small language utility using the python library parsimonious, but I am struggling with writing a part, which covers strings, especially strings with escaped quotes and other special characters. I have the…
Drakekin
  • 1,341
  • 1
  • 11
  • 23
2
votes
1 answer

Is there a parser generator for ruby that can generate a parser with no gem dependencies?

In an effort to make a DSL I have written backwards-compatible with ruby 1.8 I need to do some (relatively straightforward) parsing on the source strings. I could probably do directly with string munging, but in the interest of future…
Kaelin Colclasure
  • 3,925
  • 1
  • 26
  • 36
2
votes
1 answer

Is there a well-defined and well-reasoned transformation from LL(*) to PEG?

I am in the process of investigating PEG (Parsing Expression Grammar) parsers, and one of the topics I'm looking into is equivalence with other parsing techniques. I found a good paper about transforming regexes into equivalent PEGs at From Regular…
danfuzz
  • 4,253
  • 24
  • 34
1
vote
1 answer

What is the ellipsis (empty string) used for in a Treetop(PEG) grammar?

The Treetop website gives the following explanation that I don't understand Ellipsis An empty string matches at any position and consumes no input. It's useful when you wish to treat a single symbol as part of a sequence, for example when an…
RubenLaguna
  • 21,435
  • 13
  • 113
  • 151
1
vote
0 answers

How do I add custom syntax highlighting and autocompletion for a DSL made with PeggyJS?

I have created a DSL using PeggyJS to create my parser. Now I would like to have a programming environment with custom syntax highlighting and autocompletion. What is best practice to do this? I have looked in to CodeMirror or using VS code, but I'm…
proccan
  • 11
  • 1