ANTLR v3 grammar for Pascal

by Martin Monperrus
(tagged as )
pascal3g contains an ANTLR v3 grammar for Pascal. It's based on the ANTLR v2 version that I found at http://antlr.org/grammar/pascal/pascal.g. Additionnally, it provides an export of the abstract syntax tree to XML. pascal3g is able to parse TeX.

Changes

The main changes from the ANTLR2 version are:

Support of the forward directive: procedure normalizeselector;forward;

Support for output values: write and writeln can have special parameters called output values, parameters followed by a colon and an integer (see http://jcoppens.com/soft/pfc2/prog/pascal-fc_lrm.pdf, p 74). E.g:
WRITE(LOGFILE,' ',STRPTR:1,' string')

Allow no program parameters: e.g. program TEX; (instead of program X(foo,bar);),

Fixed bug in case statements:
caseStatement 
    : CASE^ expression OF!
        caseListElement ( SEMI! caseListElement )*
      ( SEMI! ELSE! statements )?
      END!
    ;
caseStatement 
    : CASE^ expression OF!
        caseListElement ( SEMI! caseListElement )* SEMI!?
      ( ELSE! statements )?
      END!
    ;
Introduced SUBRANGE AST node: ASCIIcode=0..255; yields
<TYPEDECL value="=" line="8">
  <IDENT value="ASCIIcode" line="8">
  </IDENT>
  <SUBRANGE value="SUBRANGE" line="8">
    <NUM_INT value="0" line="8">
    </NUM_INT>
    <NUM_INT value="255" line="8">
    </NUM_INT>
  </SUBRANGE>
</TYPEDECL>
Support for packed 1..255 (that one sometimes sees): This requires that unpackedStructuredType can also be a subrangeType. Unfortunately, the rules related to types are no longer a tree, and this results in many warnings.

The BLOCK AST node means a block: In the previous version a repeat .... until was mapped to a block even in the absence of begin ... end. This is no longer the case.

Related work

I discovered at the end of this work the existence of another implementation for ANTLR3 at http://www.antlr.org/grammar/1279217060704/pascal3.zip from Marton Papp.