nearley | 📜🔜🌲 Simple , fast , powerful parser toolkit for JavaScript | Parser library
kandi X-RAY | nearley Summary
kandi X-RAY | nearley Summary
If you are citing nearley in academic work, please use the following BibTeX entry.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Compile structure
- Train the rail - diagram
- Generate a random rule
- Build a single token
- graph
- Generate random symbols
- render token .
- Initialize a new Parser with the given rules .
- Remove indent function
- Build the rule name for the given rule name .
nearley Key Features
nearley Examples and Code Snippets
Community Discussions
Trending Discussions on nearley
QUESTION
I am trying to generate a javascript parser for the ebnf grammar described in this Microsoft article. The ebnf specified in the article does not work when I use it as its written, so I have tried to simplify it to make it work with the REx parser generator.
The goal is in Javascript to be able to parse and evaluate expressions like these to True or False:
AttributeA > 2
- The value of AttributeA is greater than 2HasCategory(Assembly)
- The node has Category AssemblyAssembly
- The node has Category AssemblyHasValue(AttributeA)
- The attribute AttributeA has a value. Its not undefined.AttributeA < AttributeB
- The value of attribute AttributeA is less than the value of attribute Attribute BIsReference
- The value of the attribute IsReference is TrueAttributeA + 2 > 5 and AttributeB - 5 != 7
AttributeA * 1.25 >= 500
I am using the REx parser generator online here: https://bottlecaps.de/rex/. If you have suggestions for other parser generators that produce JavaScript I would appreciate some links to where I can find them.
The issue I'm struggling with is the definition of the MethodCall. I have tried a lot of different definitions but all fail. When I remove the MethodCall and MethodArgs definition the REx parser generator produces a parser.
So I would appreciate any help to crack this problem a lot.
Below is the grammar as far as I have been able to get.
...ANSWER
Answered 2021-Aug-20 at 12:08There are a few problems with your grammar, but it's mostly fine.
- 'and' and 'or' conflict with
Identifier
. So, subtract those string literals fromIdentifier
in its rule. Number
was missing parentheses. It should beNumber ::= Digit ( ('.' Digit) | (Digit)* )
- You are missing the EOF rule. Almost every parser generator I know requires a bottom/EOF rule to force consumption of the entire input. I added the rule "Input".
- Make sure to click the "configure" box, then "backtracking". Your grammar is ambiguous, which is fine, but requires you to tell the parser generator to handle that.
Parser generators have a slightly different syntax for "EBNF", which is what REx takes. REx adds a string to denote the boundary between parser and lexer rules. Microsoft says the grammar is "BNF" but it's not because it uses the Kleene operator
::= [^. ]*
, an EBNF construct. It also fudges the definition of and
with prose.
I haven't tested the generated parser, but it seems like a straightforward recursive descent implementation. The parser generators that I am familiar with, and that are popular, are listed in the conversion page. (I'm writing converters for all of them and many more.)
Try this:
QUESTION
Say I had a simple language to parse in nearley that's just made of strings. "this is a string"
ANSWER
Answered 2021-Apr-22 at 06:03I'd use a regex-based lexer, certainly. But you could try to write an unambiguous grammar, based on the observation that you can never have two adjacent chars
in a charCode
:
QUESTION
I'm trying to parse a very simple language with nearley: you can put a string between matching opening and closing tags, and you can chain some tags. It looks like a kind of XML, but with[
instead of <
, with tag always 2 chars long, and without nesting.
ANSWER
Answered 2021-Mar-09 at 03:49Your language is almost too simple to need a parser generator. And at the same time, it is not context free, which makes it difficult to use a parser generator. So it is quite possible that the Nearly parser is not the best tool for you, although it is probably possible to make it work with a bit of hackery.
First things first. You have not actually provided an unambiguous definition of your language, which is why your parser reports an ambiguity. To see the ambiguity, consider the input
QUESTION
I am having difficulty "compressing" the results of my grammar by stripping out null values and embedded arrays using Javascript. I am using the Nearley grammar checker which can run JS functions after a sentence is matched. Unfortunately you get the results of the full parse as a series of arrays. The following is an example of the output
...ANSWER
Answered 2020-Nov-30 at 18:52var arr = [
[
[
[
[
[
[ [ 'climb'], [ [ null, 'to' ] ] ],
[ [ null, [ 'alt' ] ] ],
[ 332, [ null, [ 'km' ] ] ]
]
],
[ null ]
]
]
]
]
var iterator = arr.values();
for (let onlyitems of iterator) {
console.log(onlyitems);
}
QUESTION
I am using nearley and moo to come up with a rather complex grammar. It seems to be working fine EXCEPT for my whitespace requirements. I need to require whitespace when needed and allow it when not while keeping the grammar unambiguous.
For example:
...ANSWER
Answered 2020-Nov-24 at 19:23I'm not familiar with Nearly nor Moo but the regex could be
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install nearley
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page