PG is a DSL (domain specific language). PG is to perl kind of like LaTeX is to TeX. LaTeX is written in TeX and mostly extends the language, although sometimes it changes the underlying language a bit. (LaTeX used to be much worse in this regard when it was first written.)
PG behaves much the same way and in fact was loosely modeled on the LaTeX/TeX relationship. For the most part it uses the perl parser but it overlays this with some preprocessing and with a large number of "macros", subroutines that extend the power of perl in ways that are useful writing math questions.
You can look through the code in this file to get an overview of how a .pg file is processed.