A parser is a computer program that interprets the utterances of a computer language [1]. Computer languages vary enormously in complexity: the most elaborate are high-level programming languages. At the bottom end, the address box in a web browser understands an extremely simple computer language, the majority of whose utterances begin http://! Somewhere in between is the language of cells in a spreadsheet, in which one can say things like =A2+3*sum(C7:C9).
pacc is a parser generator. That means that we write a compact description of a computer language, invoke pacc with that description as its input, and pacc writes for us a parser for that language. The output of pacc is a C function, which we can use to build a complete program.
When we run a program that includes a pacc-built parser, the program will procure some input from somewhere — in most cases it will be a file, but it could be the command line, or a GUI input — and hand that input to the parser. The parser now has a couple of jobs to do.
First, it needs to decide if the input is in fact a valid utterance of the language described by the grammar. For example, if the language is simple arithmetic expressions, then 2 * (3 + 4) likely is a valid utterance, whereas 2 + * )( 3 4 is not. If the input is erroneous (not a valid utterance of the described language), then the parser needs to indicate how much it could parse before it encountered an error. If we are parsing a programming language, our utterances may be thousands of lines long, so good error-handling is vital.
Secondly, if the input was valid, the parser needs to discern its meaning. In most cases, this will involve building an Abstract Syntax Tree (AST) that captures the meaning of the input in a way that can easily be manipulated by the rest of the program. (In our first examples, though, we won't be building trees.)
| [1] | A computer language is more properly known as a formal language. It's unfortunate, in my opinion, that the word “language” is used here at all (although I can't think of a better word), because formal languages are almost entirely unlike natural languages. A skateboard and an aircraft carrier are both means of transport, but that's about all they have in common! |