开发者

Am i forced to use %glr-parser?

I have been keeping the shift/reduce errors away. Now finally i think i met my match.

Int[] a
a[0] = 1

The problem is int[] is defined as

Type OptSquareBrackets

while a[0] is defined as

Var | Var '[' expr ']'

Var and Type both are defined as VAR which is any valid variable [a-zA-Z][a-zA-Z0-9_]. Apart from adding a dummy token (such as **Decl** Type OptSquareBrackets instead) is there a way to write this to not have a conflict? Fro开发者_如何学Pythonm this one rule i get 1 shift/reduce and 1 reduce/reduce warning.


Could you define a new Token

VarLBracket [a-zA-Z][a-zA-Z0-9_]*\[

And therefore define declaration

Type | VarLBracket ']';

and define assignment target as

Var | VarLBracket expr ']';


Create a Lex rule with [] since [] is only used in declaration and everywhere else would use [var]


Technically, this problem stems from trying to tie the grammar to a semantic meaning that doesn't actually differ in syntax.

ISTM that you just need a single grammar construct that describes both types and expressions. Make the distinction in code and not in the grammar, especially if there is not actually a syntactic difference. Yacc is called a compiler generator but it is not the least bit true. It just makes parsers.

Having said that, recognizing [] as a terminal symbol might be an easier way to fix the problem and get on with things. Yacc isn't very good at ambiguous grammars and it needs to make early decisions on which path to follow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜