bison: How does one solve the Varname&Varname reduce/reduce problem?
Lets say you are writing a C++ like language and you allow references
int& i
You also开发者_JAVA技巧 allow bitwise ands
7&5
How do you solve reduce/reduce conflicts? I dont mean use the glr-parser but have no conflicts at all. Example syntax problem is
var&var //could be type& var or logical val & val
You distinguish between the sequence of grammatical terms:
<type> & <identifier>
and
<identifier> & <identifier>
though in the latter case, you are probably really dealing with:
<expression> & <expression>
or some similar construct where the '<expression>
' ends up as literals integers or simple variable names.
This can require semantic feedback to the lexical analyzer - it needs to be able to recognize that int
(in the example) is a type name (key word) and not a general identifier, but it also needs to be told about typedef names so that they can be treated as types too. Remember, though, that typedef names are scoped, in general.
You could tell flex to return a different token for the literal &
depending on what has been "lexed" before. So you end up with two rules:
<type> T_AMPERSAND <identifier>
and
<expression> T_LOGIC_AND <expression>
If your new language supports user defined types, the lexer will not know whether or not it had just found a <type>
or an <identifier>
, though. In that case you might want to make sure that <type> '&' <identifier>
is regarded a valid standalone statement whereas <expression> '&' <expression>
can only appear as an rvalue (at right right side of an assignment).
精彩评论