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

PEG grammar not working as expected

I'm working on a PEG grammar that takes code in a Music Programming Language and creates a parse tree of musical events (notes, chords, volume/tempo changes, etc.). A feature of my MPL is that it supports voices, i.e. different sequences of events…
Dave Yarwood
  • 2,866
  • 1
  • 17
  • 29
3
votes
2 answers

How do I handle C-style comments in Ruby using Parslet?

Taking as a starting point the code example from the Parslet's own creator (available in this link) I need to extend it so as to retrieve all the non-commented text from a file written in a C-like syntax. The provided example is able to successfully…
zml
  • 617
  • 7
  • 14
3
votes
1 answer

Grammar: start: (a b)? a c; Input: a d. Which error correct at position 2? 1. expected "b", "c". OR expected "c"

Grammar: rule: (a b)? a c ; Input: a d Question: Which error message correct at position 2 for given input? 1. expected "b", "c". 2. expected "c". P.S. I write parser and I have choice (dilemma) take into account that "b" expected at position or…
mezoni
  • 10,684
  • 4
  • 32
  • 54
3
votes
1 answer

Make a Grammar expression for STRING.STRING.STRING on PEG.js

I am looking for a peg.js grammar expression for matching against: "variable" # Fails "variable." # Fails "" # Fails "variable.variable" # Ok "variable.variable.variable.variable.variable" #Ok input I expect {PATH: "variable.variable"} {PATH:…
rkmax
  • 17,633
  • 23
  • 91
  • 176
3
votes
1 answer

Lambda expressions in PEG.js

I have PEG grammar problem with lambda expressions, they work if I use syntax: x:{y:{x+y}}(20)(30) which is equivalent of (function(x) { return function(y) { return x+y; }; })(20)(30); but this don't work f:{f(10)}(x:{x*x}) which is equivalent…
jcubic
  • 61,973
  • 54
  • 229
  • 402
3
votes
1 answer

Generated parser throws error for escaped quotes on Node.js

I am using PEG.js to create a parser which includes parsing strings. The strings containing any kind of character are wrapped by quotes " and may contain escaped quotes \". So far I have the following rule: start = ["] string:(( '\\"' {return…
MKroehnert
  • 3,637
  • 2
  • 34
  • 43
3
votes
1 answer

How can I avoid left-recursion in treetop without backtracking?

I am having trouble avoiding left-recursion in this simple expression parser I'm working on. Essentially, I want to parse the equation 'f x y' into two expressions 'f x' and '(f x) y' (with implicit parentheses). How can I do this while avoiding…
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43
2
votes
2 answers

pyparsing: NotAny(FollowedBy()) failing

i have some input data like [gog1] [G1] [gog2] [gog3] [gog4] [G2] [gog5] [G3] [gog6] and want to find all gogs, if not G after it. so in this case i want to get gog2 and gog3 (and maybe gog6). looks pretty simple, rigth? but i failed :( import…
Jhon BYaka
  • 21
  • 5
2
votes
1 answer

It is possible to express in PEG something like /\s(foo|bar|baz)\s.*/

A regular expression like /\s(foo|bar|baz)\s.*/ would match the following string: football bartender bazooka baz to the end ^^^^^^^^^^^^^^^ Is it possible to make a Parsing Expression Grammar rules that would parse the…
dimus
  • 8,712
  • 10
  • 45
  • 56
2
votes
1 answer

Parse expression with JavaScript using peggy js

I have written a pseudo code to determine whether the string contains * or not and return the position(start, end, and wrapped) if it contains please check the code sandbox. The problem is the code works correctly for the string which contains * at…
Benk I
  • 185
  • 13
2
votes
1 answer

Building a grammar for variables with pest parser

Using pest parser I am trying to build a grammar that can recognize variable names, but I can't get the variables to end at the next space/non-alpha character. I tried using... var_name = {!reserved ~ ASCII_ALPHA+} which works perfectly for…
2
votes
2 answers

Peg parser - support for escape characters

I'm working on a Peg parser. Among other structures, it needs to parse a tag directive. A tag can contain any character. If you want the tag to include a curly brace } you can escape it with a backslash. If you need a literal backslash, that should…
user1065745
  • 787
  • 1
  • 5
  • 9
2
votes
1 answer

PEG: What is wrong wrong with my grammar for if statement?

I'm implementing an OCaml-like language using rust-peg and my parser has a bug. I defined if-statement grammar, but it doesn't work. I'm guessing the test-case input is parsed as Apply(Apply(Apply(Apply(f, then), 2) else), 4). I mean "then" is…
knium_
  • 93
  • 6
2
votes
1 answer

How to handle semantic failures in TatSu when parsing is correct?

I am trying to create a TatSu parser for a language containing C-like expressions. I have the following grammar rules for the expressions: identifier = /[a-zA-Z][A-Za-z0-9_]*/ ; expression = or_expr ; or_expr = …
Dominick Pastore
  • 4,177
  • 2
  • 17
  • 29
2
votes
1 answer

Packrat caching : Right to left vs. Left to right?

I'm currently trying to familiarize myself with packrat parsing. So I've read the PDF paper from 2002 linked here and in section 2.3 it describes packrat caching as a preliminary process (which occurs before the actual parsing) in which a full…
ostrebler
  • 940
  • 9
  • 32