HELP: Setting up a grammar for expressions

SyntaxEditor for WPF Forum

Posted 1 month 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 1 month ago]

Comments (1)

Posted 1 month 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 (v22.1.0) 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.