Kevin Lu
Kevin Lu

Reputation: 53

Antlr4 parse postscript grammar

I'm writing a postscript grammar, then use Antlr4 to parse .ps file. I don't find any grammar file for postscript. Does anyone have the grammar file for postscript? Thanks!

Upvotes: 0

Views: 240

Answers (1)

luser droog
luser droog

Reputation: 19504

The only part of the language that needs a grammar is the { ... } construct. The rest can be handled by regular expressions, and in many cases the executable arrays can be implemented with extra recursion or looping patched onto a framework that is 99% regular expressions.

The description of the language is a relatively short part of the PLRM (at least the important first few sections). In brief,

  1. the tokenizer scans for contiguous non-whitespace, non-delimiter ({}[]()<>/) characters.
  2. attempt to convert to a realtype number or integertype number, convert to a name otherwise.
  3. any delimiter character terminates the previous token and initiates a new token.
    a. [ yields an executable name
    b. ] yields an executable name
    c. ( begins a string which may contain balanced parens and escapes
    d. < begins a hex string (or ASCII85 string if the next character is ~)
    e. / introduces the following token as a literal name
    f. { accumulate tokens until matching } and create an executable array. The sequence of tokens may contain balanced { and } pairs which construct subarrays

Upvotes: 2

Related Questions