Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Backus-Naur Form in Python

I know there are some vaguely similar questions already relating to BNF (Backus-Naur Form) grammars in Python, but none of them help me much in terms of my application.

I have multiple BNFs that I need to write code for. The code should be able to both generate and recognize legal strings using the BNF grammar.

The first BNF I'm working with is for all real numbers in Python. It is as follows:

<real number>    ::= <sign><natural number> |
                     <sign><natural number>'.'<digit sequence> |
                     <sign>'.'<digit><digit sequence> |
                     <sign><real number>'e'<natural number>
<sign>           ::= ‘’ | ‘+’ | ‘-‘
<natural number> ::= ‘0’ | <nonzero digit><digit sequence>
<nonzero digit>  ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<digit sequence> ::= ‘’ | <digit><digit sequence>
<digit>          ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Any BNF parsers I've found for Python seem extraordinarily complex, or use outside libraries. Is there any simpler way to check against and generate using BNF grammar in Python?

like image 785
Jakemmarsh Avatar asked Dec 23 '12 18:12

Jakemmarsh


People also ask

What is BNF in Python?

Backus-Naur Form (BNF) is a syntax for describing a context-free grammar. It was invented for describing the syntax of programming languages, and is still commonly used in documentation and language parsers.

What is the difference between BNF and EBNF?

BNF syntax can only represent a rule in one line, whereas in EBNF a terminating character, the semicolon, marks the end of a rule. Furthermore, EBNF includes mechanisms for enhancements, defining the number of repetitions, excluding alternatives, comments, etc.


2 Answers

This post contains an example of a lexical scanner which doesn't need third-party libraries. It may not do all you want, but you should be able to use it as a basis for something that fits your needs.

I don't know if your applications all relate to lexical scanning - but if not, ply is a fairly easy to use parser (given that you need to know broadly how parsers work).

like image 146
Vinay Sajip Avatar answered Sep 22 '22 04:09

Vinay Sajip


have a look at https://github.com/erikrose/parsimonious

Parsimonious aims to be the fastest arbitrary-lookahead parser written in pure Python—and the most usable. It's based on parsing expression grammars (PEGs), which means you feed it a simplified sort of EBNF notation.

like image 28
cleder Avatar answered Sep 20 '22 04:09

cleder