grammar - Determine if a string can be derived ambiguously in a CFG -
i know given specific context free grammar, check if ambiguous requires checking if there exists string can derived in more 1 way. , undecidable.
however, have simpler problem. given specific context free grammar , specific string, possible determine if string can derived grammar ambiguously? there general algorithm check?
yes, can use generalized parsing algorithm, such glr (tomita) parser, earley parser, or a cyk parser; of can produce parse "forest" (i.e. digraph of possible parsers) in o(n3) time , space. (creating parse forest bit trickier "parsing" (that is, recognition), there known algorithms referenced in wikipedia article.
since generalized parsing algorithms find possible parses, can rest assured if 1 parse found string, string not ambiguous.
i'd stay away cyk parsing algorithm because requires converting grammar chomsky normal form, makes recovering original parse tree(s) more complicated.
bison generate glr parser, if requested, use tool. however, aware not optimize storage of parse forest, since expecting produce single parse, , therefore can end exponentially-sized datastructures (which take exponential time construct). that's problem pathological grammars, though.
Comments
Post a Comment