ebnf | Image generator for EBNF - To see the CI status click
kandi X-RAY | ebnf Summary
kandi X-RAY | ebnf Summary
EBNF is a code that expresses the grammar of a computer language. An EBNF consists of terminal symbol and non-terminal production rules which are the restrictions governing how terminal symbols can be combined into a legal sequence. Wikipedia.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Renders a syntax node .
- Parse the factor .
- Read next token .
- Parse the options
- Create open tag .
- Remove quotes from a string
- Visit a visitable node .
- Recursively visit this node
- Render template .
- Adds one or more cols to the matrix
ebnf Key Features
ebnf Examples and Code Snippets
Community Discussions
Trending Discussions on ebnf
QUESTION
How can I convert this EBNF rules below with K Framework ?
An element can be used to mean zero or more of the previous: ...ANSWER
Answered 2021-May-31 at 11:20No, there is currently no mechanism to do this without the extra production.
I typically do this as follows:
QUESTION
I am tasked to parse (and transform) a code of a computer language, that has a slight quirk in its rules, at least I see it this way. To be exact, the compiler treats new lines (as well as semicolons) as statement separators, but other than that (e.g. inside the statement) it treats them as spacers (whitespace).
As an example, this code:
...ANSWER
Answered 2021-May-29 at 00:22The relevant quote from the "specification" is this:
A squirrel program is a simple sequence of statements.:
stats := stat [';'|'\n'] stats
[...] Statements can be separated with a new line or ‘;’ (or with the keywords
case
ordefault
if inside a switch/case statement), both symbols are not required if the statement is followed by ‘}’.
These are relatively complex rules and in their totality not context free if newlines can also be ignored everywhere else. Note however that in my understanding the text implies that ;
or \n
are required when no of the other cases apply. That would make your example illegal. That probably means that the BNF as written is correct, e.g. both ;
and \n
are optionally everywhere. In that case you can (for lark) just put an %ignore "\n"
statement and it should work fine.
Also, lark should not complain if you both ignore the \n
and use it in a rule: Where useful it will match it in a rule, otherwise it will just ignore it. Note however that this breaks if you use a Terminal that includes the \n
(e.g. WS
or /\s/
). Just have \n
as an extra case.
(For the future: You will probably get faster response for lark questions if you ask over on gitter or at least put a link to SO there.)
QUESTION
I am using Bison, together with Flex, to try and parse a simple grammar that was provided to me. In this grammar (almost) everything is considered an expression and has some kind of value; there are no statements. What's more, the EBNF definition of the grammar comes with certain ambiguities:
expression OP expression
where op may be '+', '-' '&' etc. This can easily be solved using bison's associativity operators and setting %left, %right and %nonassoc according to common language standards.IF expression THEN expression [ELSE expression]
as well asDO expression WHILE expression
, for which ignoring the common case dangling else problem I want the following behavior:
In if-then-else as well as while expressions, the embedded expressions are taken to be as long as possible (allowed by the grammar). E.g
5 + if cond_expr then then_expr else 10 + 12
is equivalent to5 + (if cond_expr then then_expr else (10 + 12))
and not5 + (if cond_expr then then_expr else 10) + 12
Given that everything in the language is considered an expression, I cannot find a way to re-write the production rules in a form that does not cause conflicts. One thing I tried, drawing inspiration from the dangling else example in the bison manual was:
...ANSWER
Answered 2021-Mar-23 at 04:16The following associativity declarations resolved all shift/reduce conflicts and produced the expected output (in all tests I could think of at least):
QUESTION
This is a grammar in EBNF to describe C:
...ANSWER
Answered 2021-Feb-11 at 12:12Is this right?
No. The grammar show merely says things like “Only switch
, break
, and continue
can have case some-constant :
labels, while the others can have some-constant :
.
To say that a case
label can only be used on commands that are directly inside a switch
, the body of a switch, or presumably the body of a compound statement that is the body of a switch, must be a statement or list of statements of a type that can have case
labels, whereas the statements or list of statements that appear elsewhere must be statements of a type that cannot have case
labels.
So define a grammar token like MayBeLabeledStatement
that can have a case
label and another grammar token like Statement
that cannot have a case
label. Both statements may expand to the various kinds of statements (expression, if
, while
, and so on), but the ones for MayBeLabeledStatement
also may have case
labels. Further, all the statements within a MayBeLabeledStatement
should be Statement
type statements, which cannot have case
labels—except you may need to think about how to handle the single statement inside a switch
(which could be a compound statement or not).
Similarly, you will need different grammar tokens to handle statements inside a while
or switch
(which allow break
) versus those not inside a while
or switch
and another for those within a while
(which allow continue)
versus those not inside a while
. You may also need some combinations, for statements that are inside a while
and are directly inside a switch
versus statements that are inside a while
and are not directly inside a switch
.
QUESTION
I've just started writing a simple parser in C# that you can find here: https://github.com/JohnnyErnest/LexerParser/blob/main/Program.cs#L1078
The main feature being that I wanted to be able to write a JSON configuration for some grammar that I want to analyze, load up the configuration at runtime and evaluate some string input via the lexer/parser and generate a tree with information about each node for things like syntax highlighting and extracting variables from sections of a parsed text, rather than using a third party tool like a yacc/bison clone to create code to be compiled from a grammar, for some languages like SQL/CSS/HTML.
The simple question is, what are some other good methods of detecting recursion problems early from malformed input before a StackOverflowException? The details are below.
I don't have grammars fully built out yet for various languages, just simple tests of some basic grammar so far, but I ran across a problem when trying to implement EBNF like: https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form when having to deal with how the right-hand side of grammar statements are evaluated like so:
...ANSWER
Answered 2021-Jan-13 at 07:40Answer is explained better here: https://stackoverflow.com/a/20179940/5556724
The version of EBNF on Wikipedia is written like so:
QUESTION
I have been trying to build a program in which I am having to export the Prometheus metrics (Prometheus Exposition Format) to Amazon Cloudwatch in Go.
I have been able to obtain the metrices by sending an HTTP GET as follows:
...ANSWER
Answered 2020-Dec-21 at 11:48There's a nice package already available to do that and it's by the Prometheus's Authors itself.
They have written a bunch of Go libraries that are shared across Prometheus components and libraries. They are considered internal to Prometheus but you can use them.
Refer: github.com/prometheus/common doc. There's a package called expfmt
that can decode and encode the Prometheus's Exposition Format (Link). Yes, it follows the EBNF syntax so ebnf
package could also be used but you're getting expfmt
right out of the box.
Package used: expfmt
Sample Input:
QUESTION
Let's say I have the following EBNF defined for a simpler two-term adder:
...ANSWER
Answered 2020-Dec-06 at 01:45The XML standard, as an example, uses the following production for whitespace:
QUESTION
I could capture a parenthetical group using something like:
...ANSWER
Answered 2020-Dec-03 at 10:15In order to be able to match an arbitrary amount of anything (be it parentheses, operators, list items etc.) you need recursion (EBNF also features repetition operators that can be used instead of recursion in some cases, but not for constructs that need to be matched like parentheses).
For well-matched parentheses, the proper production is simply:
QUESTION
The following EBNF rule expressed as
...ANSWER
Answered 2020-Nov-13 at 02:36The EBNF production for CharData
,
[14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
means that XML character data can consist of any characters except
<
, which begins markup (tags, comments, XML declarations, CDATA sections, and PIs)&
, which begins entity references,- the string of characters,
]]>
, which ends a CDATA section.
Escaping:
- Escape
<
as<
in character data. - Escape
&
as&
in character data. ]]>
cannot appear in character data; there is no escaped form.
See also:
QUESTION
I need to analyze Elisp (Emacs Lisp) code so I wrote a parser for it using Instaparse. I expected it to be slow but doing 1k lines per second is way too slow to be right even on a calculator (or my pretty old i7). Can it be that bad or do I do something extremely wrong?
It's unambiguous and I tried to keep look ahead/behinds at minimum, unfortunately Elisp is very liberal with what constitutes as a symbol so I had to add some ahead/behinds there to differentiate numbers and symbols. Also I tried to deffer this by parsing symbols, numbers and keywords as "ident" it only gave me back like 30% of time. From my tests, it looks like Instaparse struggles a lot with recursive rules and lisps have highly recursive nature so maybe I didn't mess it up - it's just that slow...
The parser:
...ANSWER
Answered 2020-Oct-09 at 13:27If you are interested in speed, and you do not want to worry for stack overflow occurrences, you may try Tunnel Grammar Studio, a parser generator I work on. The generated parsers from it are iterative during lexing, parsing, the tree construction, tree iteration, tree to string conversion and tree release. The accepted grammars are in ABNF (RFC 5234) with case sensitivity per token (RFC 7405).
Its a good idea to have a deterministic grammar with any parser you use. TGS does check for LL(1) conflicts at compile time, and will help you to create a deterministic grammar by visualizing the conflict places.
There is a demo of the tool, you can test the speed yourself. There is an option in the tool to generate fully ready test case project that will log into the console at runtime the time taken to parse, iterate the tree and free it, by only supplying the input data. Meaning that no development (except compiling the generated code) from you is expected if you want to test the speed for a grammar.
In my tests with a JSON grammar (RFC 8259) with removed ambiguity, that only emits syntax tree build events (like SAX) the iterative parser runs with around 8 megabytes per second, that are many lines per second and takes memory only proportional to the depth of the parsing, because only one token is technically needed at runtime for LL(1) grammars, i.e. it is practically "streaming" the input.
You can have also statically typed or dynamically typed concrete syntax tree, or dynamically typed abstract syntax tree with different levels of abstraction (i.e. automatic node pruning). The syntax trees builders (if chosen) for this trees are using the build events to create the relevant trees. You will need an ABNF grammar and C++ as a language target however.
The tool supports token ranges inside the parser grammar (additionally to the character ranges inside the lexer grammar). This means that you may develop your grammar without the extra care of the lexical rules order.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install ebnf
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