Pergunta

Given the following input:

int x = y;

and

int x = y();

Is there any way for an LALR(1) grammar to avoid a shift/reduce conflict? The shift/reduce conflict is deciding to reduce at y or continue to (.

(This is assuming that a variable name can be any set of alphanumeric characters, and function call is any set of alphanumeric characters following by parentheses.)

Foi útil?

Solução

It's not a shift-reduce conflict unless it is possible for an identifier to be immediately followed by an ( without being a function call. That's not normally the case, although in C-derived languages, there is the problem of differentiating cast expressions (type)(value) from parenthesized-function calls (function)(argument).

If your grammar does not exhibit that particular C wierdness, then the LALR (1) grammar can decide between shifting and reducing based on the (1) token lookahead: if the lookahead token is a (, then it shifts the identifier; otherwise, it can reduce.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top