Removing Left Recursion
First of all, I'm still a but confused about the standard way of removing it for left-associative operators:
http://stackoverflow.com/questions/2...left-recursion Especially about how to construct an AST from it by executing a peice of code for every nonterminal matched. And I'm still not totally convinced that it will be left-associative, I just barely understand it. Also, how would you remove left-recursion for something like this: Code:
expr ::= expr "(" param_list ")" |
Quote:
Code:
no_parenthesis_expr OPTIONALLY_FOLLOWED_BY arg_list_in_parenthesis |
Quote:
Code:
my_function()() |
Quote:
Code:
list_of_arg_lists_in_parenthesis And since the suggested construct is list, the list itself doesn't require recursion, but, of course, the list elements do. For lists in my parser-generator engine I had a REPEATED_ITEMS construct whose members were other language items, i.e. for my engine I would write Code:
REPEATED_ITEMS(arg_list_in_parenthesis) |
Quote:
|
Maybe it's OK, but it doesn't follow the way the language is supposed to work (any expression can be called, including but not limited to functions).
I guess you can make the list_of_arg_lists make an AST that calls the expr_without_parens and then calls the result of the previous call with the args in the next list, but it seems like there should be a better way. ---------- Post added 2011-04-09 at 10:45 ---------- Quote:
|
Quote:
Or, in other words, what prevents nodes in the AST from knowing who their parents are ? |
Quote:
Code:
call_param_list_list ::= OPAREN call_param CPAREN {OPAREN call_param CPAREN} |
This reply is a bit late since I figured it out a long time ago, but I thought I would say how I did it:
I used loops instead of recursion. It's much simpler, very intuitive, and infix operators can easily be made left-associative. The only disadvantage that I see is that it's impossible to describe in BNF. Here's an example: Code:
expr = expr2 (("+" | "-") expr2)* And it's very nice not to depend on Lex/YACC (or any other parser-generator tool, for that matter) anymore. It's much easier to interface to, gives detailed syntax error information, and it's designed so that multiple instances of it can run at the same time in different threads if I ever need that. And of course, I got a much better understanding of parsers. |
All times are GMT -5. The time now is 01:04 AM. |