Such a program could be understood in (Free web hosting with ftp) two
Tuesday, July 31st, 2007Such a program could be understood in two ways: (1) if a then { if b then s1 else s2 } (2) if a then { if b then s1 } else s2 In most programming languages, an else must match the most recent possible then, so interpretation (1) is correct. In the LR parsing table there will be a shift-reduce conflict: Shifting corresponds to interpretation (1) and reducing to interpretation (2). The ambiguity can be eliminated by introducing auxiliary nonterminals M (for matched statement) and U (for unmatched statement): . S . M . S . U . M . if E then M else M . M . other . U . if E then S . U . if E then M else U But instead of rewriting the grammar, we can leave the grammar unchanged and tolerate the shift- reduce conflict. In constructing the parsing table this conflict should be resolved by shifting, since we prefer interpretation (1). It is often possible to use ambiguous grammars by resolving shift-reduce conflicts in favor of shifting or reducing, as appropriate. But it is best to use this technique sparingly, and only in cases (such as the dangling-else described here, and operator-precedence to be described on page 74) that are well understood. Most shift-reduce conflicts, and probably all reduce-reduce conflicts, should not be resolved by fiddling with the parsing table. They are symptoms of an ill-specified grammar, and they should be resolved by eliminating ambiguities. Team-Fly Team-Fly 3.4 USING PARSER GENERATORS The task of constructing a parser is simple enough to be automated. In the previous chapter we described the lexical-analyzer aspects of JavaCC and SableCC. Here we will discuss the parser- generator aspects of these tools. Documentation for JavaCC and SableCC are available via this book’s Web site.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.