質問

Consider this simple grammar:

S -> a | b

The set of strings that may be generated by the grammar is:

{a, b}

Thus, a grammar generates a set of strings.

A parser for a grammar takes an input string and determines if the string could be generated by the grammar.

Thus, a parser is a recognizer for a grammar.

At least, that is one use of a parser.

But oftentimes a parser is used for other things. For example, a parser for a grammar may take an input string and create a tree structure which contains the input data and conforms to the grammar.

In that case the parser is not a recognizer, it is a data structure builder.

I conclude that there are different types of parsers.

Am I thinking logically? Are there indeed different types of parsers?

Has someone created a list of the different types of things that parsers have been created for?

Please let me know of any looseness or ambiguity in the above statements. I am trying to learn to be precise in statements about these concepts. For example, do you agree that "a grammar generates a set of strings"? Is that precise? correct?

役に立ちましたか?

解決

No, I do not agree. A grammar does not generate anything. It is a set of rules which define the structure of something. A parser takes a grammar and some form of input and produces some form of output, whether that be an abstract syntax tree, an indication of whether the input is well-formed according to the grammar, or whatever else it might be. There are different types of parsers, but not because of what they produce. Rather, parsers are classified based on what kind of grammars they can accept and how that grammar is interpreted. For example, there are LL parsers and LR parsers, with various subtypes having additional restrictions on, for example, how many tokens of lookahead are needed.

Regarding a grammar "generating" something, what would this generate?

S -> ("a" | "b") S?

As soon as the grammar becomes non-trivial, finding all valid input starts to no longer make sense.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top