简体   繁体   中英

Can a BNF grammar that does not follow operator associativity and precedence rules be considered as an unambiguous grammar?

Consider this BNF grammar:

<assign> = <id> = <expr>
<id> = A|B|C
<expr> = <id> + <expr>|<id> * <expr>|(<expr>)|<id>

This grammar is not ambiguous, since only one parse tree can be drawn for a statement.However, this clearly does not follow operator presedence rules.Operators *,+,() have the same precedence.Is this grammar unambiguous, or is it only not ambiguous? If it is unambigious, so a grammar can be unambigious without following the operator associativity and precedence rules,is that true?

"Unambiguous" and "not ambiguous" mean the exact same thing, which is that there is only one possible interpretation. That it is not the interpretation you expect is not relevant.

Is this grammar unambiguous?

Yes.

So a grammar can be unambigious without following the operator associativity and precedence rules,is that true?

Yes. Operator associativity and precedence are conventions from mathematics and programming languages. Ambiguity, or lack thereof, has nothing to do with whether a grammar conforms to those conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM