Search code examples
parsinggrammar

Which rule allows for the following reduction?


Let's say I have the following SQL statement:

SELECT 1 + myField + 2

If we want to reduce the expression at the parsing phase before sending it to SQL, we could turn it into:

SELECT 3 + myField

What is the name of the rule that allows this? Another example being:

SELECT myField * (1 + myField * 2 + 3)

Would turn into:

SELECT myField * (4 + (myField * 2) )

Solution

  • That's usually called "constant folding" (which should be a good search term) and it's easier to do as a transformation on the AST than trying to do it while you parse, precisely because of the reordering which is evident in your examples.