coco_r_grammar_comparison.md 4.3 KB

Fundamental Difference Between the Two Coco/R Modula-2 Grammars

Files compared

  • mod2.atg — described in the file as an “Alternative grammar for PIM style Modula-2”.
  • pimmod2.atg — described as a Coco/R grammar for Modula-2 based on Programming in Modula-2.

Overall conclusion

The two grammars describe the same broad Modula-2 language, but they are not identical. mod2.atg is a more permissive/extended alternative grammar, while pimmod2.atg follows a more explicitly structured grammar based on Programming in Modula-2.

Important semantic differences

1. Multiple IMPORT declarations

mod2.atg uses:

ModDecl = "MODULE" id [ Priority ] ";" { Import } [ Export ] Block id .

The { Import } means zero or more imports.

pimmod2.atg uses:

ModuleDeclaration = "MODULE" ident [ Priority ] ";"
                     [ Import ] [ Export ] Block ident .

The [ Import ] means zero or one import at that point.

Therefore mod2.atg accepts multiple IMPORT declarations in a module, while pimmod2.atg does not.

2. FORWARD procedure declarations

mod2.atg supports:

ProcDecl = ProcHead ";" ( Block id | "FORWARD" ) .

So a procedure may be declared with FORWARD.

pimmod2.atg uses:

ProcedureDeclaration = ProcedureHeading ";" Block ident .

There is no FORWARD alternative.

3. Arithmetic/logical operators

mod2.atg allows:

MulOp = "*" | "/" | "DIV" | "REM" | "MOD" | "AND" | "&" .

pimmod2.atg allows:

MulOperator = "*" | "/" | "DIV" | "MOD" | "AND" .

Therefore mod2.atg additionally accepts REM and &.

4. Relational operators

mod2.atg allows:

Rel = "=" | "#" | "<>" | "<" | "<=" | ">" | ">=" | "IN" .

pimmod2.atg allows:

Relation = "=" | "#" | "<" | "<=" | ">" | ">=" | "IN" .

Thus mod2.atg additionally accepts <>.

5. Set elements

mod2.atg uses:

Elem = ConstExpr [ ".." ConstExpr ] .

pimmod2.atg uses:

Element = Expression [ ".." Expression ] .

So the first grammar restricts set elements/ranges to constant expressions, while the second permits general expressions.

6. Statement structure

mod2.atg combines assignment and procedure-call syntax:

Stat = [ Design ( ":=" Expr | [ ActPar ] )
      | IfStat | CaseStat | LoopStat | ForStat
      | RepeatStat | WhileStat | WithStat
      | "EXIT" | "RETURN" [ Expr ]
      ] .

pimmod2.atg factors these explicitly:

Statement = [ Assignment | ProcedureCall | IfStatement
            | CaseStatement | WhileStatement | RepeatStatement
            | LoopStatement | ForStatement | WithStatement
            | "EXIT" | "RETURN" [ Expression ] ] .

Assignment = Designator ":=" Expression .
ProcedureCall = Designator [ ActualParameters ] .

The second form is more explicitly structured and easier to distinguish conceptually.

Naming and organization differences

Many non-semantic differences are simply naming/style choices:

mod2.atg pimmod2.atg
id ident
QualId QualIdent
Design Designator
Fact Factor
Stat Statement
ProcDecl ProcedureDeclaration
ProcHead ProcedureHeading
DefMod DefinitionModule
ProgMod ProgramModule

These changes mostly affect readability and grammar organization rather than the accepted language.

Practical interpretation

Aspect mod2.atg pimmod2.atg
Overall language Modula-2 / PIM-style Modula-2 based on Programming in Modula-2
Grammar style Compact, condensed More explicit/factored
Multiple imports Yes No
FORWARD procedures Yes No
REM operator Yes No
& operator Yes No
<> relation Yes No
Set elements ConstExpr Expression
Assignment/call structure Combined Explicitly separated

Bottom line

mod2.atg can be viewed as the more permissive/extended practical grammar, while pimmod2.atg is the more explicitly structured PIM-oriented grammar.

For a practical Modula-2 editor/parser, mod2.atg is the more interesting starting point if the objective is to accept a broader range of existing source code. For compatibility with the grammar of Programming in Modula-2, pimmod2.atg is the closer reference.