This project has moved. For the latest updates, please go here.

newbie grammar question

Jun 29, 2009 at 7:50 PM
Edited Jun 30, 2009 at 1:59 AM

Hello I am new to all this. I saw samples for simple expression evaluators and they worked fine. I need to extend that to be able to do the following:

input: max(45,54) for 3 5+6 min(2,3) for 2

output: 54 54 54 11 2 2

where the keyword for is to repeat the number on left by number on right times.

even simpler input "2+3 4+5" to output "5 9"

What would the grammar be like for something like this.









Jun 30, 2009 at 2:22 AM

Well, hard to describe it without actually writing a grammar, but here's an action plan

You need to implement "for" operator:

forExpr.Rule = Expr + "for" + Number;

You also need to define an "sequence expression" - when several expressions are delimited by space:

seqExpr.Rule = MakePlusRule(seqExpr, Symbol(" "), Expr);

Test the grammar in parsing - once it parses correctly your samples, you need to implement "execution" part.

You need to define custom AST nodes for both non-terminals (forExpr and seqExpr), and implement Execute method in them accordingly.

So forExprNode.Execute should return string composed of n copies of the first arg; seqExprNode.Execute should concatenate all child expressions with space in between and return the resulting string.