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

Custom Methods for Treetop Syntax Nodes

I have a Treetop PEG grammar that matches some keys. I want to look up the values associated with those keys in a hash I give the parser. How can I make it so that the syntax nodes have access to methods or variables from the parser? For example,…
Phrogz
  • 296,393
  • 112
  • 651
  • 745
0
votes
2 answers

PEG grammar to suppress execution in if command

I want to create a grammar parsing some commands. Most is working flawless but the "if(condition,then-value,else-value)" is not working together with "out" command to show some value. It works fine in case the output-command is outside the…
Achim
  • 442
  • 1
  • 3
  • 13
0
votes
1 answer

Which system should I use for parsing Scheme in OCaml?

I'm writing a compiler (badly) in OCaml, as a learning project; I'm at the point where using Jane Street's SexpLib isn't cutting it: match str.[0] with | '-' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' -> compile_int str…
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
0
votes
1 answer

PEG Parser generator how to fix Expected "whitespace" but " " found

I have a readable stream of text input (including unicode characters from html) from which I am trying to extract information by specifying the structure in PEG.js and returning custom JSON objects from matched items. I have the text input in the…
Harshit Laddha
  • 2,044
  • 8
  • 34
  • 64
0
votes
1 answer

How to describe conditional statement (if-then-else) using PEG

i'm working on Qt's qmake project file parser (open source project). And i have a trouble with describing qmake's variant of conditional statement, called "scope" in documentation. EBNF (simplified): ScopeStatement -> Condition ScopeBody Condition…
eraxillan
  • 1,552
  • 1
  • 19
  • 40
0
votes
1 answer

How to export state variable from PEG.js parser

I'm starting to use excellent PEG.JS JavaScript parser generator to implement Qt's qmake project file parser (*.pro). It looks bash script, with variable assignments and function calls. First of all, i need to parse all assignments to some kind of…
eraxillan
  • 1,552
  • 1
  • 19
  • 40
0
votes
1 answer

PEG.js and empty sequences

I'm trying to implement the TPTP grammar in PEG. It contains a rule for an empty sequence, which is used in many other rules, and PEG is rejecting this. A Google search finds…
rwallace
  • 31,405
  • 40
  • 123
  • 242
0
votes
1 answer

How can grammar from RFC 2396 (about URI) be expressed in PEG?

I am trying to come up with a PEG grammar that would parse a hostname according the following BNF of RFC 2396 hostname = *( domainlabel "." ) toplabel [ "." ] domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum toplabel …
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
0
votes
1 answer

what is wrong with this peg grammar?

the following grammar (from RFC 2396): domainlabel = 'a' / ('a' ('a' / '-')* 'a') cannot parse this: aa why?
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
0
votes
2 answers

Expected any character but end of input found

my input is a recursive structure looks like this (notice the blank 2nd line): xxx @{} yyy @{ zzz @{} wwww }   the grammar as i see that would read it should look like this: start = item+ item = thing / space thing = '@{' item* '}' space…
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
0
votes
1 answer

Get parsimonious to print useful error message for Sequence blocks

I'm using parsimonious (python PEG parser library) to parse text that looks like this: text = """ block block_name_0 { foo } block block_name_1 { bar } """ It is a series of blocks with a simple body requirement (must be alphanum) that…
therealtypon
  • 455
  • 2
  • 5
  • 12
0
votes
1 answer

Grammar to parse a chess move using PEG

What is the correct grammar to parse a move in SAN format using PEG? The solution I come up is MOVE <- [NBRQK]? [a-h]? [1-8]? [-x]? [a-h] [1-8] [NBRQ]? However, this seems wrong as it doesn't parse Nh4 because h matches the first optional file…
Alexandru
  • 25,070
  • 18
  • 69
  • 78
0
votes
1 answer

How do I convert PEG parser into ambiguous one?

As far as I understand, most languages are context-free with some exceptions. For instance, a * b may stand for type * pointer_declaration or multiplication in C++. Which one takes place depends on the context, the meaning of the first identifier.…
0
votes
2 answers

PEG parsing match at least one preserving order

Given the PEG rule: rule = element1:'abc' element2:'def' element3:'ghi' ; How do I rewrite this such that it matches at least one of the elements but possibly all while enforcing their order? I.e. I would like to match all of the following…
ARF
  • 7,420
  • 8
  • 45
  • 72
0
votes
1 answer

Why do parser combinators don't backtrack in case of failure?

I looked through the Artima guide on parser combinators, which says that we need to append failure(msg) to our grammar rules to make error-reporting meaningful for the user def value: Parser[Any] = obj | stringLit | num | "true" | "false" |…