Download Menhir Reference Manual - GODI Package Documentation
Transcript
current lookahead token, even though this might also be desirable. It is unclear whether this keyword is useful; it might be suppressed in the future. The $previouserror keyword evaluates to an integer value, and indicates how many tokens were successfully shifted since the last error token was shifted. This allows heuristics such as “when a new error is detected, do not display a new error message unless the previous error is ancient enough” to be implemented if and where desired. When are errors detected? An error is detected when the current state of the automaton has no action on the current lookahead token. Thus, understanding exactly when errors are detected requires understanding how the automaton is constructed. Menhir’s construction technique is not Knuth’s canonical LR(1) technique [10], which is too expensive to be practical. Instead, Menhir merges states [12] and introduces so-called default reductions. Both techniques can defer error detection by allowing extra reductions to take place before an error is detected. All LALR(1) parser generators exhibit the same problem. 9. Using Menhir as an interpreter When --interpret is set, Menhir no longer behaves as a compiler. Instead, it acts as an interpreter. That is, it repeatedly: • reads a sentence off the standard input channel; • parses this sentence, according to the grammar; • displays an outcome. This process stops when the end of the input channel is reached. 9.1 Sentences The syntax of sentences is as follows: sentence ::= [ lid : ] uid . . . uid \n Less formally, a sentence is a sequence of zero or more terminal symbols (uid’s), separated with whitespace, terminated with a newline character, and optionally preceded with a non-terminal start symbol (lid). This nonterminal symbol can be omitted if, and only if, the grammar only has one start symbol. For instance, here are four valid sentences for the grammar of arithmetic expressions found in the directory demos/calc: main: INT PLUS INT EOL INT PLUS INT INT PLUS PLUS INT EOL INT PLUS PLUS In the first sentence, the start symbol main was explicitly specified. In the other sentences, it was omitted, which is permitted, because this grammar has no start symbol other than main. The first sentence is a stream of four terminal symbols, namely INT, PLUS, INT, and EOL. These terminal symbols must be provided under their symbolic names. Writing, say, “12+32\n” instead of INT PLUS INT EOL is not permitted. Menhir would not be able to make sense of such a concrete notation, since it does not have a lexer for it. 9.2 Outcomes As soon as Menhir is able to read a complete sentence off the standard input channel (that is, as soon as it finds the newline character that ends the sentence), it parses the sentence according to whichever grammar was specified on the command line, and displays an outcome. An outcome is one of the following: • ACCEPT: a prefix of the sentence was successfully parsed; a parser generated by Menhir would successfully stop and produce a semantic value;