Description of operator priority using EBNF

I wrote a token and expression analyzer for the preprocessor language, which I plan to use in my later projects. I started to think that maybe I should describe the language using EBNF (Extended Backus-Naur Form) in order to keep the syntax more supported or even use it to generate later versions of the parser.

My first impression was that EBNF is used to validate the token and check the syntax. Later, I discovered that it can also be used to describe the priority of an operator, such as in this post or in a Wikipedia article :

expression ::= equality-expression equality-expression ::= additive-expression ( ( '==' | '!=' ) additive-expression ) * additive-expression ::= multiplicative-expression ( ( '+' | '-' ) multiplicative-expression ) * multiplicative-expression ::= primary ( ( '*' | '/' ) primary ) * primary ::= '(' expression ')' | NUMBER | VARIABLE | '-' primary 

I see how this allows the generator to generate code with built-in operator priority, but should it really be expressed? Is operator priority not more about semantics and EBNF about syntax? If I decide to write a description of my language in EBNF, should I write it taking into account the priority of the operator or document, which is in a separate section?

+6
source share
1 answer

Was there similar material for my college degree.

I suggest NOT using the operator priority function, even if it looks simpler than syntactic sugar.

Most of the languages ​​that will be described by EBNF use many operators with various functions that are better described and updated using EBNF expressions instead of operator precedence.

Some operators are unary prefix, some unary posfix, some are binary (aka "infix"), some binary are evaluated from left to right, and some are evaluated from right to left. Some characters are operators in a certain context and are used as other tokens, in another context - an example of "+", "-", which can be binary operators ("x - y"), unary prefix operators ("x - -y" )) or part of the literal ("x + -5").

In my experience, it is more β€œsafe” to describe them with EBNF expressions. If the programming language that you describe is very small, with very few and similar syntax operators (example: all binary or all prefix unary).

Only my 2 cents.

+3
source

Source: https://habr.com/ru/post/973494/


All Articles