#
#  File: agfl.k3
#  Defines the root module of the agfl parser generator.
#
#  Copyright 2005-2006 Radboud University of Nijmegen
#
#  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 of the License, 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 Library 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.
#
#  CVS ID: "$Id: agfl.k3,v 1.55 2006/08/09 11:13:08 marcs Exp $"
#
ROOT agfl.

USES affix, set, rule, io, id, code, globals, options, optimize,
     properties, parser, modules, transduction, intermediate,
     icoptimize, textstorage.

ACTION agfl:
  parse command line,
    open output files,
    show version,
    read grammar modules,
    abort if errors found,
    analyse grammar,
    transform grammar,
    calculate properties,
    optionally generate lexicon interface file,
    generate intermediate code,
    conditional directors,
    optimize intermediate code,
    abort if errors found,
    generate code for grammar,
    conditional logging,
    maybe show memory usage,
    close output files.

ACTION show version:
  get agfl version (TEXT),
    write ("RU AGFL system, version " + TEXT + "\n").

#
# TODO: call 'create predefined ids' from 'init id'. Now they are created
# before reading the lexicon, in order to avoid numbering conflicts with
# the lexicon system.
#
# "create start nonterminal" is created after reading the lexicon so the
# first lexicon nonterminal (in the .lif file) has nr 0.
#
ACTION read grammar modules:
  create predefined ids,
    create start nonterminal,
    parse grammar,
    display time ("parsed grammar").

# collect specs before affix analysis before syntax analysis !!

ACTION analyse grammar:
  message ("checking grammar..."),
    collect specifications,
    display time ("collect specifications"),
    analyse affix rules,
    warn for hybrid ids,
    display time ("warned for hybrid ids"),
    analyse syntax rules,
    display time ("syntax rules analysed"),
    abort if errors found.

#
# Note: free word order should not be implemented
# by expanding all possible alternatives
#
ACTION transform grammar:
  expand standard transductions,
    display time ("expanded standard transduction"),
    transform free word order members to final groups,
    display time ("transformed free word order members"),
    expand final group transductions,
    display time ("expanded final group transductions").

#
# Property analysis:
#
# reachability: prevent generation of dead code
# nonfalseness: needed for director sets and left recursion analysis
# predicates: no memo/directors, may be treacherous left recursive
# first sets: needed director sets and left recursion analysis
# follow sets: needed for director sets
# last sets: nice to have
# left recursion: needs to be removed
# left recursive paths: nice to have
# penalty analysis: penalties interfere with memos
# string generation: non-generating rules don't terminate
#
ACTION calculate properties:
  message ("calculating properties..."),
    analyse reachability,
    display time ("reachable nonterminals calculated"),
    analyse nonfalseness,
    display time ("nonfalse checks done"),
    analyse predicates,
    display time ("predicate rules calculated"),
    calculate first sets,
    display time ("first sets calculated"),
    conditional last sets,
    conditional follow sets,
    analyse left recursion,
    display time ("left recursive rules calculated"),
    conditional wellformedness analysis,
    conditional penalty analysis,
    abort if errors found,
    get log file (TEXT),
    message ("logging properties... (" + TEXT + ")"),
    log grammar statistics,
    log properties,
    display time ("logged properties").

ACTION conditional wellformedness analysis:
  if no analysis option;
  analyse cyclicity,
    display time ("analysed cyclicity"),
    analyse string generation,
    display time ("generating nonterminals calculated"),
    check for conflicting rule types,
    display time ("checked for conflicting rule types"),
    check for commit in rules and options,
    display time ("checked for commit in rules and options").

ACTION conditional last sets:
  if full analysis option, last sets;
  +.

ACTION conditional follow sets:
  if full analysis option, follow sets;
  if directors option, follow sets;
  +.

ACTION conditional penalty analysis:
  if analyse penalties,
    analyse penalties,
    display time ("analysed penalties");
  +.

TEST if analyse penalties:
  if negative memo option;
  if full analysis option.

ACTION last sets:
  calculate last sets,
    display time ("last sets calculated").

ACTION follow sets:
  calculate follow sets,
    display time ("follow sets calculated").

ACTION optionally factorize grammar:
  if factorize option,
    message ("optimizing (left-factorization)..."),
    left factorize grammar,
    display time ("left-factored grammar");
  +.

ACTION conditional directors:
  if directors option,
    initialize directors with memos,
    display time ("initialized directors with memos");
  +.

ACTION conditional logging:
  if log option,
    log affix rules,
    log hash stats,
    dump ids,
    (if directors option,
       log memo directors,
       display time ("logged memo directors");
     +);
  +.

ACTION generate code for grammar:
  message ("coding..."),
  generate code.

ACTION optionally generate lexicon interface file:
  if lexicon used,
    open lexicon interface file,
    write lexicon modules to lif,
    write affixes to lif,
    write lexicon rules to lif,
    close lexicon interface file;
  +.

ACTION maybe show memory usage:
  if verbose option,
    show memory usage;
  +.

EXTERNAL ACTION show memory usage:301
