HELP: Setting up a grammar for expressions

SyntaxEditor for WPF Forum

Posted 2 years ago by Josh
Version: 18.1.3
Avatar

Hello,

Consider the following code:

statementA 5
statementA (5)
statementA 5 + mathFunc(22)
statementA (5 + mathFunc(22) * 33)
a = (5)
a = a + (mathFunc(22) + 2 * 5)

How can I write a grammar that understands expressions for any level of depth?

[Modified 2 years ago]

Comments (1)

Answer - Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Josh,

The best place to start in the design stage is by building out a formal EBNF grammar that works for your language syntax.  Our LL(*) Parser Framework uses EBNF-like syntax, so you would then translate your formal EBNF grammar over to productions in our framework.

As for expressions, you can nest expressions, but do so in order of precedence.  For instance the outermost level would be Expression, then you might have Expression's production call one for AssignmentExpression, and that call into a BinaryOperatorExpression, and so on down the levels of precedence until you get to a PrimaryExpression, things like Identifier.

Things like a ParenthesizedExpression would consume the opening '(' and then call into Expression again, followed by ')'.

See our Getting Started #4 QuickStart's SimpleGrammar.cs file for some examples on expression setup in a grammar.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.