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

Antlr4 how to build a grammar allowed keywords as identifier

This is a demo code label: var id let id = 10 goto label If allowed keyword as identifier will be let: var var let var = 10 goto let This is totally legal code. But it seems very hard to do this in antlr. AFAIK, If antlr match a token let, will…
wener
  • 7,191
  • 6
  • 54
  • 78
6
votes
1 answer

Are parsing expression grammars suited to parsing the shell command language?

The POSIX shell command language is not easy to parse, largely because of tight coupling between lexing and parsing. However, parsing expression grammars (PEGs) are often scannerless. By combining lexing and parsing, it seems that I could avoid…
Demi
  • 3,535
  • 5
  • 29
  • 45
6
votes
2 answers

Creating a recursive LPeg pattern

In a normal PEG (parsing expression grammar) this is a valid grammar: values <- number (comma values)* number <- [0-9]+ comma <- ',' However, if I try to write this using LPeg the recursive nature of that rule fails: local lpeg =…
Phrogz
  • 296,393
  • 112
  • 651
  • 745
6
votes
0 answers

Parsing Expression Grammar for syntax highlighting

First... Would it be possible to accomplish simple syntax highlighting using a PEG. I'm only looking for it to be able to recognize and highlight basic things that are common to c style languages Second... If there are any examples of this or…
Matt Zera
  • 465
  • 3
  • 12
5
votes
1 answer

LPeg grammar oddity

Part of a Lua application of mine is a search bar, and I'm trying to make it understand boolean expressions. I'm using LPeg, but the current grammar gives a strange result: > re, yajl = require're', require'yajl' > querypattern = re.compile[=[ …
mmirate
  • 704
  • 6
  • 25
5
votes
1 answer

How to handle ambiguity in syntax (like in C) in a Parsing Expression Grammar (like PEG.js)

So from my limited understanding, C has syntax ambiguity as seen in the expression: T(*b)[4]; Here it is said about this sort of thing: The well-known "typedef problem" with parsing C is that the standard C grammar is ambiguous unless the lexer…
Lance
  • 75,200
  • 93
  • 289
  • 503
5
votes
1 answer

How do I handle negative numbers in a PEG grammar?

I'm trying to write a simple int expression parser using tatsu, a PEG-based Python parser generator. Here is my code: import tatsu grammar = r''' start = expression $ ; expression = add | sub | term ; add = expression '+' term ; sub…
itsadok
  • 28,822
  • 30
  • 126
  • 171
5
votes
0 answers

Any PEG parser capable to handle left recursion?

Well, I know it's possible to rewrite the grammar to eliminate left recursion. But this is a very boring process, and sometimes it's very nontrivial to keep correct associativity. Is there any parser capable to handle properly grammars with left…
user536232
  • 645
  • 5
  • 18
5
votes
1 answer

PEG.js Get any text between ( and );

I'm trying to catch some text between parathesis with a semicolon in the end. Example: (in here there can be 'anything' !"#¤);); any character is possible); I've tried this: Text = "(" text:(.*) ");" { return text.join(""); } But it seems (.*)…
mottosson
  • 3,283
  • 4
  • 35
  • 73
5
votes
1 answer

How do you parse nested comments in pegjs?

I was wondering how do you parse comments (say, a la Haskell), in pegjs. The goal: {- This is a comment and should parse. Comments start with {- and end with -}. If you've noticed, I still included {- and -} in the comment. This…
Hassan Hayat
  • 1,056
  • 8
  • 20
5
votes
2 answers

PEG.js extension with predefined functions and variables

I have had a look at the PEG.js parser generator for JavaScript. It looks pretty nice! I don't have much experience with specifying grammars. I am searching for help to extend the example grammar at 1 a bit to allow for Decimal numbers Operator…
mikldk
  • 194
  • 1
  • 4
5
votes
1 answer

Problems with an ambiguous grammar and PEG.js (no examples found)

I want to parse a file with lines of the following content: simple word abbr -8. (012) word, simple phrase, one another phrase - (simply dummy text of the printing; Lorem Ipsum : "Lorem" - has been the industry's standard dummy text, ever since the…
static
  • 8,126
  • 15
  • 63
  • 89
5
votes
1 answer

Parsing of optionals with PEG (Grako) falling short?

My colleague PaulS asked me the following: I'm writing a parser for an existing language (SystemVerilog - an IEEE standard), and the specification has a rule in it that is similar in structure to this: cover_point = [[data_type]…
Apalala
  • 9,017
  • 3
  • 30
  • 48
5
votes
2 answers

Parsing complete mathematical expressions with PEG.js

I'm trying to extend the example grammar of PEG.js for parsing mathematical expressions with all the 4 operators for my online BASIC interpreter experiment: http://www.dantonag.it/basicjs/basicjs.html but not all the expressions are parsed…
friol
  • 6,996
  • 4
  • 44
  • 81
5
votes
1 answer

parsimonious parser - error trying to parse assignment grammar

I'm using the Python Parsimonious Parser to try to build an interpreter for a simple language I'm designing. I watched this tutorial video which was very helpful, and now I'm slowly modifying the code to match my own rules. I'm stuck on an…
rafiki_rafi
  • 1,177
  • 2
  • 11
  • 27
1 2
3
19 20