ba2eae5d52
- simulator source now in 'src' subdirectory - imported files from 'ext' repository - support building in arbitrary places, including outside of the source tree. See comment at top of SConstruct file for more details. Regression tests are temporarily disabled; that syetem needs more extensive revisions. SConstruct: Update for new directory structure. Modify to support build trees that are not subdirectories of the source tree. See comment at top of file for more details. Regression tests are temporarily disabled. src/arch/SConscript: src/arch/isa_parser.py: src/python/SConscript: Update for new directory structure. --HG-- rename : build/SConstruct => SConstruct rename : build/default_options/ALPHA_FS => build_opts/ALPHA_FS rename : build/default_options/ALPHA_FS_TL => build_opts/ALPHA_FS_TL rename : build/default_options/ALPHA_SE => build_opts/ALPHA_SE rename : build/default_options/MIPS_SE => build_opts/MIPS_SE rename : build/default_options/SPARC_SE => build_opts/SPARC_SE rename : Doxyfile => src/Doxyfile rename : SConscript => src/SConscript rename : arch/SConscript => src/arch/SConscript rename : arch/alpha/SConscript => src/arch/alpha/SConscript rename : arch/alpha/aout_machdep.h => src/arch/alpha/aout_machdep.h rename : arch/alpha/arguments.cc => src/arch/alpha/arguments.cc rename : arch/alpha/arguments.hh => src/arch/alpha/arguments.hh rename : arch/alpha/ecoff_machdep.h => src/arch/alpha/ecoff_machdep.h rename : arch/alpha/ev5.cc => src/arch/alpha/ev5.cc rename : arch/alpha/ev5.hh => src/arch/alpha/ev5.hh rename : arch/alpha/faults.cc => src/arch/alpha/faults.cc rename : arch/alpha/faults.hh => src/arch/alpha/faults.hh rename : arch/alpha/freebsd/system.cc => src/arch/alpha/freebsd/system.cc rename : arch/alpha/freebsd/system.hh => src/arch/alpha/freebsd/system.hh rename : arch/alpha/isa/branch.isa => src/arch/alpha/isa/branch.isa rename : arch/alpha/isa/decoder.isa => src/arch/alpha/isa/decoder.isa rename : arch/alpha/isa/fp.isa => src/arch/alpha/isa/fp.isa rename : arch/alpha/isa/int.isa => src/arch/alpha/isa/int.isa rename : arch/alpha/isa/main.isa => src/arch/alpha/isa/main.isa rename : arch/alpha/isa/mem.isa => src/arch/alpha/isa/mem.isa rename : arch/alpha/isa/opcdec.isa => src/arch/alpha/isa/opcdec.isa rename : arch/alpha/isa/pal.isa => src/arch/alpha/isa/pal.isa rename : arch/alpha/isa/unimp.isa => src/arch/alpha/isa/unimp.isa rename : arch/alpha/isa/unknown.isa => src/arch/alpha/isa/unknown.isa rename : arch/alpha/isa/util.isa => src/arch/alpha/isa/util.isa rename : arch/alpha/isa_traits.hh => src/arch/alpha/isa_traits.hh rename : arch/alpha/linux/aligned.hh => src/arch/alpha/linux/aligned.hh rename : arch/alpha/linux/hwrpb.hh => src/arch/alpha/linux/hwrpb.hh rename : arch/alpha/linux/linux.cc => src/arch/alpha/linux/linux.cc rename : arch/alpha/linux/linux.hh => src/arch/alpha/linux/linux.hh rename : arch/alpha/linux/process.cc => src/arch/alpha/linux/process.cc rename : arch/alpha/linux/process.hh => src/arch/alpha/linux/process.hh rename : arch/alpha/linux/system.cc => src/arch/alpha/linux/system.cc rename : arch/alpha/linux/system.hh => src/arch/alpha/linux/system.hh rename : arch/alpha/linux/thread_info.hh => src/arch/alpha/linux/thread_info.hh rename : arch/alpha/linux/threadinfo.hh => src/arch/alpha/linux/threadinfo.hh rename : arch/alpha/osfpal.cc => src/arch/alpha/osfpal.cc rename : arch/alpha/osfpal.hh => src/arch/alpha/osfpal.hh rename : arch/alpha/process.cc => src/arch/alpha/process.cc rename : arch/alpha/process.hh => src/arch/alpha/process.hh rename : arch/alpha/regfile.hh => src/arch/alpha/regfile.hh rename : arch/alpha/stacktrace.cc => src/arch/alpha/stacktrace.cc rename : arch/alpha/stacktrace.hh => src/arch/alpha/stacktrace.hh rename : arch/alpha/system.cc => src/arch/alpha/system.cc rename : arch/alpha/system.hh => src/arch/alpha/system.hh rename : arch/alpha/tlb.cc => src/arch/alpha/tlb.cc rename : arch/alpha/tlb.hh => src/arch/alpha/tlb.hh rename : arch/alpha/tru64/process.cc => src/arch/alpha/tru64/process.cc rename : arch/alpha/tru64/process.hh => src/arch/alpha/tru64/process.hh rename : arch/alpha/tru64/system.cc => src/arch/alpha/tru64/system.cc rename : arch/alpha/tru64/system.hh => src/arch/alpha/tru64/system.hh rename : arch/alpha/tru64/tru64.cc => src/arch/alpha/tru64/tru64.cc rename : arch/alpha/tru64/tru64.hh => src/arch/alpha/tru64/tru64.hh rename : arch/alpha/types.hh => src/arch/alpha/types.hh rename : arch/alpha/utility.hh => src/arch/alpha/utility.hh rename : arch/alpha/vtophys.cc => src/arch/alpha/vtophys.cc rename : arch/alpha/vtophys.hh => src/arch/alpha/vtophys.hh rename : arch/isa_parser.py => src/arch/isa_parser.py rename : arch/isa_specific.hh => src/arch/isa_specific.hh rename : arch/mips/SConscript => src/arch/mips/SConscript rename : arch/mips/faults.cc => src/arch/mips/faults.cc rename : arch/mips/faults.hh => src/arch/mips/faults.hh rename : arch/mips/isa/base.isa => src/arch/mips/isa/base.isa rename : arch/mips/isa/bitfields.isa => src/arch/mips/isa/bitfields.isa rename : arch/mips/isa/decoder.isa => src/arch/mips/isa/decoder.isa rename : arch/mips/isa/formats/basic.isa => src/arch/mips/isa/formats/basic.isa rename : arch/mips/isa/formats/branch.isa => src/arch/mips/isa/formats/branch.isa rename : arch/mips/isa/formats/formats.isa => src/arch/mips/isa/formats/formats.isa rename : arch/mips/isa/formats/fp.isa => src/arch/mips/isa/formats/fp.isa rename : arch/mips/isa/formats/int.isa => src/arch/mips/isa/formats/int.isa rename : arch/mips/isa/formats/mem.isa => src/arch/mips/isa/formats/mem.isa rename : arch/mips/isa/formats/noop.isa => src/arch/mips/isa/formats/noop.isa rename : arch/mips/isa/formats/tlbop.isa => src/arch/mips/isa/formats/tlbop.isa rename : arch/mips/isa/formats/trap.isa => src/arch/mips/isa/formats/trap.isa rename : arch/mips/isa/formats/unimp.isa => src/arch/mips/isa/formats/unimp.isa rename : arch/mips/isa/formats/unknown.isa => src/arch/mips/isa/formats/unknown.isa rename : arch/mips/isa/formats/util.isa => src/arch/mips/isa/formats/util.isa rename : arch/mips/isa/includes.isa => src/arch/mips/isa/includes.isa rename : arch/mips/isa/main.isa => src/arch/mips/isa/main.isa rename : arch/mips/isa/operands.isa => src/arch/mips/isa/operands.isa rename : arch/mips/isa_traits.cc => src/arch/mips/isa_traits.cc rename : arch/mips/isa_traits.hh => src/arch/mips/isa_traits.hh rename : arch/mips/linux/linux.cc => src/arch/mips/linux/linux.cc rename : arch/mips/linux/linux.hh => src/arch/mips/linux/linux.hh rename : arch/mips/linux/process.cc => src/arch/mips/linux/process.cc rename : arch/mips/linux/process.hh => src/arch/mips/linux/process.hh rename : arch/mips/process.cc => src/arch/mips/process.cc rename : arch/mips/process.hh => src/arch/mips/process.hh rename : arch/mips/regfile/float_regfile.hh => src/arch/mips/regfile/float_regfile.hh rename : arch/mips/regfile/int_regfile.hh => src/arch/mips/regfile/int_regfile.hh rename : arch/mips/regfile/misc_regfile.hh => src/arch/mips/regfile/misc_regfile.hh rename : arch/mips/regfile/regfile.hh => src/arch/mips/regfile/regfile.hh rename : arch/mips/stacktrace.hh => src/arch/mips/stacktrace.hh rename : arch/mips/types.hh => src/arch/mips/types.hh rename : arch/mips/utility.hh => src/arch/mips/utility.hh rename : arch/sparc/SConscript => src/arch/sparc/SConscript rename : arch/sparc/faults.cc => src/arch/sparc/faults.cc rename : arch/sparc/faults.hh => src/arch/sparc/faults.hh rename : arch/sparc/isa/base.isa => src/arch/sparc/isa/base.isa rename : arch/sparc/isa/bitfields.isa => src/arch/sparc/isa/bitfields.isa rename : arch/sparc/isa/decoder.isa => src/arch/sparc/isa/decoder.isa rename : arch/sparc/isa/formats.isa => src/arch/sparc/isa/formats.isa rename : arch/sparc/isa/formats/basic.isa => src/arch/sparc/isa/formats/basic.isa rename : arch/sparc/isa/formats/branch.isa => src/arch/sparc/isa/formats/branch.isa rename : arch/sparc/isa/formats/integerop.isa => src/arch/sparc/isa/formats/integerop.isa rename : arch/sparc/isa/formats/mem.isa => src/arch/sparc/isa/formats/mem.isa rename : arch/sparc/isa/formats/nop.isa => src/arch/sparc/isa/formats/nop.isa rename : arch/sparc/isa/formats/priv.isa => src/arch/sparc/isa/formats/priv.isa rename : arch/sparc/isa/formats/trap.isa => src/arch/sparc/isa/formats/trap.isa rename : arch/sparc/isa/formats/unknown.isa => src/arch/sparc/isa/formats/unknown.isa rename : arch/sparc/isa/includes.isa => src/arch/sparc/isa/includes.isa rename : arch/sparc/isa/main.isa => src/arch/sparc/isa/main.isa rename : arch/sparc/isa/operands.isa => src/arch/sparc/isa/operands.isa rename : arch/sparc/isa_traits.hh => src/arch/sparc/isa_traits.hh rename : arch/sparc/linux/linux.cc => src/arch/sparc/linux/linux.cc rename : arch/sparc/linux/linux.hh => src/arch/sparc/linux/linux.hh rename : arch/sparc/linux/process.cc => src/arch/sparc/linux/process.cc rename : arch/sparc/linux/process.hh => src/arch/sparc/linux/process.hh rename : arch/sparc/process.cc => src/arch/sparc/process.cc rename : arch/sparc/process.hh => src/arch/sparc/process.hh rename : arch/sparc/regfile.hh => src/arch/sparc/regfile.hh rename : arch/sparc/solaris/process.cc => src/arch/sparc/solaris/process.cc rename : arch/sparc/solaris/process.hh => src/arch/sparc/solaris/process.hh rename : arch/sparc/solaris/solaris.cc => src/arch/sparc/solaris/solaris.cc rename : arch/sparc/solaris/solaris.hh => src/arch/sparc/solaris/solaris.hh rename : arch/sparc/stacktrace.hh => src/arch/sparc/stacktrace.hh rename : arch/sparc/system.cc => src/arch/sparc/system.cc rename : arch/sparc/system.hh => src/arch/sparc/system.hh rename : arch/sparc/utility.hh => src/arch/sparc/utility.hh rename : base/bitfield.hh => src/base/bitfield.hh rename : base/callback.hh => src/base/callback.hh rename : base/chunk_generator.hh => src/base/chunk_generator.hh rename : base/circlebuf.cc => src/base/circlebuf.cc rename : base/circlebuf.hh => src/base/circlebuf.hh rename : base/compression/lzss_compression.cc => src/base/compression/lzss_compression.cc rename : base/compression/lzss_compression.hh => src/base/compression/lzss_compression.hh rename : base/compression/null_compression.hh => src/base/compression/null_compression.hh rename : base/cprintf.cc => src/base/cprintf.cc rename : base/cprintf.hh => src/base/cprintf.hh rename : base/cprintf_formats.hh => src/base/cprintf_formats.hh rename : base/crc.cc => src/base/crc.cc rename : base/crc.hh => src/base/crc.hh rename : base/date.cc => src/base/date.cc rename : base/dbl_list.hh => src/base/dbl_list.hh rename : base/endian.hh => src/base/endian.hh rename : base/fast_alloc.cc => src/base/fast_alloc.cc rename : base/fast_alloc.hh => src/base/fast_alloc.hh rename : base/fenv.hh => src/base/fenv.hh rename : base/fifo_buffer.cc => src/base/fifo_buffer.cc rename : base/fifo_buffer.hh => src/base/fifo_buffer.hh rename : base/hashmap.hh => src/base/hashmap.hh rename : base/hostinfo.cc => src/base/hostinfo.cc rename : base/hostinfo.hh => src/base/hostinfo.hh rename : base/hybrid_pred.cc => src/base/hybrid_pred.cc rename : base/hybrid_pred.hh => src/base/hybrid_pred.hh rename : base/inet.cc => src/base/inet.cc rename : base/inet.hh => src/base/inet.hh rename : base/inifile.cc => src/base/inifile.cc rename : base/inifile.hh => src/base/inifile.hh rename : base/intmath.cc => src/base/intmath.cc rename : base/intmath.hh => src/base/intmath.hh rename : base/kgdb.h => src/base/kgdb.h rename : base/loader/aout_object.cc => src/base/loader/aout_object.cc rename : base/loader/aout_object.hh => src/base/loader/aout_object.hh rename : base/loader/coff_sym.h => src/base/loader/coff_sym.h rename : base/loader/coff_symconst.h => src/base/loader/coff_symconst.h rename : base/loader/ecoff_object.cc => src/base/loader/ecoff_object.cc rename : base/loader/ecoff_object.hh => src/base/loader/ecoff_object.hh rename : base/loader/elf_object.cc => src/base/loader/elf_object.cc rename : base/loader/elf_object.hh => src/base/loader/elf_object.hh rename : base/loader/exec_aout.h => src/base/loader/exec_aout.h rename : base/loader/exec_ecoff.h => src/base/loader/exec_ecoff.h rename : base/loader/object_file.cc => src/base/loader/object_file.cc rename : base/loader/object_file.hh => src/base/loader/object_file.hh rename : base/loader/symtab.cc => src/base/loader/symtab.cc rename : base/loader/symtab.hh => src/base/loader/symtab.hh rename : base/match.cc => src/base/match.cc rename : base/match.hh => src/base/match.hh rename : base/misc.cc => src/base/misc.cc rename : base/misc.hh => src/base/misc.hh rename : base/mod_num.hh => src/base/mod_num.hh rename : base/mysql.cc => src/base/mysql.cc rename : base/mysql.hh => src/base/mysql.hh rename : base/output.cc => src/base/output.cc rename : base/output.hh => src/base/output.hh rename : base/pollevent.cc => src/base/pollevent.cc rename : base/pollevent.hh => src/base/pollevent.hh rename : base/predictor.hh => src/base/predictor.hh rename : base/random.cc => src/base/random.cc rename : base/random.hh => src/base/random.hh rename : base/range.cc => src/base/range.cc rename : base/range.hh => src/base/range.hh rename : base/refcnt.hh => src/base/refcnt.hh rename : base/remote_gdb.cc => src/base/remote_gdb.cc rename : base/remote_gdb.hh => src/base/remote_gdb.hh rename : base/res_list.hh => src/base/res_list.hh rename : base/sat_counter.cc => src/base/sat_counter.cc rename : base/sat_counter.hh => src/base/sat_counter.hh rename : base/sched_list.hh => src/base/sched_list.hh rename : base/socket.cc => src/base/socket.cc rename : base/socket.hh => src/base/socket.hh rename : base/statistics.cc => src/base/statistics.cc rename : base/statistics.hh => src/base/statistics.hh rename : base/stats/events.cc => src/base/stats/events.cc rename : base/stats/events.hh => src/base/stats/events.hh rename : base/stats/flags.hh => src/base/stats/flags.hh rename : base/stats/mysql.cc => src/base/stats/mysql.cc rename : base/stats/mysql.hh => src/base/stats/mysql.hh rename : base/stats/mysql_run.hh => src/base/stats/mysql_run.hh rename : base/stats/output.hh => src/base/stats/output.hh rename : base/stats/statdb.cc => src/base/stats/statdb.cc rename : base/stats/statdb.hh => src/base/stats/statdb.hh rename : base/stats/text.cc => src/base/stats/text.cc rename : base/stats/text.hh => src/base/stats/text.hh rename : base/stats/types.hh => src/base/stats/types.hh rename : base/stats/visit.cc => src/base/stats/visit.cc rename : base/stats/visit.hh => src/base/stats/visit.hh rename : base/str.cc => src/base/str.cc rename : base/str.hh => src/base/str.hh rename : base/time.cc => src/base/time.cc rename : base/time.hh => src/base/time.hh rename : base/timebuf.hh => src/base/timebuf.hh rename : base/trace.cc => src/base/trace.cc rename : base/trace.hh => src/base/trace.hh rename : base/traceflags.py => src/base/traceflags.py rename : base/userinfo.cc => src/base/userinfo.cc rename : base/userinfo.hh => src/base/userinfo.hh rename : cpu/SConscript => src/cpu/SConscript rename : cpu/base.cc => src/cpu/base.cc rename : cpu/base.hh => src/cpu/base.hh rename : cpu/base_dyn_inst.cc => src/cpu/base_dyn_inst.cc rename : cpu/base_dyn_inst.hh => src/cpu/base_dyn_inst.hh rename : cpu/cpu_exec_context.cc => src/cpu/cpu_exec_context.cc rename : cpu/cpu_exec_context.hh => src/cpu/cpu_exec_context.hh rename : cpu/cpu_models.py => src/cpu/cpu_models.py rename : cpu/exec_context.hh => src/cpu/exec_context.hh rename : cpu/exetrace.cc => src/cpu/exetrace.cc rename : cpu/exetrace.hh => src/cpu/exetrace.hh rename : cpu/inst_seq.hh => src/cpu/inst_seq.hh rename : cpu/intr_control.cc => src/cpu/intr_control.cc rename : cpu/intr_control.hh => src/cpu/intr_control.hh rename : cpu/memtest/memtest.cc => src/cpu/memtest/memtest.cc rename : cpu/memtest/memtest.hh => src/cpu/memtest/memtest.hh rename : cpu/o3/2bit_local_pred.cc => src/cpu/o3/2bit_local_pred.cc rename : cpu/o3/2bit_local_pred.hh => src/cpu/o3/2bit_local_pred.hh rename : cpu/o3/alpha_cpu.cc => src/cpu/o3/alpha_cpu.cc rename : cpu/o3/alpha_cpu.hh => src/cpu/o3/alpha_cpu.hh rename : cpu/o3/alpha_cpu_builder.cc => src/cpu/o3/alpha_cpu_builder.cc rename : cpu/o3/alpha_cpu_impl.hh => src/cpu/o3/alpha_cpu_impl.hh rename : cpu/o3/alpha_dyn_inst.cc => src/cpu/o3/alpha_dyn_inst.cc rename : cpu/o3/alpha_dyn_inst.hh => src/cpu/o3/alpha_dyn_inst.hh rename : cpu/o3/alpha_dyn_inst_impl.hh => src/cpu/o3/alpha_dyn_inst_impl.hh rename : cpu/o3/alpha_impl.hh => src/cpu/o3/alpha_impl.hh rename : cpu/o3/alpha_params.hh => src/cpu/o3/alpha_params.hh rename : cpu/o3/bpred_unit.cc => src/cpu/o3/bpred_unit.cc rename : cpu/o3/bpred_unit.hh => src/cpu/o3/bpred_unit.hh rename : cpu/o3/bpred_unit_impl.hh => src/cpu/o3/bpred_unit_impl.hh rename : cpu/o3/btb.cc => src/cpu/o3/btb.cc rename : cpu/o3/btb.hh => src/cpu/o3/btb.hh rename : cpu/o3/comm.hh => src/cpu/o3/comm.hh rename : cpu/o3/commit.cc => src/cpu/o3/commit.cc rename : cpu/o3/commit.hh => src/cpu/o3/commit.hh rename : cpu/o3/commit_impl.hh => src/cpu/o3/commit_impl.hh rename : cpu/o3/cpu.cc => src/cpu/o3/cpu.cc rename : cpu/o3/cpu.hh => src/cpu/o3/cpu.hh rename : cpu/o3/cpu_policy.hh => src/cpu/o3/cpu_policy.hh rename : cpu/o3/decode.cc => src/cpu/o3/decode.cc rename : cpu/o3/decode.hh => src/cpu/o3/decode.hh rename : cpu/o3/decode_impl.hh => src/cpu/o3/decode_impl.hh rename : cpu/o3/fetch.cc => src/cpu/o3/fetch.cc rename : cpu/o3/fetch.hh => src/cpu/o3/fetch.hh rename : cpu/o3/fetch_impl.hh => src/cpu/o3/fetch_impl.hh rename : cpu/o3/free_list.cc => src/cpu/o3/free_list.cc rename : cpu/o3/free_list.hh => src/cpu/o3/free_list.hh rename : cpu/o3/iew.cc => src/cpu/o3/iew.cc rename : cpu/o3/iew.hh => src/cpu/o3/iew.hh rename : cpu/o3/iew_impl.hh => src/cpu/o3/iew_impl.hh rename : cpu/o3/inst_queue.cc => src/cpu/o3/inst_queue.cc rename : cpu/o3/inst_queue.hh => src/cpu/o3/inst_queue.hh rename : cpu/o3/inst_queue_impl.hh => src/cpu/o3/inst_queue_impl.hh rename : cpu/o3/mem_dep_unit.cc => src/cpu/o3/mem_dep_unit.cc rename : cpu/o3/mem_dep_unit.hh => src/cpu/o3/mem_dep_unit.hh rename : cpu/o3/mem_dep_unit_impl.hh => src/cpu/o3/mem_dep_unit_impl.hh rename : cpu/o3/ras.cc => src/cpu/o3/ras.cc rename : cpu/o3/ras.hh => src/cpu/o3/ras.hh rename : cpu/o3/regfile.hh => src/cpu/o3/regfile.hh rename : cpu/o3/rename.cc => src/cpu/o3/rename.cc rename : cpu/o3/rename.hh => src/cpu/o3/rename.hh rename : cpu/o3/rename_impl.hh => src/cpu/o3/rename_impl.hh rename : cpu/o3/rename_map.cc => src/cpu/o3/rename_map.cc rename : cpu/o3/rename_map.hh => src/cpu/o3/rename_map.hh rename : cpu/o3/rob.cc => src/cpu/o3/rob.cc rename : cpu/o3/rob.hh => src/cpu/o3/rob.hh rename : cpu/o3/rob_impl.hh => src/cpu/o3/rob_impl.hh rename : cpu/o3/sat_counter.cc => src/cpu/o3/sat_counter.cc rename : cpu/o3/sat_counter.hh => src/cpu/o3/sat_counter.hh rename : cpu/o3/store_set.cc => src/cpu/o3/store_set.cc rename : cpu/o3/store_set.hh => src/cpu/o3/store_set.hh rename : cpu/o3/tournament_pred.cc => src/cpu/o3/tournament_pred.cc rename : cpu/o3/tournament_pred.hh => src/cpu/o3/tournament_pred.hh rename : cpu/op_class.cc => src/cpu/op_class.cc rename : cpu/op_class.hh => src/cpu/op_class.hh rename : cpu/ozone/cpu.cc => src/cpu/ozone/cpu.cc rename : cpu/ozone/cpu.hh => src/cpu/ozone/cpu.hh rename : cpu/ozone/cpu_impl.hh => src/cpu/ozone/cpu_impl.hh rename : cpu/ozone/ea_list.cc => src/cpu/ozone/ea_list.cc rename : cpu/ozone/ea_list.hh => src/cpu/ozone/ea_list.hh rename : cpu/pc_event.cc => src/cpu/pc_event.cc rename : cpu/pc_event.hh => src/cpu/pc_event.hh rename : cpu/profile.cc => src/cpu/profile.cc rename : cpu/profile.hh => src/cpu/profile.hh rename : cpu/simple/atomic.cc => src/cpu/simple/atomic.cc rename : cpu/simple/atomic.hh => src/cpu/simple/atomic.hh rename : cpu/simple/base.cc => src/cpu/simple/base.cc rename : cpu/simple/base.hh => src/cpu/simple/base.hh rename : cpu/simple/timing.cc => src/cpu/simple/timing.cc rename : cpu/simple/timing.hh => src/cpu/simple/timing.hh rename : cpu/smt.hh => src/cpu/smt.hh rename : cpu/static_inst.cc => src/cpu/static_inst.cc rename : cpu/static_inst.hh => src/cpu/static_inst.hh rename : cpu/trace/opt_cpu.cc => src/cpu/trace/opt_cpu.cc rename : cpu/trace/opt_cpu.hh => src/cpu/trace/opt_cpu.hh rename : cpu/trace/reader/ibm_reader.cc => src/cpu/trace/reader/ibm_reader.cc rename : cpu/trace/reader/ibm_reader.hh => src/cpu/trace/reader/ibm_reader.hh rename : cpu/trace/reader/itx_reader.cc => src/cpu/trace/reader/itx_reader.cc rename : cpu/trace/reader/itx_reader.hh => src/cpu/trace/reader/itx_reader.hh rename : cpu/trace/reader/m5_reader.cc => src/cpu/trace/reader/m5_reader.cc rename : cpu/trace/reader/m5_reader.hh => src/cpu/trace/reader/m5_reader.hh rename : cpu/trace/reader/mem_trace_reader.cc => src/cpu/trace/reader/mem_trace_reader.cc rename : cpu/trace/reader/mem_trace_reader.hh => src/cpu/trace/reader/mem_trace_reader.hh rename : cpu/trace/trace_cpu.cc => src/cpu/trace/trace_cpu.cc rename : cpu/trace/trace_cpu.hh => src/cpu/trace/trace_cpu.hh rename : dev/alpha_access.h => src/dev/alpha_access.h rename : dev/alpha_console.cc => src/dev/alpha_console.cc rename : dev/alpha_console.hh => src/dev/alpha_console.hh rename : dev/baddev.cc => src/dev/baddev.cc rename : dev/baddev.hh => src/dev/baddev.hh rename : dev/disk_image.cc => src/dev/disk_image.cc rename : dev/disk_image.hh => src/dev/disk_image.hh rename : dev/etherbus.cc => src/dev/etherbus.cc rename : dev/etherbus.hh => src/dev/etherbus.hh rename : dev/etherdump.cc => src/dev/etherdump.cc rename : dev/etherdump.hh => src/dev/etherdump.hh rename : dev/etherint.cc => src/dev/etherint.cc rename : dev/etherint.hh => src/dev/etherint.hh rename : dev/etherlink.cc => src/dev/etherlink.cc rename : dev/etherlink.hh => src/dev/etherlink.hh rename : dev/etherpkt.cc => src/dev/etherpkt.cc rename : dev/etherpkt.hh => src/dev/etherpkt.hh rename : dev/ethertap.cc => src/dev/ethertap.cc rename : dev/ethertap.hh => src/dev/ethertap.hh rename : dev/ide_atareg.h => src/dev/ide_atareg.h rename : dev/ide_ctrl.cc => src/dev/ide_ctrl.cc rename : dev/ide_ctrl.hh => src/dev/ide_ctrl.hh rename : dev/ide_disk.cc => src/dev/ide_disk.cc rename : dev/ide_disk.hh => src/dev/ide_disk.hh rename : dev/ide_wdcreg.h => src/dev/ide_wdcreg.h rename : dev/io_device.cc => src/dev/io_device.cc rename : dev/io_device.hh => src/dev/io_device.hh rename : dev/isa_fake.cc => src/dev/isa_fake.cc rename : dev/isa_fake.hh => src/dev/isa_fake.hh rename : dev/ns_gige.cc => src/dev/ns_gige.cc rename : dev/ns_gige.hh => src/dev/ns_gige.hh rename : dev/ns_gige_reg.h => src/dev/ns_gige_reg.h rename : dev/pciconfigall.cc => src/dev/pciconfigall.cc rename : dev/pciconfigall.hh => src/dev/pciconfigall.hh rename : dev/pcidev.cc => src/dev/pcidev.cc rename : dev/pcidev.hh => src/dev/pcidev.hh rename : dev/pcireg.h => src/dev/pcireg.h rename : dev/pitreg.h => src/dev/pitreg.h rename : dev/pktfifo.cc => src/dev/pktfifo.cc rename : dev/pktfifo.hh => src/dev/pktfifo.hh rename : dev/platform.cc => src/dev/platform.cc rename : dev/platform.hh => src/dev/platform.hh rename : dev/rtcreg.h => src/dev/rtcreg.h rename : dev/simconsole.cc => src/dev/simconsole.cc rename : dev/simconsole.hh => src/dev/simconsole.hh rename : dev/simple_disk.cc => src/dev/simple_disk.cc rename : dev/simple_disk.hh => src/dev/simple_disk.hh rename : dev/sinic.cc => src/dev/sinic.cc rename : dev/sinic.hh => src/dev/sinic.hh rename : dev/sinicreg.hh => src/dev/sinicreg.hh rename : dev/tsunami.cc => src/dev/tsunami.cc rename : dev/tsunami.hh => src/dev/tsunami.hh rename : dev/tsunami_cchip.cc => src/dev/tsunami_cchip.cc rename : dev/tsunami_cchip.hh => src/dev/tsunami_cchip.hh rename : dev/tsunami_io.cc => src/dev/tsunami_io.cc rename : dev/tsunami_io.hh => src/dev/tsunami_io.hh rename : dev/tsunami_pchip.cc => src/dev/tsunami_pchip.cc rename : dev/tsunami_pchip.hh => src/dev/tsunami_pchip.hh rename : dev/tsunamireg.h => src/dev/tsunamireg.h rename : dev/uart.cc => src/dev/uart.cc rename : dev/uart.hh => src/dev/uart.hh rename : dev/uart8250.cc => src/dev/uart8250.cc rename : dev/uart8250.hh => src/dev/uart8250.hh rename : kern/kernel_stats.cc => src/kern/kernel_stats.cc rename : kern/kernel_stats.hh => src/kern/kernel_stats.hh rename : kern/linux/events.cc => src/kern/linux/events.cc rename : kern/linux/events.hh => src/kern/linux/events.hh rename : kern/linux/linux.hh => src/kern/linux/linux.hh rename : kern/linux/linux_syscalls.cc => src/kern/linux/linux_syscalls.cc rename : kern/linux/linux_syscalls.hh => src/kern/linux/linux_syscalls.hh rename : kern/linux/printk.cc => src/kern/linux/printk.cc rename : kern/linux/printk.hh => src/kern/linux/printk.hh rename : kern/linux/sched.hh => src/kern/linux/sched.hh rename : kern/solaris/solaris.hh => src/kern/solaris/solaris.hh rename : kern/system_events.cc => src/kern/system_events.cc rename : kern/system_events.hh => src/kern/system_events.hh rename : kern/tru64/dump_mbuf.cc => src/kern/tru64/dump_mbuf.cc rename : kern/tru64/dump_mbuf.hh => src/kern/tru64/dump_mbuf.hh rename : kern/tru64/mbuf.hh => src/kern/tru64/mbuf.hh rename : kern/tru64/printf.cc => src/kern/tru64/printf.cc rename : kern/tru64/printf.hh => src/kern/tru64/printf.hh rename : kern/tru64/tru64.hh => src/kern/tru64/tru64.hh rename : kern/tru64/tru64_events.cc => src/kern/tru64/tru64_events.cc rename : kern/tru64/tru64_events.hh => src/kern/tru64/tru64_events.hh rename : kern/tru64/tru64_syscalls.cc => src/kern/tru64/tru64_syscalls.cc rename : kern/tru64/tru64_syscalls.hh => src/kern/tru64/tru64_syscalls.hh rename : mem/bridge.cc => src/mem/bridge.cc rename : mem/bridge.hh => src/mem/bridge.hh rename : mem/bus.cc => src/mem/bus.cc rename : mem/bus.hh => src/mem/bus.hh rename : mem/cache/prefetch/tagged_prefetcher_impl.hh => src/mem/cache/prefetch/tagged_prefetcher_impl.hh rename : mem/config/prefetch.hh => src/mem/config/prefetch.hh rename : mem/mem_object.cc => src/mem/mem_object.cc rename : mem/mem_object.hh => src/mem/mem_object.hh rename : mem/packet.cc => src/mem/packet.cc rename : mem/packet.hh => src/mem/packet.hh rename : mem/page_table.cc => src/mem/page_table.cc rename : mem/page_table.hh => src/mem/page_table.hh rename : mem/physical.cc => src/mem/physical.cc rename : mem/physical.hh => src/mem/physical.hh rename : mem/port.cc => src/mem/port.cc rename : mem/port.hh => src/mem/port.hh rename : mem/request.hh => src/mem/request.hh rename : mem/translating_port.cc => src/mem/translating_port.cc rename : mem/translating_port.hh => src/mem/translating_port.hh rename : mem/vport.cc => src/mem/vport.cc rename : mem/vport.hh => src/mem/vport.hh rename : python/SConscript => src/python/SConscript rename : python/m5/__init__.py => src/python/m5/__init__.py rename : python/m5/config.py => src/python/m5/config.py rename : python/m5/convert.py => src/python/m5/convert.py rename : python/m5/multidict.py => src/python/m5/multidict.py rename : python/m5/objects/AlphaConsole.py => src/python/m5/objects/AlphaConsole.py rename : python/m5/objects/AlphaFullCPU.py => src/python/m5/objects/AlphaFullCPU.py rename : python/m5/objects/AlphaTLB.py => src/python/m5/objects/AlphaTLB.py rename : python/m5/objects/BadDevice.py => src/python/m5/objects/BadDevice.py rename : python/m5/objects/BaseCPU.py => src/python/m5/objects/BaseCPU.py rename : python/m5/objects/BaseCache.py => src/python/m5/objects/BaseCache.py rename : python/m5/objects/Bridge.py => src/python/m5/objects/Bridge.py rename : python/m5/objects/Bus.py => src/python/m5/objects/Bus.py rename : python/m5/objects/CoherenceProtocol.py => src/python/m5/objects/CoherenceProtocol.py rename : python/m5/objects/Device.py => src/python/m5/objects/Device.py rename : python/m5/objects/DiskImage.py => src/python/m5/objects/DiskImage.py rename : python/m5/objects/Ethernet.py => src/python/m5/objects/Ethernet.py rename : python/m5/objects/Ide.py => src/python/m5/objects/Ide.py rename : python/m5/objects/IntrControl.py => src/python/m5/objects/IntrControl.py rename : python/m5/objects/MemObject.py => src/python/m5/objects/MemObject.py rename : python/m5/objects/MemTest.py => src/python/m5/objects/MemTest.py rename : python/m5/objects/Pci.py => src/python/m5/objects/Pci.py rename : python/m5/objects/PhysicalMemory.py => src/python/m5/objects/PhysicalMemory.py rename : python/m5/objects/Platform.py => src/python/m5/objects/Platform.py rename : python/m5/objects/Process.py => src/python/m5/objects/Process.py rename : python/m5/objects/Repl.py => src/python/m5/objects/Repl.py rename : python/m5/objects/Root.py => src/python/m5/objects/Root.py rename : python/m5/objects/SimConsole.py => src/python/m5/objects/SimConsole.py rename : python/m5/objects/SimpleDisk.py => src/python/m5/objects/SimpleDisk.py rename : python/m5/objects/System.py => src/python/m5/objects/System.py rename : python/m5/objects/Tsunami.py => src/python/m5/objects/Tsunami.py rename : python/m5/objects/Uart.py => src/python/m5/objects/Uart.py rename : python/m5/smartdict.py => src/python/m5/smartdict.py rename : sim/async.hh => src/sim/async.hh rename : sim/builder.cc => src/sim/builder.cc rename : sim/builder.hh => src/sim/builder.hh rename : sim/byteswap.hh => src/sim/byteswap.hh rename : sim/debug.cc => src/sim/debug.cc rename : sim/debug.hh => src/sim/debug.hh rename : sim/eventq.cc => src/sim/eventq.cc rename : sim/eventq.hh => src/sim/eventq.hh rename : sim/faults.cc => src/sim/faults.cc rename : sim/faults.hh => src/sim/faults.hh rename : sim/host.hh => src/sim/host.hh rename : sim/main.cc => src/sim/main.cc rename : sim/param.cc => src/sim/param.cc rename : sim/param.hh => src/sim/param.hh rename : sim/process.cc => src/sim/process.cc rename : sim/process.hh => src/sim/process.hh rename : sim/pseudo_inst.cc => src/sim/pseudo_inst.cc rename : sim/pseudo_inst.hh => src/sim/pseudo_inst.hh rename : sim/root.cc => src/sim/root.cc rename : sim/serialize.cc => src/sim/serialize.cc rename : sim/serialize.hh => src/sim/serialize.hh rename : sim/sim_events.cc => src/sim/sim_events.cc rename : sim/sim_events.hh => src/sim/sim_events.hh rename : sim/sim_exit.hh => src/sim/sim_exit.hh rename : sim/sim_object.cc => src/sim/sim_object.cc rename : sim/sim_object.hh => src/sim/sim_object.hh rename : sim/startup.cc => src/sim/startup.cc rename : sim/startup.hh => src/sim/startup.hh rename : sim/stat_control.cc => src/sim/stat_control.cc rename : sim/stat_control.hh => src/sim/stat_control.hh rename : sim/stats.hh => src/sim/stats.hh rename : sim/syscall_emul.cc => src/sim/syscall_emul.cc rename : sim/syscall_emul.hh => src/sim/syscall_emul.hh rename : sim/system.cc => src/sim/system.cc rename : sim/system.hh => src/sim/system.hh rename : sim/vptr.hh => src/sim/vptr.hh rename : test/Makefile => src/unittest/Makefile rename : test/bitvectest.cc => src/unittest/bitvectest.cc rename : test/circletest.cc => src/unittest/circletest.cc rename : test/cprintftest.cc => src/unittest/cprintftest.cc rename : test/foo.ini => src/unittest/foo.ini rename : test/genini.py => src/unittest/genini.py rename : test/initest.cc => src/unittest/initest.cc rename : test/initest.ini => src/unittest/initest.ini rename : test/lru_test.cc => src/unittest/lru_test.cc rename : test/nmtest.cc => src/unittest/nmtest.cc rename : test/offtest.cc => src/unittest/offtest.cc rename : test/paramtest.cc => src/unittest/paramtest.cc rename : test/rangetest.cc => src/unittest/rangetest.cc rename : test/sized_test.cc => src/unittest/sized_test.cc rename : test/stattest.cc => src/unittest/stattest.cc rename : test/strnumtest.cc => src/unittest/strnumtest.cc rename : test/symtest.cc => src/unittest/symtest.cc rename : test/tokentest.cc => src/unittest/tokentest.cc rename : test/tracetest.cc => src/unittest/tracetest.cc extra : convert_revision : cab6a5271ca1b368193cd948e5d3dcc47ab1bd48
1846 lines
65 KiB
Python
1846 lines
65 KiB
Python
#-----------------------------------------------------------------------------
|
|
# ply: yacc.py
|
|
#
|
|
# Author: David M. Beazley (beazley@cs.uchicago.edu)
|
|
# Department of Computer Science
|
|
# University of Chicago
|
|
# Chicago, IL 60637
|
|
#
|
|
# Copyright (C) 2001, David M. Beazley
|
|
#
|
|
# $Header: /home/stever/bk/newmem2/ext/ply/yacc.py 1.3 03/06/06 14:59:28-00:00 stever@ $
|
|
#
|
|
# This library is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
# License as published by the Free Software Foundation; either
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
#
|
|
# This library 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
|
|
# Lesser General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
# License along with this library; if not, write to the Free Software
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
#
|
|
# See the file COPYING for a complete copy of the LGPL.
|
|
#
|
|
#
|
|
# This implements an LR parser that is constructed from grammar rules defined
|
|
# as Python functions. Roughly speaking, this module is a cross between
|
|
# John Aycock's Spark system and the GNU bison utility.
|
|
#
|
|
# Disclaimer: This is a work in progress. SLR parsing seems to work fairly
|
|
# well and there is extensive error checking. LALR(1) is in progress. The
|
|
# rest of this file is a bit of a mess. Please pardon the dust.
|
|
#
|
|
# The current implementation is only somewhat object-oriented. The
|
|
# LR parser itself is defined in terms of an object (which allows multiple
|
|
# parsers to co-exist). However, most of the variables used during table
|
|
# construction are defined in terms of global variables. Users shouldn't
|
|
# notice unless they are trying to define multiple parsers at the same
|
|
# time using threads (in which case they should have their head examined).
|
|
#-----------------------------------------------------------------------------
|
|
|
|
__version__ = "1.3"
|
|
|
|
#-----------------------------------------------------------------------------
|
|
# === User configurable parameters ===
|
|
#
|
|
# Change these to modify the default behavior of yacc (if you wish)
|
|
#-----------------------------------------------------------------------------
|
|
|
|
yaccdebug = 1 # Debugging mode. If set, yacc generates a
|
|
# a 'parser.out' file in the current directory
|
|
|
|
debug_file = 'parser.out' # Default name of the debugging file
|
|
tab_module = 'parsetab' # Default name of the table module
|
|
default_lr = 'SLR' # Default LR table generation method
|
|
|
|
error_count = 3 # Number of symbols that must be shifted to leave recovery mode
|
|
|
|
import re, types, sys, cStringIO, md5, os.path
|
|
|
|
# Exception raised for yacc-related errors
|
|
class YaccError(Exception): pass
|
|
|
|
#-----------------------------------------------------------------------------
|
|
# === LR Parsing Engine ===
|
|
#
|
|
# The following classes are used for the LR parser itself. These are not
|
|
# used during table construction and are independent of the actual LR
|
|
# table generation algorithm
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# This class is used to hold non-terminal grammar symbols during parsing.
|
|
# It normally has the following attributes set:
|
|
# .type = Grammar symbol type
|
|
# .value = Symbol value
|
|
# .lineno = Starting line number
|
|
# .endlineno = Ending line number (optional, set automatically)
|
|
|
|
class YaccSymbol:
|
|
def __str__(self): return self.type
|
|
def __repr__(self): return str(self)
|
|
|
|
# This class is a wrapper around the objects actually passed to each
|
|
# grammar rule. Index lookup and assignment actually assign the
|
|
# .value attribute of the underlying YaccSymbol object.
|
|
# The lineno() method returns the line number of a given
|
|
# item (or 0 if not defined). The linespan() method returns
|
|
# a tuple of (startline,endline) representing the range of lines
|
|
# for a symbol.
|
|
|
|
class YaccSlice:
|
|
def __init__(self,s):
|
|
self.slice = s
|
|
self.pbstack = []
|
|
|
|
def __getitem__(self,n):
|
|
return self.slice[n].value
|
|
|
|
def __setitem__(self,n,v):
|
|
self.slice[n].value = v
|
|
|
|
def __len__(self):
|
|
return len(self.slice)
|
|
|
|
def lineno(self,n):
|
|
return getattr(self.slice[n],"lineno",0)
|
|
|
|
def linespan(self,n):
|
|
startline = getattr(self.slice[n],"lineno",0)
|
|
endline = getattr(self.slice[n],"endlineno",startline)
|
|
return startline,endline
|
|
|
|
def pushback(self,n):
|
|
if n <= 0:
|
|
raise ValueError, "Expected a positive value"
|
|
if n > (len(self.slice)-1):
|
|
raise ValueError, "Can't push %d tokens. Only %d are available." % (n,len(self.slice)-1)
|
|
for i in range(0,n):
|
|
self.pbstack.append(self.slice[-i-1])
|
|
|
|
# The LR Parsing engine. This is defined as a class so that multiple parsers
|
|
# can exist in the same process. A user never instantiates this directly.
|
|
# Instead, the global yacc() function should be used to create a suitable Parser
|
|
# object.
|
|
|
|
class Parser:
|
|
def __init__(self,magic=None):
|
|
|
|
# This is a hack to keep users from trying to instantiate a Parser
|
|
# object directly.
|
|
|
|
if magic != "xyzzy":
|
|
raise YaccError, "Can't instantiate Parser. Use yacc() instead."
|
|
|
|
# Reset internal state
|
|
self.productions = None # List of productions
|
|
self.errorfunc = None # Error handling function
|
|
self.action = { } # LR Action table
|
|
self.goto = { } # LR goto table
|
|
self.require = { } # Attribute require table
|
|
self.method = "Unknown LR" # Table construction method used
|
|
|
|
def errok(self):
|
|
self.errorcount = 0
|
|
|
|
def restart(self):
|
|
del self.statestack[:]
|
|
del self.symstack[:]
|
|
sym = YaccSymbol()
|
|
sym.type = '$'
|
|
self.symstack.append(sym)
|
|
self.statestack.append(0)
|
|
|
|
def parse(self,input=None,lexer=None,debug=0):
|
|
lookahead = None # Current lookahead symbol
|
|
lookaheadstack = [ ] # Stack of lookahead symbols
|
|
actions = self.action # Local reference to action table
|
|
goto = self.goto # Local reference to goto table
|
|
prod = self.productions # Local reference to production list
|
|
pslice = YaccSlice(None) # Slice object passed to grammar rules
|
|
pslice.parser = self # Parser object
|
|
self.errorcount = 0 # Used during error recovery
|
|
|
|
# If no lexer was given, we will try to use the lex module
|
|
if not lexer:
|
|
import lex as lexer
|
|
|
|
pslice.lexer = lexer
|
|
|
|
# If input was supplied, pass to lexer
|
|
if input:
|
|
lexer.input(input)
|
|
|
|
# Tokenize function
|
|
get_token = lexer.token
|
|
|
|
statestack = [ ] # Stack of parsing states
|
|
self.statestack = statestack
|
|
symstack = [ ] # Stack of grammar symbols
|
|
self.symstack = symstack
|
|
|
|
errtoken = None # Err token
|
|
|
|
# The start state is assumed to be (0,$)
|
|
statestack.append(0)
|
|
sym = YaccSymbol()
|
|
sym.type = '$'
|
|
symstack.append(sym)
|
|
|
|
while 1:
|
|
# Get the next symbol on the input. If a lookahead symbol
|
|
# is already set, we just use that. Otherwise, we'll pull
|
|
# the next token off of the lookaheadstack or from the lexer
|
|
if not lookahead:
|
|
if not lookaheadstack:
|
|
lookahead = get_token() # Get the next token
|
|
else:
|
|
lookahead = lookaheadstack.pop()
|
|
if not lookahead:
|
|
lookahead = YaccSymbol()
|
|
lookahead.type = '$'
|
|
if debug:
|
|
print "%-20s : %s" % (lookahead, [xx.type for xx in symstack])
|
|
|
|
# Check the action table
|
|
s = statestack[-1]
|
|
ltype = lookahead.type
|
|
t = actions.get((s,ltype),None)
|
|
|
|
if t is not None:
|
|
if t > 0:
|
|
# shift a symbol on the stack
|
|
if ltype == '$':
|
|
# Error, end of input
|
|
print "yacc: Parse error. EOF"
|
|
return
|
|
statestack.append(t)
|
|
symstack.append(lookahead)
|
|
lookahead = None
|
|
|
|
# Decrease error count on successful shift
|
|
if self.errorcount > 0:
|
|
self.errorcount -= 1
|
|
|
|
continue
|
|
|
|
if t < 0:
|
|
# reduce a symbol on the stack, emit a production
|
|
p = prod[-t]
|
|
pname = p.name
|
|
plen = p.len
|
|
|
|
# Get production function
|
|
sym = YaccSymbol()
|
|
sym.type = pname # Production name
|
|
sym.value = None
|
|
|
|
if plen:
|
|
targ = symstack[-plen-1:]
|
|
targ[0] = sym
|
|
try:
|
|
sym.lineno = targ[1].lineno
|
|
sym.endlineno = getattr(targ[-1],"endlineno",targ[-1].lineno)
|
|
except AttributeError:
|
|
sym.lineno = 0
|
|
del symstack[-plen:]
|
|
del statestack[-plen:]
|
|
else:
|
|
sym.lineno = 0
|
|
targ = [ sym ]
|
|
pslice.slice = targ
|
|
pslice.pbstack = []
|
|
# Call the grammar rule with our special slice object
|
|
p.func(pslice)
|
|
|
|
# Validate attributes of the resulting value attribute
|
|
# if require:
|
|
# try:
|
|
# t0 = targ[0]
|
|
# r = Requires.get(t0.type,None)
|
|
# t0d = t0.__dict__
|
|
# if r:
|
|
# for field in r:
|
|
# tn = t0
|
|
# for fname in field:
|
|
# try:
|
|
# tf = tn.__dict__
|
|
# tn = tf.get(fname)
|
|
# except StandardError:
|
|
# tn = None
|
|
# if not tn:
|
|
# print "%s:%d: Rule %s doesn't set required attribute '%s'" % \
|
|
# (p.file,p.line,p.name,".".join(field))
|
|
# except TypeError,LookupError:
|
|
# print "Bad requires directive " % r
|
|
# pass
|
|
|
|
|
|
# If there was a pushback, put that on the stack
|
|
if pslice.pbstack:
|
|
lookaheadstack.append(lookahead)
|
|
for _t in pslice.pbstack:
|
|
lookaheadstack.append(_t)
|
|
lookahead = None
|
|
|
|
symstack.append(sym)
|
|
statestack.append(goto[statestack[-1],pname])
|
|
continue
|
|
|
|
if t == 0:
|
|
n = symstack[-1]
|
|
return getattr(n,"value",None)
|
|
|
|
if t == None:
|
|
# We have some kind of parsing error here. To handle this,
|
|
# we are going to push the current token onto the tokenstack
|
|
# and replace it with an 'error' token. If there are any synchronization
|
|
# rules, they may catch it.
|
|
#
|
|
# In addition to pushing the error token, we call call the user defined p_error()
|
|
# function if this is the first syntax error. This function is only called
|
|
# if errorcount == 0.
|
|
|
|
if not self.errorcount:
|
|
self.errorcount = error_count
|
|
errtoken = lookahead
|
|
if errtoken.type == '$':
|
|
errtoken = None # End of file!
|
|
if self.errorfunc:
|
|
global errok,token,restart
|
|
errok = self.errok # Set some special functions available in error recovery
|
|
token = get_token
|
|
restart = self.restart
|
|
tok = self.errorfunc(errtoken)
|
|
del errok, token, restart # Delete special functions
|
|
|
|
if not self.errorcount:
|
|
# User must have done some kind of panic mode recovery on their own. The returned token
|
|
# is the next lookahead
|
|
lookahead = tok
|
|
errtoken = None
|
|
continue
|
|
else:
|
|
if errtoken:
|
|
if hasattr(errtoken,"lineno"): lineno = lookahead.lineno
|
|
else: lineno = 0
|
|
if lineno:
|
|
print "yacc: Syntax error at line %d, token=%s" % (lineno, errtoken.type)
|
|
else:
|
|
print "yacc: Syntax error, token=%s" % errtoken.type
|
|
else:
|
|
print "yacc: Parse error in input. EOF"
|
|
return
|
|
|
|
else:
|
|
self.errorcount = error_count
|
|
|
|
# case 1: the statestack only has 1 entry on it. If we're in this state, the
|
|
# entire parse has been rolled back and we're completely hosed. The token is
|
|
# discarded and we just keep going.
|
|
|
|
if len(statestack) <= 1 and lookahead.type != '$':
|
|
lookahead = None
|
|
errtoken = None
|
|
# Nuke the pushback stack
|
|
del lookaheadstack[:]
|
|
continue
|
|
|
|
# case 2: the statestack has a couple of entries on it, but we're
|
|
# at the end of the file. nuke the top entry and generate an error token
|
|
|
|
# Start nuking entries on the stack
|
|
if lookahead.type == '$':
|
|
# Whoa. We're really hosed here. Bail out
|
|
return
|
|
|
|
if lookahead.type != 'error':
|
|
sym = symstack[-1]
|
|
if sym.type == 'error':
|
|
# Hmmm. Error is on top of stack, we'll just nuke input
|
|
# symbol and continue
|
|
lookahead = None
|
|
continue
|
|
t = YaccSymbol()
|
|
t.type = 'error'
|
|
if hasattr(lookahead,"lineno"):
|
|
t.lineno = lookahead.lineno
|
|
t.value = lookahead
|
|
lookaheadstack.append(lookahead)
|
|
lookahead = t
|
|
else:
|
|
symstack.pop()
|
|
statestack.pop()
|
|
|
|
continue
|
|
|
|
# Call an error function here
|
|
raise RuntimeError, "yacc: internal parser error!!!\n"
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# === Parser Construction ===
|
|
#
|
|
# The following functions and variables are used to implement the yacc() function
|
|
# itself. This is pretty hairy stuff involving lots of error checking,
|
|
# construction of LR items, kernels, and so forth. Although a lot of
|
|
# this work is done using global variables, the resulting Parser object
|
|
# is completely self contained--meaning that it is safe to repeatedly
|
|
# call yacc() with different grammars in the same application.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# validate_file()
|
|
#
|
|
# This function checks to see if there are duplicated p_rulename() functions
|
|
# in the parser module file. Without this function, it is really easy for
|
|
# users to make mistakes by cutting and pasting code fragments (and it's a real
|
|
# bugger to try and figure out why the resulting parser doesn't work). Therefore,
|
|
# we just do a little regular expression pattern matching of def statements
|
|
# to try and detect duplicates.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
def validate_file(filename):
|
|
base,ext = os.path.splitext(filename)
|
|
if ext != '.py': return 1 # No idea. Assume it's okay.
|
|
|
|
try:
|
|
f = open(filename)
|
|
lines = f.readlines()
|
|
f.close()
|
|
except IOError:
|
|
return 1 # Oh well
|
|
|
|
# Match def p_funcname(
|
|
fre = re.compile(r'\s*def\s+(p_[a-zA-Z_0-9]*)\(')
|
|
counthash = { }
|
|
linen = 1
|
|
noerror = 1
|
|
for l in lines:
|
|
m = fre.match(l)
|
|
if m:
|
|
name = m.group(1)
|
|
prev = counthash.get(name)
|
|
if not prev:
|
|
counthash[name] = linen
|
|
else:
|
|
print "%s:%d: Function %s redefined. Previously defined on line %d" % (filename,linen,name,prev)
|
|
noerror = 0
|
|
linen += 1
|
|
return noerror
|
|
|
|
# This function looks for functions that might be grammar rules, but which don't have the proper p_suffix.
|
|
def validate_dict(d):
|
|
for n,v in d.items():
|
|
if n[0:2] == 'p_' and isinstance(v,types.FunctionType): continue
|
|
if n[0:2] == 't_': continue
|
|
|
|
if n[0:2] == 'p_':
|
|
print "yacc: Warning. '%s' not defined as a function" % n
|
|
if isinstance(v,types.FunctionType) and v.func_code.co_argcount == 1:
|
|
try:
|
|
doc = v.__doc__.split(" ")
|
|
if doc[1] == ':':
|
|
print "%s:%d: Warning. Possible grammar rule '%s' defined without p_ prefix." % (v.func_code.co_filename, v.func_code.co_firstlineno,n)
|
|
except StandardError:
|
|
pass
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# === GRAMMAR FUNCTIONS ===
|
|
#
|
|
# The following global variables and functions are used to store, manipulate,
|
|
# and verify the grammar rules specified by the user.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Initialize all of the global variables used during grammar construction
|
|
def initialize_vars():
|
|
global Productions, Prodnames, Prodmap, Terminals
|
|
global Nonterminals, First, Follow, Precedence, LRitems
|
|
global Errorfunc, Signature, Requires
|
|
|
|
Productions = [None] # A list of all of the productions. The first
|
|
# entry is always reserved for the purpose of
|
|
# building an augmented grammar
|
|
|
|
Prodnames = { } # A dictionary mapping the names of nonterminals to a list of all
|
|
# productions of that nonterminal.
|
|
|
|
Prodmap = { } # A dictionary that is only used to detect duplicate
|
|
# productions.
|
|
|
|
Terminals = { } # A dictionary mapping the names of terminal symbols to a
|
|
# list of the rules where they are used.
|
|
|
|
Nonterminals = { } # A dictionary mapping names of nonterminals to a list
|
|
# of rule numbers where they are used.
|
|
|
|
First = { } # A dictionary of precomputed FIRST(x) symbols
|
|
|
|
Follow = { } # A dictionary of precomputed FOLLOW(x) symbols
|
|
|
|
Precedence = { } # Precedence rules for each terminal. Contains tuples of the
|
|
# form ('right',level) or ('nonassoc', level) or ('left',level)
|
|
|
|
LRitems = [ ] # A list of all LR items for the grammar. These are the
|
|
# productions with the "dot" like E -> E . PLUS E
|
|
|
|
Errorfunc = None # User defined error handler
|
|
|
|
Signature = md5.new() # Digital signature of the grammar rules, precedence
|
|
# and other information. Used to determined when a
|
|
# parsing table needs to be regenerated.
|
|
|
|
Requires = { } # Requires list
|
|
|
|
# File objects used when creating the parser.out debugging file
|
|
global _vf, _vfc
|
|
_vf = cStringIO.StringIO()
|
|
_vfc = cStringIO.StringIO()
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# class Production:
|
|
#
|
|
# This class stores the raw information about a single production or grammar rule.
|
|
# It has a few required attributes:
|
|
#
|
|
# name - Name of the production (nonterminal)
|
|
# prod - A list of symbols making up its production
|
|
# number - Production number.
|
|
#
|
|
# In addition, a few additional attributes are used to help with debugging or
|
|
# optimization of table generation.
|
|
#
|
|
# file - File where production action is defined.
|
|
# lineno - Line number where action is defined
|
|
# func - Action function
|
|
# prec - Precedence level
|
|
# lr_next - Next LR item. Example, if we are ' E -> E . PLUS E'
|
|
# then lr_next refers to 'E -> E PLUS . E'
|
|
# lr_index - LR item index (location of the ".") in the prod list.
|
|
# len - Length of the production (number of symbols on right hand side)
|
|
# -----------------------------------------------------------------------------
|
|
|
|
class Production:
|
|
def __init__(self,**kw):
|
|
for k,v in kw.items():
|
|
setattr(self,k,v)
|
|
self.lr_index = -1
|
|
self.lr0_added = 0 # Flag indicating whether or not added to LR0 closure
|
|
self.usyms = [ ]
|
|
|
|
def __str__(self):
|
|
if self.prod:
|
|
s = "%s -> %s" % (self.name," ".join(self.prod))
|
|
else:
|
|
s = "%s -> <empty>" % self.name
|
|
return s
|
|
|
|
def __repr__(self):
|
|
return str(self)
|
|
|
|
# Compute lr_items from the production
|
|
def lr_item(self,n):
|
|
if n > len(self.prod): return None
|
|
p = Production()
|
|
p.name = self.name
|
|
p.prod = list(self.prod)
|
|
p.number = self.number
|
|
p.lr_index = n
|
|
p.prod.insert(n,".")
|
|
p.prod = tuple(p.prod)
|
|
p.len = len(p.prod)
|
|
p.usyms = self.usyms
|
|
|
|
# Precompute list of productions immediately following
|
|
try:
|
|
p.lrafter = Prodnames[p.prod[n+1]]
|
|
except (IndexError,KeyError),e:
|
|
p.lrafter = []
|
|
try:
|
|
p.lrbefore = p.prod[n-1]
|
|
except IndexError:
|
|
p.lrbefore = None
|
|
|
|
return p
|
|
|
|
class MiniProduction:
|
|
pass
|
|
|
|
# Utility function
|
|
def is_identifier(s):
|
|
for c in s:
|
|
if not (c.isalnum() or c == '_'): return 0
|
|
return 1
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# add_production()
|
|
#
|
|
# Given an action function, this function assembles a production rule.
|
|
# The production rule is assumed to be found in the function's docstring.
|
|
# This rule has the general syntax:
|
|
#
|
|
# name1 ::= production1
|
|
# | production2
|
|
# | production3
|
|
# ...
|
|
# | productionn
|
|
# name2 ::= production1
|
|
# | production2
|
|
# ...
|
|
# -----------------------------------------------------------------------------
|
|
|
|
def add_production(f,file,line,prodname,syms):
|
|
|
|
if Terminals.has_key(prodname):
|
|
print "%s:%d: Illegal rule name '%s'. Already defined as a token." % (file,line,prodname)
|
|
return -1
|
|
if prodname == 'error':
|
|
print "%s:%d: Illegal rule name '%s'. error is a reserved word." % (file,line,prodname)
|
|
return -1
|
|
|
|
if not is_identifier(prodname):
|
|
print "%s:%d: Illegal rule name '%s'" % (file,line,prodname)
|
|
return -1
|
|
|
|
for s in syms:
|
|
if not is_identifier(s) and s != '%prec':
|
|
print "%s:%d: Illegal name '%s' in rule '%s'" % (file,line,s, prodname)
|
|
return -1
|
|
|
|
# See if the rule is already in the rulemap
|
|
map = "%s -> %s" % (prodname,syms)
|
|
if Prodmap.has_key(map):
|
|
m = Prodmap[map]
|
|
print "%s:%d: Duplicate rule %s." % (file,line, m)
|
|
print "%s:%d: Previous definition at %s:%d" % (file,line, m.file, m.line)
|
|
return -1
|
|
|
|
p = Production()
|
|
p.name = prodname
|
|
p.prod = syms
|
|
p.file = file
|
|
p.line = line
|
|
p.func = f
|
|
p.number = len(Productions)
|
|
|
|
|
|
Productions.append(p)
|
|
Prodmap[map] = p
|
|
if not Nonterminals.has_key(prodname):
|
|
Nonterminals[prodname] = [ ]
|
|
|
|
# Add all terminals to Terminals
|
|
i = 0
|
|
while i < len(p.prod):
|
|
t = p.prod[i]
|
|
if t == '%prec':
|
|
try:
|
|
precname = p.prod[i+1]
|
|
except IndexError:
|
|
print "%s:%d: Syntax error. Nothing follows %%prec." % (p.file,p.line)
|
|
return -1
|
|
|
|
prec = Precedence.get(precname,None)
|
|
if not prec:
|
|
print "%s:%d: Nothing known about the precedence of '%s'" % (p.file,p.line,precname)
|
|
return -1
|
|
else:
|
|
p.prec = prec
|
|
del p.prod[i]
|
|
del p.prod[i]
|
|
continue
|
|
|
|
if Terminals.has_key(t):
|
|
Terminals[t].append(p.number)
|
|
# Is a terminal. We'll assign a precedence to p based on this
|
|
if not hasattr(p,"prec"):
|
|
p.prec = Precedence.get(t,('right',0))
|
|
else:
|
|
if not Nonterminals.has_key(t):
|
|
Nonterminals[t] = [ ]
|
|
Nonterminals[t].append(p.number)
|
|
i += 1
|
|
|
|
if not hasattr(p,"prec"):
|
|
p.prec = ('right',0)
|
|
|
|
# Set final length of productions
|
|
p.len = len(p.prod)
|
|
p.prod = tuple(p.prod)
|
|
|
|
# Calculate unique syms in the production
|
|
p.usyms = [ ]
|
|
for s in p.prod:
|
|
if s not in p.usyms:
|
|
p.usyms.append(s)
|
|
|
|
# Add to the global productions list
|
|
try:
|
|
Prodnames[p.name].append(p)
|
|
except KeyError:
|
|
Prodnames[p.name] = [ p ]
|
|
return 0
|
|
|
|
# Given a raw rule function, this function rips out its doc string
|
|
# and adds rules to the grammar
|
|
|
|
def add_function(f):
|
|
line = f.func_code.co_firstlineno
|
|
file = f.func_code.co_filename
|
|
error = 0
|
|
|
|
if f.func_code.co_argcount > 1:
|
|
print "%s:%d: Rule '%s' has too many arguments." % (file,line,f.__name__)
|
|
return -1
|
|
|
|
if f.func_code.co_argcount < 1:
|
|
print "%s:%d: Rule '%s' requires an argument." % (file,line,f.__name__)
|
|
return -1
|
|
|
|
if f.__doc__:
|
|
# Split the doc string into lines
|
|
pstrings = f.__doc__.splitlines()
|
|
lastp = None
|
|
dline = line
|
|
for ps in pstrings:
|
|
dline += 1
|
|
p = ps.split()
|
|
if not p: continue
|
|
try:
|
|
if p[0] == '|':
|
|
# This is a continuation of a previous rule
|
|
if not lastp:
|
|
print "%s:%d: Misplaced '|'." % (file,dline)
|
|
return -1
|
|
prodname = lastp
|
|
if len(p) > 1:
|
|
syms = p[1:]
|
|
else:
|
|
syms = [ ]
|
|
else:
|
|
prodname = p[0]
|
|
lastp = prodname
|
|
assign = p[1]
|
|
if len(p) > 2:
|
|
syms = p[2:]
|
|
else:
|
|
syms = [ ]
|
|
if assign != ':' and assign != '::=':
|
|
print "%s:%d: Syntax error. Expected ':'" % (file,dline)
|
|
return -1
|
|
e = add_production(f,file,dline,prodname,syms)
|
|
error += e
|
|
except StandardError:
|
|
print "%s:%d: Syntax error in rule '%s'" % (file,dline,ps)
|
|
error -= 1
|
|
else:
|
|
print "%s:%d: No documentation string specified in function '%s'" % (file,line,f.__name__)
|
|
return error
|
|
|
|
|
|
# Cycle checking code (Michael Dyck)
|
|
|
|
def compute_reachable():
|
|
'''
|
|
Find each symbol that can be reached from the start symbol.
|
|
Print a warning for any nonterminals that can't be reached.
|
|
(Unused terminals have already had their warning.)
|
|
'''
|
|
Reachable = { }
|
|
for s in Terminals.keys() + Nonterminals.keys():
|
|
Reachable[s] = 0
|
|
|
|
mark_reachable_from( Productions[0].prod[0], Reachable )
|
|
|
|
for s in Nonterminals.keys():
|
|
if not Reachable[s]:
|
|
print "yacc: Symbol '%s' is unreachable." % s
|
|
|
|
def mark_reachable_from(s, Reachable):
|
|
'''
|
|
Mark all symbols that are reachable from symbol s.
|
|
'''
|
|
if Reachable[s]:
|
|
# We've already reached symbol s.
|
|
return
|
|
Reachable[s] = 1
|
|
for p in Prodnames.get(s,[]):
|
|
for r in p.prod:
|
|
mark_reachable_from(r, Reachable)
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# compute_terminates()
|
|
#
|
|
# This function looks at the various parsing rules and tries to detect
|
|
# infinite recursion cycles (grammar rules where there is no possible way
|
|
# to derive a string of only terminals).
|
|
# -----------------------------------------------------------------------------
|
|
def compute_terminates():
|
|
'''
|
|
Raise an error for any symbols that don't terminate.
|
|
'''
|
|
Terminates = {}
|
|
|
|
# Terminals:
|
|
for t in Terminals.keys():
|
|
Terminates[t] = 1
|
|
|
|
Terminates['$'] = 1
|
|
|
|
# Nonterminals:
|
|
|
|
# Initialize to false:
|
|
for n in Nonterminals.keys():
|
|
Terminates[n] = 0
|
|
|
|
# Then propagate termination until no change:
|
|
while 1:
|
|
some_change = 0
|
|
for (n,pl) in Prodnames.items():
|
|
# Nonterminal n terminates iff any of its productions terminates.
|
|
for p in pl:
|
|
# Production p terminates iff all of its rhs symbols terminate.
|
|
for s in p.prod:
|
|
if not Terminates[s]:
|
|
# The symbol s does not terminate,
|
|
# so production p does not terminate.
|
|
p_terminates = 0
|
|
break
|
|
else:
|
|
# didn't break from the loop,
|
|
# so every symbol s terminates
|
|
# so production p terminates.
|
|
p_terminates = 1
|
|
|
|
if p_terminates:
|
|
# symbol n terminates!
|
|
if not Terminates[n]:
|
|
Terminates[n] = 1
|
|
some_change = 1
|
|
# Don't need to consider any more productions for this n.
|
|
break
|
|
|
|
if not some_change:
|
|
break
|
|
|
|
some_error = 0
|
|
for (s,terminates) in Terminates.items():
|
|
if not terminates:
|
|
if not Prodnames.has_key(s) and not Terminals.has_key(s) and s != 'error':
|
|
# s is used-but-not-defined, and we've already warned of that,
|
|
# so it would be overkill to say that it's also non-terminating.
|
|
pass
|
|
else:
|
|
print "yacc: Infinite recursion detected for symbol '%s'." % s
|
|
some_error = 1
|
|
|
|
return some_error
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# verify_productions()
|
|
#
|
|
# This function examines all of the supplied rules to see if they seem valid.
|
|
# -----------------------------------------------------------------------------
|
|
def verify_productions(cycle_check=1):
|
|
error = 0
|
|
for p in Productions:
|
|
if not p: continue
|
|
|
|
for s in p.prod:
|
|
if not Prodnames.has_key(s) and not Terminals.has_key(s) and s != 'error':
|
|
print "%s:%d: Symbol '%s' used, but not defined as a token or a rule." % (p.file,p.line,s)
|
|
error = 1
|
|
continue
|
|
|
|
unused_tok = 0
|
|
# Now verify all of the tokens
|
|
if yaccdebug:
|
|
_vf.write("Unused terminals:\n\n")
|
|
for s,v in Terminals.items():
|
|
if s != 'error' and not v:
|
|
print "yacc: Warning. Token '%s' defined, but not used." % s
|
|
if yaccdebug: _vf.write(" %s\n"% s)
|
|
unused_tok += 1
|
|
|
|
# Print out all of the productions
|
|
if yaccdebug:
|
|
_vf.write("\nGrammar\n\n")
|
|
for i in range(1,len(Productions)):
|
|
_vf.write("Rule %-5d %s\n" % (i, Productions[i]))
|
|
|
|
unused_prod = 0
|
|
# Verify the use of all productions
|
|
for s,v in Nonterminals.items():
|
|
if not v:
|
|
p = Prodnames[s][0]
|
|
print "%s:%d: Warning. Rule '%s' defined, but not used." % (p.file,p.line, s)
|
|
unused_prod += 1
|
|
|
|
|
|
if unused_tok == 1:
|
|
print "yacc: Warning. There is 1 unused token."
|
|
if unused_tok > 1:
|
|
print "yacc: Warning. There are %d unused tokens." % unused_tok
|
|
|
|
if unused_prod == 1:
|
|
print "yacc: Warning. There is 1 unused rule."
|
|
if unused_prod > 1:
|
|
print "yacc: Warning. There are %d unused rules." % unused_prod
|
|
|
|
if yaccdebug:
|
|
_vf.write("\nTerminals, with rules where they appear\n\n")
|
|
ks = Terminals.keys()
|
|
ks.sort()
|
|
for k in ks:
|
|
_vf.write("%-20s : %s\n" % (k, " ".join([str(s) for s in Terminals[k]])))
|
|
_vf.write("\nNonterminals, with rules where they appear\n\n")
|
|
ks = Nonterminals.keys()
|
|
ks.sort()
|
|
for k in ks:
|
|
_vf.write("%-20s : %s\n" % (k, " ".join([str(s) for s in Nonterminals[k]])))
|
|
|
|
if (cycle_check):
|
|
compute_reachable()
|
|
error += compute_terminates()
|
|
# error += check_cycles()
|
|
return error
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# build_lritems()
|
|
#
|
|
# This function walks the list of productions and builds a complete set of the
|
|
# LR items. The LR items are stored in two ways: First, they are uniquely
|
|
# numbered and placed in the list _lritems. Second, a linked list of LR items
|
|
# is built for each production. For example:
|
|
#
|
|
# E -> E PLUS E
|
|
#
|
|
# Creates the list
|
|
#
|
|
# [E -> . E PLUS E, E -> E . PLUS E, E -> E PLUS . E, E -> E PLUS E . ]
|
|
# -----------------------------------------------------------------------------
|
|
|
|
def build_lritems():
|
|
for p in Productions:
|
|
lastlri = p
|
|
lri = p.lr_item(0)
|
|
i = 0
|
|
while 1:
|
|
lri = p.lr_item(i)
|
|
lastlri.lr_next = lri
|
|
if not lri: break
|
|
lri.lr_num = len(LRitems)
|
|
LRitems.append(lri)
|
|
lastlri = lri
|
|
i += 1
|
|
|
|
# In order for the rest of the parser generator to work, we need to
|
|
# guarantee that no more lritems are generated. Therefore, we nuke
|
|
# the p.lr_item method. (Only used in debugging)
|
|
# Production.lr_item = None
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# add_precedence()
|
|
#
|
|
# Given a list of precedence rules, add to the precedence table.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
def add_precedence(plist):
|
|
plevel = 0
|
|
error = 0
|
|
for p in plist:
|
|
plevel += 1
|
|
try:
|
|
prec = p[0]
|
|
terms = p[1:]
|
|
if prec != 'left' and prec != 'right' and prec != 'nonassoc':
|
|
print "yacc: Invalid precedence '%s'" % prec
|
|
return -1
|
|
for t in terms:
|
|
if Precedence.has_key(t):
|
|
print "yacc: Precedence already specified for terminal '%s'" % t
|
|
error += 1
|
|
continue
|
|
Precedence[t] = (prec,plevel)
|
|
except:
|
|
print "yacc: Invalid precedence table."
|
|
error += 1
|
|
|
|
return error
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# augment_grammar()
|
|
#
|
|
# Compute the augmented grammar. This is just a rule S' -> start where start
|
|
# is the starting symbol.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
def augment_grammar(start=None):
|
|
if not start:
|
|
start = Productions[1].name
|
|
Productions[0] = Production(name="S'",prod=[start],number=0,len=1,prec=('right',0),func=None)
|
|
Productions[0].usyms = [ start ]
|
|
Nonterminals[start].append(0)
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
# first()
|
|
#
|
|
# Compute the value of FIRST1(beta) where beta is a tuple of symbols.
|
|
#
|
|
# During execution of compute_first1, the result may be incomplete.
|
|
# Afterward (e.g., when called from compute_follow()), it will be complete.
|
|
# -------------------------------------------------------------------------
|
|
def first(beta):
|
|
|
|
# We are computing First(x1,x2,x3,...,xn)
|
|
result = [ ]
|
|
for x in beta:
|
|
x_produces_empty = 0
|
|
|
|
# Add all the non-<empty> symbols of First[x] to the result.
|
|
for f in First[x]:
|
|
if f == '<empty>':
|
|
x_produces_empty = 1
|
|
else:
|
|
if f not in result: result.append(f)
|
|
|
|
if x_produces_empty:
|
|
# We have to consider the next x in beta,
|
|
# i.e. stay in the loop.
|
|
pass
|
|
else:
|
|
# We don't have to consider any further symbols in beta.
|
|
break
|
|
else:
|
|
# There was no 'break' from the loop,
|
|
# so x_produces_empty was true for all x in beta,
|
|
# so beta produces empty as well.
|
|
result.append('<empty>')
|
|
|
|
return result
|
|
|
|
|
|
# FOLLOW(x)
|
|
# Given a non-terminal. This function computes the set of all symbols
|
|
# that might follow it. Dragon book, p. 189.
|
|
|
|
def compute_follow(start=None):
|
|
# Add '$' to the follow list of the start symbol
|
|
for k in Nonterminals.keys():
|
|
Follow[k] = [ ]
|
|
|
|
if not start:
|
|
start = Productions[1].name
|
|
|
|
Follow[start] = [ '$' ]
|
|
|
|
while 1:
|
|
didadd = 0
|
|
for p in Productions[1:]:
|
|
# Here is the production set
|
|
for i in range(len(p.prod)):
|
|
B = p.prod[i]
|
|
if Nonterminals.has_key(B):
|
|
# Okay. We got a non-terminal in a production
|
|
fst = first(p.prod[i+1:])
|
|
hasempty = 0
|
|
for f in fst:
|
|
if f != '<empty>' and f not in Follow[B]:
|
|
Follow[B].append(f)
|
|
didadd = 1
|
|
if f == '<empty>':
|
|
hasempty = 1
|
|
if hasempty or i == (len(p.prod)-1):
|
|
# Add elements of follow(a) to follow(b)
|
|
for f in Follow[p.name]:
|
|
if f not in Follow[B]:
|
|
Follow[B].append(f)
|
|
didadd = 1
|
|
if not didadd: break
|
|
|
|
if 0 and yaccdebug:
|
|
_vf.write('\nFollow:\n')
|
|
for k in Nonterminals.keys():
|
|
_vf.write("%-20s : %s\n" % (k, " ".join([str(s) for s in Follow[k]])))
|
|
|
|
# -------------------------------------------------------------------------
|
|
# compute_first1()
|
|
#
|
|
# Compute the value of FIRST1(X) for all symbols
|
|
# -------------------------------------------------------------------------
|
|
def compute_first1():
|
|
|
|
# Terminals:
|
|
for t in Terminals.keys():
|
|
First[t] = [t]
|
|
|
|
First['$'] = ['$']
|
|
First['#'] = ['#'] # what's this for?
|
|
|
|
# Nonterminals:
|
|
|
|
# Initialize to the empty set:
|
|
for n in Nonterminals.keys():
|
|
First[n] = []
|
|
|
|
# Then propagate symbols until no change:
|
|
while 1:
|
|
some_change = 0
|
|
for n in Nonterminals.keys():
|
|
for p in Prodnames[n]:
|
|
for f in first(p.prod):
|
|
if f not in First[n]:
|
|
First[n].append( f )
|
|
some_change = 1
|
|
if not some_change:
|
|
break
|
|
|
|
if 0 and yaccdebug:
|
|
_vf.write('\nFirst:\n')
|
|
for k in Nonterminals.keys():
|
|
_vf.write("%-20s : %s\n" %
|
|
(k, " ".join([str(s) for s in First[k]])))
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# === SLR Generation ===
|
|
#
|
|
# The following functions are used to construct SLR (Simple LR) parsing tables
|
|
# as described on p.221-229 of the dragon book.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Global variables for the LR parsing engine
|
|
def lr_init_vars():
|
|
global _lr_action, _lr_goto, _lr_method
|
|
global _lr_goto_cache
|
|
|
|
_lr_action = { } # Action table
|
|
_lr_goto = { } # Goto table
|
|
_lr_method = "Unknown" # LR method used
|
|
_lr_goto_cache = { }
|
|
|
|
# Compute the LR(0) closure operation on I, where I is a set of LR(0) items.
|
|
# prodlist is a list of productions.
|
|
|
|
_add_count = 0 # Counter used to detect cycles
|
|
|
|
def lr0_closure(I):
|
|
global _add_count
|
|
|
|
_add_count += 1
|
|
prodlist = Productions
|
|
|
|
# Add everything in I to J
|
|
J = I[:]
|
|
didadd = 1
|
|
while didadd:
|
|
didadd = 0
|
|
for j in J:
|
|
for x in j.lrafter:
|
|
if x.lr0_added == _add_count: continue
|
|
# Add B --> .G to J
|
|
J.append(x.lr_next)
|
|
x.lr0_added = _add_count
|
|
didadd = 1
|
|
|
|
return J
|
|
|
|
# Compute the LR(0) goto function goto(I,X) where I is a set
|
|
# of LR(0) items and X is a grammar symbol. This function is written
|
|
# in a way that guarantees uniqueness of the generated goto sets
|
|
# (i.e. the same goto set will never be returned as two different Python
|
|
# objects). With uniqueness, we can later do fast set comparisons using
|
|
# id(obj) instead of element-wise comparison.
|
|
|
|
def lr0_goto(I,x):
|
|
# First we look for a previously cached entry
|
|
g = _lr_goto_cache.get((id(I),x),None)
|
|
if g: return g
|
|
|
|
# Now we generate the goto set in a way that guarantees uniqueness
|
|
# of the result
|
|
|
|
s = _lr_goto_cache.get(x,None)
|
|
if not s:
|
|
s = { }
|
|
_lr_goto_cache[x] = s
|
|
|
|
gs = [ ]
|
|
for p in I:
|
|
n = p.lr_next
|
|
if n and n.lrbefore == x:
|
|
s1 = s.get(id(n),None)
|
|
if not s1:
|
|
s1 = { }
|
|
s[id(n)] = s1
|
|
gs.append(n)
|
|
s = s1
|
|
g = s.get('$',None)
|
|
if not g:
|
|
if gs:
|
|
g = lr0_closure(gs)
|
|
s['$'] = g
|
|
else:
|
|
s['$'] = gs
|
|
_lr_goto_cache[(id(I),x)] = g
|
|
return g
|
|
|
|
# Compute the kernel of a set of LR(0) items
|
|
def lr0_kernel(I):
|
|
KI = [ ]
|
|
for p in I:
|
|
if p.name == "S'" or p.lr_index > 0 or p.len == 0:
|
|
KI.append(p)
|
|
|
|
return KI
|
|
|
|
_lr0_cidhash = { }
|
|
|
|
# Compute the LR(0) sets of item function
|
|
def lr0_items():
|
|
|
|
C = [ lr0_closure([Productions[0].lr_next]) ]
|
|
i = 0
|
|
for I in C:
|
|
_lr0_cidhash[id(I)] = i
|
|
i += 1
|
|
|
|
# Loop over the items in C and each grammar symbols
|
|
i = 0
|
|
while i < len(C):
|
|
I = C[i]
|
|
i += 1
|
|
|
|
# Collect all of the symbols that could possibly be in the goto(I,X) sets
|
|
asyms = { }
|
|
for ii in I:
|
|
for s in ii.usyms:
|
|
asyms[s] = None
|
|
|
|
for x in asyms.keys():
|
|
g = lr0_goto(I,x)
|
|
if not g: continue
|
|
if _lr0_cidhash.has_key(id(g)): continue
|
|
_lr0_cidhash[id(g)] = len(C)
|
|
C.append(g)
|
|
|
|
return C
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# slr_parse_table()
|
|
#
|
|
# This function constructs an SLR table.
|
|
# -----------------------------------------------------------------------------
|
|
def slr_parse_table():
|
|
global _lr_method
|
|
goto = _lr_goto # Goto array
|
|
action = _lr_action # Action array
|
|
actionp = { } # Action production array (temporary)
|
|
|
|
_lr_method = "SLR"
|
|
|
|
n_srconflict = 0
|
|
n_rrconflict = 0
|
|
|
|
if yaccdebug:
|
|
_vf.write("\n\nParsing method: SLR\n\n")
|
|
|
|
# Step 1: Construct C = { I0, I1, ... IN}, collection of LR(0) items
|
|
# This determines the number of states
|
|
|
|
C = lr0_items()
|
|
|
|
# Build the parser table, state by state
|
|
st = 0
|
|
for I in C:
|
|
# Loop over each production in I
|
|
actlist = [ ] # List of actions
|
|
|
|
if yaccdebug:
|
|
_vf.write("\nstate %d\n\n" % st)
|
|
for p in I:
|
|
_vf.write(" (%d) %s\n" % (p.number, str(p)))
|
|
_vf.write("\n")
|
|
|
|
for p in I:
|
|
try:
|
|
if p.prod[-1] == ".":
|
|
if p.name == "S'":
|
|
# Start symbol. Accept!
|
|
action[st,"$"] = 0
|
|
actionp[st,"$"] = p
|
|
else:
|
|
# We are at the end of a production. Reduce!
|
|
for a in Follow[p.name]:
|
|
actlist.append((a,p,"reduce using rule %d (%s)" % (p.number,p)))
|
|
r = action.get((st,a),None)
|
|
if r is not None:
|
|
# Whoa. Have a shift/reduce or reduce/reduce conflict
|
|
if r > 0:
|
|
# Need to decide on shift or reduce here
|
|
# By default we favor shifting. Need to add
|
|
# some precedence rules here.
|
|
sprec,slevel = Productions[actionp[st,a].number].prec
|
|
rprec,rlevel = Precedence.get(a,('right',0))
|
|
if (slevel < rlevel) or ((slevel == rlevel) and (rprec == 'left')):
|
|
# We really need to reduce here.
|
|
action[st,a] = -p.number
|
|
actionp[st,a] = p
|
|
if not slevel and not rlevel:
|
|
_vfc.write("shift/reduce conflict in state %d resolved as reduce.\n" % st)
|
|
_vf.write(" ! shift/reduce conflict for %s resolved as reduce.\n" % a)
|
|
n_srconflict += 1
|
|
elif (slevel == rlevel) and (rprec == 'nonassoc'):
|
|
action[st,a] = None
|
|
else:
|
|
# Hmmm. Guess we'll keep the shift
|
|
if not slevel and not rlevel:
|
|
_vfc.write("shift/reduce conflict in state %d resolved as shift.\n" % st)
|
|
_vf.write(" ! shift/reduce conflict for %s resolved as shift.\n" % a)
|
|
n_srconflict +=1
|
|
elif r < 0:
|
|
# Reduce/reduce conflict. In this case, we favor the rule
|
|
# that was defined first in the grammar file
|
|
oldp = Productions[-r]
|
|
pp = Productions[p.number]
|
|
if oldp.line > pp.line:
|
|
action[st,a] = -p.number
|
|
actionp[st,a] = p
|
|
# print "Reduce/reduce conflict in state %d" % st
|
|
n_rrconflict += 1
|
|
_vfc.write("reduce/reduce conflict in state %d resolved using rule %d (%s).\n" % (st, actionp[st,a].number, actionp[st,a]))
|
|
_vf.write(" ! reduce/reduce conflict for %s resolved using rule %d (%s).\n" % (a,actionp[st,a].number, actionp[st,a]))
|
|
else:
|
|
print "Unknown conflict in state %d" % st
|
|
else:
|
|
action[st,a] = -p.number
|
|
actionp[st,a] = p
|
|
else:
|
|
i = p.lr_index
|
|
a = p.prod[i+1] # Get symbol right after the "."
|
|
if Terminals.has_key(a):
|
|
g = lr0_goto(I,a)
|
|
j = _lr0_cidhash.get(id(g),-1)
|
|
if j >= 0:
|
|
# We are in a shift state
|
|
actlist.append((a,p,"shift and go to state %d" % j))
|
|
r = action.get((st,a),None)
|
|
if r is not None:
|
|
# Whoa have a shift/reduce or shift/shift conflict
|
|
if r > 0:
|
|
if r != j:
|
|
print "Shift/shift conflict in state %d" % st
|
|
elif r < 0:
|
|
# Do a precedence check.
|
|
# - if precedence of reduce rule is higher, we reduce.
|
|
# - if precedence of reduce is same and left assoc, we reduce.
|
|
# - otherwise we shift
|
|
rprec,rlevel = Productions[actionp[st,a].number].prec
|
|
sprec,slevel = Precedence.get(a,('right',0))
|
|
if (slevel > rlevel) or ((slevel == rlevel) and (rprec != 'left')):
|
|
# We decide to shift here... highest precedence to shift
|
|
action[st,a] = j
|
|
actionp[st,a] = p
|
|
if not slevel and not rlevel:
|
|
n_srconflict += 1
|
|
_vfc.write("shift/reduce conflict in state %d resolved as shift.\n" % st)
|
|
_vf.write(" ! shift/reduce conflict for %s resolved as shift.\n" % a)
|
|
elif (slevel == rlevel) and (rprec == 'nonassoc'):
|
|
action[st,a] = None
|
|
else:
|
|
# Hmmm. Guess we'll keep the reduce
|
|
if not slevel and not rlevel:
|
|
n_srconflict +=1
|
|
_vfc.write("shift/reduce conflict in state %d resolved as reduce.\n" % st)
|
|
_vf.write(" ! shift/reduce conflict for %s resolved as reduce.\n" % a)
|
|
|
|
else:
|
|
print "Unknown conflict in state %d" % st
|
|
else:
|
|
action[st,a] = j
|
|
actionp[st,a] = p
|
|
|
|
except StandardError,e:
|
|
raise YaccError, "Hosed in slr_parse_table", e
|
|
|
|
# Print the actions associated with each terminal
|
|
if yaccdebug:
|
|
for a,p,m in actlist:
|
|
if action.has_key((st,a)):
|
|
if p is actionp[st,a]:
|
|
_vf.write(" %-15s %s\n" % (a,m))
|
|
_vf.write("\n")
|
|
for a,p,m in actlist:
|
|
if action.has_key((st,a)):
|
|
if p is not actionp[st,a]:
|
|
_vf.write(" ! %-15s [ %s ]\n" % (a,m))
|
|
|
|
# Construct the goto table for this state
|
|
if yaccdebug:
|
|
_vf.write("\n")
|
|
nkeys = { }
|
|
for ii in I:
|
|
for s in ii.usyms:
|
|
if Nonterminals.has_key(s):
|
|
nkeys[s] = None
|
|
for n in nkeys.keys():
|
|
g = lr0_goto(I,n)
|
|
j = _lr0_cidhash.get(id(g),-1)
|
|
if j >= 0:
|
|
goto[st,n] = j
|
|
if yaccdebug:
|
|
_vf.write(" %-15s shift and go to state %d\n" % (n,j))
|
|
|
|
st += 1
|
|
|
|
if n_srconflict == 1:
|
|
print "yacc: %d shift/reduce conflict" % n_srconflict
|
|
if n_srconflict > 1:
|
|
print "yacc: %d shift/reduce conflicts" % n_srconflict
|
|
if n_rrconflict == 1:
|
|
print "yacc: %d reduce/reduce conflict" % n_rrconflict
|
|
if n_rrconflict > 1:
|
|
print "yacc: %d reduce/reduce conflicts" % n_rrconflict
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# ==== LALR(1) Parsing ====
|
|
# **** UNFINISHED! 6/16/01
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
# Compute the lr1_closure of a set I. I is a list of tuples (p,a) where
|
|
# p is a LR0 item and a is a terminal
|
|
|
|
_lr1_add_count = 0
|
|
|
|
def lr1_closure(I):
|
|
global _lr1_add_count
|
|
|
|
_lr1_add_count += 1
|
|
|
|
J = I[:]
|
|
|
|
# Loop over items (p,a) in I.
|
|
ji = 0
|
|
while ji < len(J):
|
|
p,a = J[ji]
|
|
# p = [ A -> alpha . B beta]
|
|
|
|
# For each production B -> gamma
|
|
for B in p.lr1_after:
|
|
f = tuple(p.lr1_beta + (a,))
|
|
|
|
# For each terminal b in first(Beta a)
|
|
for b in first(f):
|
|
# Check if (B -> . gamma, b) is in J
|
|
# Only way this can happen is if the add count mismatches
|
|
pn = B.lr_next
|
|
if pn.lr_added.get(b,0) == _lr1_add_count: continue
|
|
pn.lr_added[b] = _lr1_add_count
|
|
J.append((pn,b))
|
|
ji += 1
|
|
|
|
return J
|
|
|
|
def lalr_parse_table():
|
|
|
|
# Compute some lr1 information about all of the productions
|
|
for p in LRitems:
|
|
try:
|
|
after = p.prod[p.lr_index + 1]
|
|
p.lr1_after = Prodnames[after]
|
|
p.lr1_beta = p.prod[p.lr_index + 2:]
|
|
except LookupError:
|
|
p.lr1_after = [ ]
|
|
p.lr1_beta = [ ]
|
|
p.lr_added = { }
|
|
|
|
# Compute the LR(0) items
|
|
C = lr0_items()
|
|
CK = []
|
|
for I in C:
|
|
CK.append(lr0_kernel(I))
|
|
|
|
print CK
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# ==== LR Utility functions ====
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# _lr_write_tables()
|
|
#
|
|
# This function writes the LR parsing tables to a file
|
|
# -----------------------------------------------------------------------------
|
|
|
|
def lr_write_tables(modulename=tab_module):
|
|
filename = modulename + ".py"
|
|
try:
|
|
f = open(filename,"w")
|
|
|
|
f.write("""
|
|
# %s
|
|
# This file is automatically generated. Do not edit.
|
|
|
|
_lr_method = %s
|
|
|
|
_lr_signature = %s
|
|
""" % (filename, repr(_lr_method), repr(Signature.digest())))
|
|
|
|
# Change smaller to 0 to go back to original tables
|
|
smaller = 1
|
|
|
|
# Factor out names to try and make smaller
|
|
if smaller:
|
|
items = { }
|
|
|
|
for k,v in _lr_action.items():
|
|
i = items.get(k[1])
|
|
if not i:
|
|
i = ([],[])
|
|
items[k[1]] = i
|
|
i[0].append(k[0])
|
|
i[1].append(v)
|
|
|
|
f.write("\n_lr_action_items = {")
|
|
for k,v in items.items():
|
|
f.write("%r:([" % k)
|
|
for i in v[0]:
|
|
f.write("%r," % i)
|
|
f.write("],[")
|
|
for i in v[1]:
|
|
f.write("%r," % i)
|
|
|
|
f.write("]),")
|
|
f.write("}\n")
|
|
|
|
f.write("""
|
|
_lr_action = { }
|
|
for _k, _v in _lr_action_items.items():
|
|
for _x,_y in zip(_v[0],_v[1]):
|
|
_lr_action[(_x,_k)] = _y
|
|
del _lr_action_items
|
|
""")
|
|
|
|
else:
|
|
f.write("\n_lr_action = { ");
|
|
for k,v in _lr_action.items():
|
|
f.write("(%r,%r):%r," % (k[0],k[1],v))
|
|
f.write("}\n");
|
|
|
|
if smaller:
|
|
# Factor out names to try and make smaller
|
|
items = { }
|
|
|
|
for k,v in _lr_goto.items():
|
|
i = items.get(k[1])
|
|
if not i:
|
|
i = ([],[])
|
|
items[k[1]] = i
|
|
i[0].append(k[0])
|
|
i[1].append(v)
|
|
|
|
f.write("\n_lr_goto_items = {")
|
|
for k,v in items.items():
|
|
f.write("%r:([" % k)
|
|
for i in v[0]:
|
|
f.write("%r," % i)
|
|
f.write("],[")
|
|
for i in v[1]:
|
|
f.write("%r," % i)
|
|
|
|
f.write("]),")
|
|
f.write("}\n")
|
|
|
|
f.write("""
|
|
_lr_goto = { }
|
|
for _k, _v in _lr_goto_items.items():
|
|
for _x,_y in zip(_v[0],_v[1]):
|
|
_lr_goto[(_x,_k)] = _y
|
|
del _lr_goto_items
|
|
""")
|
|
else:
|
|
f.write("\n_lr_goto = { ");
|
|
for k,v in _lr_goto.items():
|
|
f.write("(%r,%r):%r," % (k[0],k[1],v))
|
|
f.write("}\n");
|
|
|
|
# Write production table
|
|
f.write("_lr_productions = [\n")
|
|
for p in Productions:
|
|
if p:
|
|
if (p.func):
|
|
f.write(" (%r,%d,%r,%r,%d),\n" % (p.name, p.len, p.func.__name__,p.file,p.line))
|
|
else:
|
|
f.write(" (%r,%d,None,None,None),\n" % (p.name, p.len))
|
|
else:
|
|
f.write(" None,\n")
|
|
f.write("]\n")
|
|
f.close()
|
|
|
|
except IOError,e:
|
|
print "Unable to create '%s'" % filename
|
|
print e
|
|
return
|
|
|
|
def lr_read_tables(module=tab_module,optimize=0):
|
|
global _lr_action, _lr_goto, _lr_productions, _lr_method
|
|
try:
|
|
exec "import %s as parsetab" % module
|
|
|
|
if (optimize) or (Signature.digest() == parsetab._lr_signature):
|
|
_lr_action = parsetab._lr_action
|
|
_lr_goto = parsetab._lr_goto
|
|
_lr_productions = parsetab._lr_productions
|
|
_lr_method = parsetab._lr_method
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
except (ImportError,AttributeError):
|
|
return 0
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# yacc(module)
|
|
#
|
|
# Build the parser module
|
|
# -----------------------------------------------------------------------------
|
|
|
|
def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module, start=None, check_recursion=1, optimize=0):
|
|
global yaccdebug
|
|
yaccdebug = debug
|
|
|
|
initialize_vars()
|
|
files = { }
|
|
error = 0
|
|
|
|
# Add starting symbol to signature
|
|
if start:
|
|
Signature.update(start)
|
|
|
|
# Try to figure out what module we are working with
|
|
if module:
|
|
# User supplied a module object.
|
|
if not isinstance(module, types.ModuleType):
|
|
raise ValueError,"Expected a module"
|
|
|
|
ldict = module.__dict__
|
|
|
|
else:
|
|
# No module given. We might be able to get information from the caller.
|
|
# Throw an exception and unwind the traceback to get the globals
|
|
|
|
try:
|
|
raise RuntimeError
|
|
except RuntimeError:
|
|
e,b,t = sys.exc_info()
|
|
f = t.tb_frame
|
|
f = f.f_back # Walk out to our calling function
|
|
ldict = f.f_globals # Grab its globals dictionary
|
|
|
|
# If running in optimized mode. We're going to
|
|
|
|
if (optimize and lr_read_tables(tabmodule,1)):
|
|
# Read parse table
|
|
del Productions[:]
|
|
for p in _lr_productions:
|
|
if not p:
|
|
Productions.append(None)
|
|
else:
|
|
m = MiniProduction()
|
|
m.name = p[0]
|
|
m.len = p[1]
|
|
m.file = p[3]
|
|
m.line = p[4]
|
|
if p[2]:
|
|
m.func = ldict[p[2]]
|
|
Productions.append(m)
|
|
|
|
else:
|
|
# Get the tokens map
|
|
tokens = ldict.get("tokens",None)
|
|
|
|
if not tokens:
|
|
raise YaccError,"module does not define a list 'tokens'"
|
|
if not (isinstance(tokens,types.ListType) or isinstance(tokens,types.TupleType)):
|
|
raise YaccError,"tokens must be a list or tuple."
|
|
|
|
# Check to see if a requires dictionary is defined.
|
|
requires = ldict.get("require",None)
|
|
if requires:
|
|
if not (isinstance(requires,types.DictType)):
|
|
raise YaccError,"require must be a dictionary."
|
|
|
|
for r,v in requires.items():
|
|
try:
|
|
if not (isinstance(v,types.ListType)):
|
|
raise TypeError
|
|
v1 = [x.split(".") for x in v]
|
|
Requires[r] = v1
|
|
except StandardError:
|
|
print "Invalid specification for rule '%s' in require. Expected a list of strings" % r
|
|
|
|
|
|
# Build the dictionary of terminals. We a record a 0 in the
|
|
# dictionary to track whether or not a terminal is actually
|
|
# used in the grammar
|
|
|
|
if 'error' in tokens:
|
|
print "yacc: Illegal token 'error'. Is a reserved word."
|
|
raise YaccError,"Illegal token name"
|
|
|
|
for n in tokens:
|
|
if Terminals.has_key(n):
|
|
print "yacc: Warning. Token '%s' multiply defined." % n
|
|
Terminals[n] = [ ]
|
|
|
|
Terminals['error'] = [ ]
|
|
|
|
# Get the precedence map (if any)
|
|
prec = ldict.get("precedence",None)
|
|
if prec:
|
|
if not (isinstance(prec,types.ListType) or isinstance(prec,types.TupleType)):
|
|
raise YaccError,"precedence must be a list or tuple."
|
|
add_precedence(prec)
|
|
Signature.update(repr(prec))
|
|
|
|
for n in tokens:
|
|
if not Precedence.has_key(n):
|
|
Precedence[n] = ('right',0) # Default, right associative, 0 precedence
|
|
|
|
# Look for error handler
|
|
ef = ldict.get('p_error',None)
|
|
if ef:
|
|
if not isinstance(ef,types.FunctionType):
|
|
raise YaccError,"'p_error' defined, but is not a function."
|
|
eline = ef.func_code.co_firstlineno
|
|
efile = ef.func_code.co_filename
|
|
files[efile] = None
|
|
|
|
if (ef.func_code.co_argcount != 1):
|
|
raise YaccError,"%s:%d: p_error() requires 1 argument." % (efile,eline)
|
|
global Errorfunc
|
|
Errorfunc = ef
|
|
else:
|
|
print "yacc: Warning. no p_error() function is defined."
|
|
|
|
# Get the list of built-in functions with p_ prefix
|
|
symbols = [ldict[f] for f in ldict.keys()
|
|
if (isinstance(ldict[f],types.FunctionType) and ldict[f].__name__[:2] == 'p_'
|
|
and ldict[f].__name__ != 'p_error')]
|
|
|
|
# Check for non-empty symbols
|
|
if len(symbols) == 0:
|
|
raise YaccError,"no rules of the form p_rulename are defined."
|
|
|
|
# Sort the symbols by line number
|
|
symbols.sort(lambda x,y: cmp(x.func_code.co_firstlineno,y.func_code.co_firstlineno))
|
|
|
|
# Add all of the symbols to the grammar
|
|
for f in symbols:
|
|
if (add_function(f)) < 0:
|
|
error += 1
|
|
else:
|
|
files[f.func_code.co_filename] = None
|
|
|
|
# Make a signature of the docstrings
|
|
for f in symbols:
|
|
if f.__doc__:
|
|
Signature.update(f.__doc__)
|
|
|
|
lr_init_vars()
|
|
|
|
if error:
|
|
raise YaccError,"Unable to construct parser."
|
|
|
|
if not lr_read_tables(tabmodule):
|
|
|
|
# Validate files
|
|
for filename in files.keys():
|
|
if not validate_file(filename):
|
|
error = 1
|
|
|
|
# Validate dictionary
|
|
validate_dict(ldict)
|
|
|
|
if start and not Prodnames.has_key(start):
|
|
raise YaccError,"Bad starting symbol '%s'" % start
|
|
|
|
augment_grammar(start)
|
|
error = verify_productions(cycle_check=check_recursion)
|
|
otherfunc = [ldict[f] for f in ldict.keys()
|
|
if (isinstance(ldict[f],types.FunctionType) and ldict[f].__name__[:2] != 'p_')]
|
|
|
|
if error:
|
|
raise YaccError,"Unable to construct parser."
|
|
|
|
build_lritems()
|
|
compute_first1()
|
|
compute_follow(start)
|
|
|
|
if method == 'SLR':
|
|
slr_parse_table()
|
|
elif method == 'LALR1':
|
|
lalr_parse_table()
|
|
return
|
|
else:
|
|
raise YaccError, "Unknown parsing method '%s'" % method
|
|
|
|
lr_write_tables(tabmodule)
|
|
|
|
if yaccdebug:
|
|
try:
|
|
f = open(debug_file,"w")
|
|
f.write(_vfc.getvalue())
|
|
f.write("\n\n")
|
|
f.write(_vf.getvalue())
|
|
f.close()
|
|
except IOError,e:
|
|
print "yacc: can't create '%s'" % debug_file,e
|
|
|
|
# Made it here. Create a parser object and set up its internal state.
|
|
# Set global parse() method to bound method of parser object.
|
|
|
|
p = Parser("xyzzy")
|
|
p.productions = Productions
|
|
p.errorfunc = Errorfunc
|
|
p.action = _lr_action
|
|
p.goto = _lr_goto
|
|
p.method = _lr_method
|
|
p.require = Requires
|
|
|
|
global parse
|
|
parse = p.parse
|
|
|
|
# Clean up all of the globals we created
|
|
if (not optimize):
|
|
yacc_cleanup()
|
|
return p
|
|
|
|
# yacc_cleanup function. Delete all of the global variables
|
|
# used during table construction
|
|
|
|
def yacc_cleanup():
|
|
global _lr_action, _lr_goto, _lr_method, _lr_goto_cache
|
|
del _lr_action, _lr_goto, _lr_method, _lr_goto_cache
|
|
|
|
global Productions, Prodnames, Prodmap, Terminals
|
|
global Nonterminals, First, Follow, Precedence, LRitems
|
|
global Errorfunc, Signature, Requires
|
|
|
|
del Productions, Prodnames, Prodmap, Terminals
|
|
del Nonterminals, First, Follow, Precedence, LRitems
|
|
del Errorfunc, Signature, Requires
|
|
|
|
global _vf, _vfc
|
|
del _vf, _vfc
|
|
|
|
|
|
# Stub that raises an error if parsing is attempted without first calling yacc()
|
|
def parse(*args,**kwargs):
|
|
raise YaccError, "yacc: No parser built with yacc()"
|
|
|