Next: Getting started, Previous: Top, Up: Top [Contents][Index]
A parser is a computer program that interprets the utterances of a computer language. A computer language is more properly known as a formal language. (Formal languages are almost entirely unlike natural languages—English, Pǔtōnghuà, Castellano, etc. It is usually a mistake to make analogies or comparisons between human languages and computer languages.)
A computer language is a (usually) infinite set of utterances or sentences. Each utterance is itself a finite string of symbols drawn from a finite alphabet. Although the set of all possible utterances is infinite, a useful computer language can be described by a finite (and usually quite short) grammar. For example, consider the computer language which consists of all the positive even numbers, written as decimals. This makes up an infinite set of utterances, written with an alphabet of ten symbols (‘0’, ‘1’, …, ‘9’). Not every combination of those symbols is a valid utterance of this language, but we don’t have to list all the ones that are. The language can be described very briefly: any combination of those symbols, provided the last one is one of (‘0’, ‘2’, ‘4’, ‘6’, ‘8’).
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)’ presumably 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.)
Next: Getting started, Previous: Top, Up: Top [Contents][Index]
Last updated: 2016-08-03 21:39:50 UTC
One thing pacc needs is more users. And, perhaps, one way to get more users is to reduce the friction in getting started with pacc. An obvious lubricant is packaging. Read More...
Looking at _pacc_coords(), I noticed that it seemed to have the same realloc() bug that I'd just fixed in _pacc_result(). However, the "list of arrays" trick really wasn't going to work here. Read More...