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

How to output as hexadecimal and binary in PEG.js grammar in separate output lines?

There's a simple arithmetic grammar found in PEGjs, that I want to expand its output to 2 or 3 lines each containing expression results in Decimal, Hex and Binary format. Example: input: (3*(5+8)) output should be: 39\n27\n00100111 I searched for…
HabibS
  • 382
  • 6
  • 14
0
votes
0 answers

Issues with writing a recursive PEG grammar

I'm writing a parser in Rust, using the peg crate. Right now I have 3 main rules: identifier (variable name) expression (identifier or function call) function_call (the syntax of a function call) I want any expression to be callable, with any…
stacky
  • 13
  • 3
0
votes
0 answers

A way to pass cursor position to parboiled2 generated ast

I 'm just wondering if it's possible to pass a Position of the cursor to AST. I have a strong feeling that it might be possible inside the rule macro but at the quick glace haven't found anything. I need this to give more valuable error reporting on…
ppopoff
  • 658
  • 7
  • 17
0
votes
0 answers

Solving left recursion in packrat parser

I am looking at parsing arithmetic expression like (2 + (a + b ) ) + (3 + 2 ) * 2 using pypeg2. How to write grammar for this? The intension is to implement bin_expr ::= optional('('), var_or_const, Binary_operator, var_or_const, optional(')') |…
Mangesh T
  • 15
  • 4
0
votes
0 answers

Ignoring invalid syntax before a valid expression

I have a fully functional cpp-peglib grammar that works pretty well for parsing expressions like: SomeObj.value + (Math.random(0, 32)) * 3.14 - ("abcdef".find("c", 0)).toFloat() I would like to implement an autocomplete system where it parses…
Andos
  • 202
  • 3
  • 12
0
votes
1 answer

PEG error "expression cannot fail; following choices cannot be reached"

I tried to group together two cases to reduce code duplication in the grammar: From string = _{("'" ~ (string_value) ~ "'") | ("\"" ~ (string_value) ~ "\"") | ("\"" ~ (string_value_escape_1) ~ "\"") | ("'" ~ (string_value_escape_2) ~ "'")} …
Guy Korland
  • 9,139
  • 14
  • 59
  • 106
0
votes
3 answers

Making BBcode parser with PEG problem

I am making bbcode parser with PEG (Citrus implementation for Ruby) and I am stuck on parsing this [b]sometext[anothertext[/b] There is code grammar BBCodeParser rule document (open_tag | close_tag | new_line | text)* end rule open_tag …
Schovi
  • 1,960
  • 5
  • 19
  • 33
0
votes
1 answer

Rust pest parser fail

I'm learning pest parser written in Rust and I need to parse a file: {ISomething as Something, IOther as Other} I wrote a rule: LBrace = { "{" } RBrace = { "}" } Comma = { "," } As = { "as" } symbolAliases = { LBrace ~ importAliases ~ ( Comma ~…
PFA hard
  • 81
  • 5
0
votes
1 answer

How to write a peg used in lpeg to parse lua itself?

As title say, I know lua has a offical extended BNF in The Complete Syntax of Lua. I want to write a PEG to pass to lpeg.re.compile to parse lua itself. Maybe the Lua PEG is something like it's BNF. I have read the BNF and try to translate it to a…
rangercyh
  • 37
  • 4
0
votes
1 answer

Is there a Tatsu or any PEG-format grammar available for the [g]awk language syntax?

As the subject asks, does anyone know of an existing Tatsu grammar (or at least a PEG-format grammar) for the [g]awk language? I did already browse all existing Tatsu examples that I could find, and searched extensively around the net for any…
pjfarley3
  • 29
  • 2
0
votes
1 answer

PEG Grammar Parsing, error when expression starts with negative number

I have the following PEG grammar defined: Program = _{ SOI ~ Expr ~ EOF } Expr = { UnaryExpr | BinaryExpr } Term = _{Int | "(" ~ Expr ~ ")" } UnaryExpr = { Operator ~ Term } BinaryExpr = { Term ~ (Operator ~ Term)* } Operator = { "+" | "-" |…
Apollo
  • 945
  • 2
  • 9
  • 24
0
votes
1 answer

Why does TextX ignore \n in string literal, but not in regex?

TL;DR: The issue will be fixed in version 3.0 of TextX. The workaround is to use regex for matching escaped (\) characters, such as \n. FULL QUESTION: Using TextX, I am parsing a homegrown mark-up language, where paragraph and line breaks are…
MLAO
  • 3
  • 4
0
votes
2 answers

lua lpeg expression to not sub in between delimeters

I would like to understand how I could lpeg to replace strings if they are NOT between a certain start and end delimiter. Below is an example, where I would like to use SKIPstart and SKIPstop to signify where text shouldn't be…
likethevegetable
  • 264
  • 1
  • 4
  • 17
0
votes
2 answers

How do you define a decimal number in a PEG grammar?

I have the following grammar Arithmetic: Term < Factor (Add / Sub)* Add < "+" Factor Sub < "-" Factor Factor < Primary (Mul / Div)* Mul < "*" Primary Div < "/" Primary Primary < Parens / Neg /…
José Mancharo
  • 175
  • 1
  • 14
0
votes
1 answer

How does precedence works in rust-peg?

I have trouble in understanding pecedence! in rust-peg crate. Take a look at this code: peg::parser! { grammar example() for str { rule letter() -> &'input str = x:$(['a'..='z' | 'A'..='A']) rule ident() -> &'input str = x:$(letter()+) …
Doubtful
  • 83
  • 5