ParvaDoc.atg



import Library.*;
import java.util.*;

COMPILER ParvaDoc $CN
/* Parva level 2 grammar  - Coco/R for C# or Java
   P.D. Terry, Rhodes University, 2005
   Grammar only  -  LL(1)
   As supplied for CSC 301 examination - 2005 */
















CHARACTERS
  lf          = CHR(10) .
  backslash   = CHR(92) .
  control     = CHR(0) .. CHR(31) .
  letter      = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .
  digit       = "0123456789" .
  stringCh    = ANY - '"' - control - backslash .
  charCh      = ANY - "'" - control - backslash .
  printable   = ANY - control .





TOKENS
  identifier  = letter { letter | digit | "_" } .
  number      = digit { digit } .
  stringLit   = '"' { stringCh | backslash printable } '"' .
  charLit     = "'" ( charCh   | backslash printable ) "'" .


PRAGMAS /* if required */



COMMENTS FROM "//" TO lf


COMMENTS FROM "/*" TO "*/"





IGNORE control

PRODUCTIONS
  ParvaDoc
  = {   FuncOrVarDeclarations
      | ConstDeclarations
    } .

  FuncOrVarDeclarations
  = Type Ident
    (   Function
      | GlobalVars ) .

  Function
  = "("
    FormalParameters
    ")"
    Block .

  GlobalVars
  = OneGlobal
    { "," Ident
      OneGlobal
    } ";" .

  OneGlobal
  = [ "=" Expression
    ] .

  FormalParameters
  = [ OneParam
      { "," OneParam
      }
    ] .

  OneParam
  = Type
    Ident .

  Block
  = "{"
    { Statement
    }
    "}" .

  Statement
  =   Block
    | AssignmentOrCall
    | ";"
    | ConstDeclarations
    | VarDeclarations
    | IfStatement
    | WhileStatement
    | HaltStatement
    | ReturnStatement
    | ReadStatement
    | WriteStatement .

  ConstDeclarations
  = "const" OneConst
    { ","
      OneConst
    } ";" .

  OneConst
  = Ident
    "="
    Constant .

  Constant
  =   IntConst
    | CharConst
    | "true"
    | "false"
    | "null" .

  VarDeclarations
  = Type
    OneVar
    { "," OneVar
    } ";" .

  OneVar
  = Ident
    [ "=" Expression
    ] .

  AssignmentOrCall
  = Designator
    (   "=" Expression
      | "(" Arguments
        ")"
    ) ";" .

  Designator
  = Ident
    [ "[" Expression
      "]"
    ] .

  Arguments
  = [ OneArg
      { "," OneArg
      }
    ] .

  OneArg
  = Expression .

  IfStatement
  = "if" "(" Condition
    ")" Statement .

  WhileStatement
  = "while" "(" Condition
    ")" Statement .

  ReturnStatement
  = "return"
    [ Expression
    ] ";" .

  HaltStatement
  = "halt" ";" .

  ReadStatement
  = "read" "(" ReadElement
    { "," ReadElement
    }
    ")" ";" .

  ReadElement
  =   StringConst
    | Designator .

  WriteStatement
  = "write" "(" WriteElement
    { "," WriteElement
    }
    ")" ";" .

  WriteElement
  =   StringConst
    | Expression .

  Condition
  = Expression .

  Expression
  = AddExp
    [ RelOp
      AddExp
    ] .

  AddExp
  = [ "+" | "-" ] Term
    { AddOp
      Term
    } .

  Term
  = Factor
    { MulOp
      Factor
    } .

  Factor
  =   Designator
      [ "(" Arguments
        ")"
      ]
    | Constant
    | "new" BasicType "[" Expression "]"
    | "!" Factor
    | "(" Expression ")" .

  Type
  =   "void"
    | BasicType
      [ "[]"
      ] .

  BasicType
  =   "int"
    | "bool" .

  AddOp
  = "+" | "-" | "||" .

  MulOp
  = "*" | "/" | "%" | "&&" .

  RelOp
  = "==" | "!=" | "<" | "<=" | ">" | ">=" .

  Ident
  = identifier .

  StringConst
  = stringLit .

  CharConst
  = charLit .

  IntConst
  = number .

END ParvaDoc.


/*------------------------------------------------------------------------- XXX.java -- the generic Compiler Driver Compiler Generator Coco/R, Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz extended by M. Loeberbauer & A. Woess, Univ. of Linz ported from C# to Java by Wolfgang Ahorner extended (June 2004) by Pat Terry, Rhodes University (p.terry@ru.ac.za) (changes marked as "pdt") This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. As an exception, it is allowed to write an extension of Coco/R that is used as a plugin in non-free software. If not otherwise stated, any source code generated by Coco/R (other than Coco/R itself) does not fall under the GNU General Public License. -------------------------------------------------------------------------*/ -->begin /* Generic driver frame file for Coco/R for Java PDT October 2005 - for CSC 301 pracs Modify this to suit your own purposes - hints are given below! */ // ----------------------- you may need to change the "import" clauses: import Library.*; import java.io.*; public class -->class { public static void main (String[] args) { boolean mergeErrors = false; String inputName = null; // ------------------------ you may need to process command line parameters: for (int i = 0; i < args.length; i++) { if (args[i].toLowerCase().equals("-l")) mergeErrors = true; else inputName = args[i]; } if (inputName == null) { System.err.println("No input file specified"); System.exit(1); } int pos = inputName.lastIndexOf('/'); if (pos < 0) pos = inputName.lastIndexOf('\\'); String dir = inputName.substring(0, pos+1); /*++++ If the parser needs an output file, include a section like the following and add a line public static OutFile output; to your ATG file. String outputName = null; pos = inputName.lastIndexOf('.'); if (pos < 0) outputName = inputName + ".out"; else outputName = inputName.substring(0, pos) + ".out"; Parser.output = new OutFile(outputName); if (Parser.output.openError()) { System.err.println("cannot open " + outputName); System.exit(1); } ++++++ */ Scanner.Init(inputName); Errors.Init(inputName, dir, mergeErrors); // ----------------------- add other initialization if required: Parser.Parse(); Errors.Summarize(); // ----------------------- add other finalization if required: /*++++ If the parser needs an output file, uncomment this section Parser.output.close(); ++++++ */ } } // end driver $$$