Recursive descent same prefix

I study recursive decent parsing and come up with some scenarios in which, I think, the algorithm does not work. One of them, given this simple grammar:

S → E;
E → id | id + id

Then the string is id + id;valid in the language of this grammar. However, if we execute the recursive descent algorithm, it drops from Sto E, then to id, which is the first matching terminal. Now the entrance is in +, and we are back in S, trying to match ;, which then fails; but there is no other level selection rule S.

I think that grammar is not ambiguous, as in the language id;and id + id;there are only 2 lines, each of which has a unique parse tree. The common problem here is that the non-terminal has derivatives with the same prefixes and potentially makes a choice that corresponds to a deeper level in recursion, but creates invalid inputs for lower levels.

I read about typical problems with recursive descents like left recursion, but did not find the problem mentioned above. So is this really a problem or am I missing something?


I found an authoritative answer from a book Parsing Techniques: A Practical Guide p.182-188that classifies the above approach as a naive recursive decent and emphasize the same problem. There are two solutions that always work in the general case without waiting (since in the general case the required lengths forward and forward increase with the length of the prefix): An exhaustive recursive descent that requires the use of extensions and a recursive descent in width.

+4
source share
3 answers

, PEG, . , PEG, PEG , , - .

PEG, CFG, , , , . , , - . , , , lookahead, .

+1

, , , , ? - :

func recogniseS
    expect(E)
    expect(semicolon)

fund recogniseE
    expect(id)

    if nextTokenIs(plus) then 
        expect(plus)
        expect(id)
    endif

, , :

S → id [+ id];

. , + . , , .

+1

, ( E' ):

S → E ;
E → id E'
E' → | + id

E' , ;, , +.

+1

Source: https://habr.com/ru/post/1568002/


All Articles