Trying to figure out how to do simple expressions in if statement
47 views
Skip to first unread message
Steve Tuckner
unread,
Dec 2, 2014, 1:26:30 PM12/2/14
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to treet...@googlegroups.com
Hello all,
I have a fairly simple grammer where I am trying to parse and expression as part of an if statement. I want it to handle both of the following cases:
if 1 == 1 do end
and
if 1 + 1 == 2 do end
The relevant portions of the grammar are below. If I change the last thing in binary_expression to expression, then it dies when it hits the do. If I leave it as is, it can parse the first but not the second. Any ideas on the right way to do this?
Thanks,
Steve
rule if 'if' ws expression ws block <IfNode> end
rule block 'do' ws (statement ws)* 'end' <BlockNode> end
rule operand (numeric_literal / string_literal / identifier) <OperandNode> end
rule numeric_literal [\d]+ <NumericLiteralNode> end
rule string_literal "'" [^']* "'" <StringLiteralNode> end
rule symbol ':' identifier <SymbolNode> end
rule identifier identifier_start identifier_rest end
Steve Tuckner
unread,
Dec 9, 2014, 10:57:12 AM12/9/14
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to treet...@googlegroups.com
So here I am replying to my own post with a much simpler example and any help would be GREATLY appreciated. I am not sure how to parse something like: ( 1 + 2 + 3 + 4 ). My attempt below will only parse ( 1 + 2 ) successfully.
rule whole_expr '(' ws expr ws ')' end
rule expr [\d]+ ws '+' ws expr end
rule ws [ \t\n]+ end
The question is whether I am doing this in the right way at all. Is there a better way to have recursively defined rules?
Thanks,
Steve
Steve Tuckner
unread,
Dec 9, 2014, 3:30:05 PM12/9/14
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to treet...@googlegroups.com
Responding again to myself, It looks like the way to solve this is to offer an alternative path that recurses as the first option. So to fix this,
That seems to work. Still looking for feedback if this is the way to go or not.
Steve
Clifford Heath
unread,
Dec 9, 2014, 4:50:13 PM12/9/14
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to treet...@googlegroups.com
I prefer to use repetition to handle such operations, not recursion.
Recursion makes it harder to manage the arithmetic precedence.
Here’s how I do it:
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to treet...@googlegroups.com
I realize the thread is a little old but nevertheless - Steve, there is a sample that does just that (arithmetic expression parsing) in the examples/ subdirectory on the treetop github.