Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Parsing Expression Grammar Support for C# 3.0 Part 1 - PEG Lib and Parser Generator

, 7 Oct 2008 CPOL
Introduction to the parsing method PEG with library and parser generator
PEG_GrammarExplorer.zip
PEG_GrammarExplorer
Documents
PEG_GrammarExplorer_fromCP-Dateien
PEG_GrammerExplorer.jpg
mssccprj.scc
PEG Explorer
mssccprj.scc
PEG Explorer.csproj.user
Properties
Settings.settings
vssver2.scc
vssver2.scc
PegBase
mssccprj.scc
Properties
vssver2.scc
vssver2.scc
PegSamples
BasicEncodingRules
input
CDURKR2URKR125195
DefiniteLengthForm
CDURKR2URKR125195
hello
TDAUTPTEUR0100011.tap3
vssver2.scc
hello
IndefiniteLengthForm
DefiniteLengthForm
TDAUTPTEUR0100011.tap3
TDAUTPTEUR0100011.tap3
TDAUTPTEUR0100011.tap3
TDAUTPTEUR0100011_withError.tap3
vssver2.scc
vssver2.scc
calc0_direct
input
vssver2.scc
vssver2.scc
calc0_tree
input
vssver2.scc
vssver2.scc
CSharp3
docu
input
vssver2.scc
vssver2.scc
C_KernighanRitchie2
input
vssver2.scc
vssver2.scc
EMail
input
vssver2.scc
vssver2.scc
Json
input
vssver2.scc
peg_template
vssver2.scc
vssver2.scc
mssccprj.scc
PEG Samples.csproj.user
PegGenerator
input
C#
TestCases
C#
.cs
vssver2.scc
vssver2.scc
vssver2.scc
vssver2.scc
Properties
vssver2.scc
python_2_5_2
input
adwords
awapi_python_samples_1.0.0
src
decoratorators_01
vssver2.scc
Problems
Sample PEG Console Parser
PEG Console Parser
input
Properties
Sample PEG Console Parser.csproj.user
vssver2.scc
PEG_GrammarExplorer_Submission.zip
PEG_GrammarExplorer_Submission
PEG_GrammerExplorer.jpg
PEG_GrammerExplorer.zip
# fortune.py -- chooses a random fortune, as the fortune(8) program in
#               the BSD-games package does

import struct, whrandom, string

# C long variables are different sizes on 32-bit and 64-bit machines,
# so we have to measure how big they are on the machine where this is running.
LONG_SIZE = struct.calcsize('L') 
is_64_bit = (LONG_SIZE == 8)

def get(filename):
    "Select a random quotation, using a pregenerated .dat file"

    # First, we open the .dat file, and read the header information.
    # The C structure containing this info looks like:
    ## typedef struct {				/* information table */
    ## #define	VERSION		1
    ## 	unsigned long	str_version;		/* version number */
    ## 	unsigned long	str_numstr;		/* # of strings in the file */
    ## 	unsigned long	str_longlen;		/* length of longest string */
    ## 	unsigned long	str_shortlen;		/* length of shortest string */
    ## #define	STR_RANDOM	0x1		/* randomized pointers */
    ## #define	STR_ORDERED	0x2		/* ordered pointers */
    ## #define	STR_ROTATED	0x4		/* rot-13'd text */
    ## 	unsigned long	str_flags;		/* bit field for flags */
    ## 	unsigned char	stuff[4];		/* long aligned space */
    ## #define	str_delim	stuff[0]	/* delimiting character */
    ## } STRFILE;

    datfile = open(filename+'.dat', 'r')
    data = datfile.read(5 * LONG_SIZE)
    if is_64_bit:
	v1, v2, n1, n2, l1, l2, s1, s2, f1, f2 = struct.unpack('!10L', data)
	version  = v1 + (v2 << 32)
	numstr   = n1 + (n2 << 32)
	longlen  = l1 + (l2 << 32)
	shortlen = s1 + (s2 << 32)
	flags    = f1 + (f2 << 32)
    else:
	version, numstr, longlen, shortlen, flags = struct.unpack('5l', data)

    delimiter = datfile.read(1) 
    datfile.read(3)			# Throw away padding bytes
    if is_64_bit: datfile.read(4)       # 64-bit machines align to 8 bytes

    # Pick a random number
    r = whrandom.randint(0, numstr)
    datfile.seek(LONG_SIZE * r, 1)	# Seek to the chosen pointer
    data = datfile.read(LONG_SIZE * 2)

    if is_64_bit:
	s1, s2, e1, e2 = struct.unpack('!4L', data)
	start, end = s1 + (s2 << 32), e1 + (e2 << 32)
    else:
	start, end = struct.unpack('!ll', data)
    datfile.close()

    file = open(filename, 'r')
    file.seek(start)
    quotation = file.read(end-start)
    L=string.split(quotation, '\n')
    while string.strip(L[-1]) == delimiter or string.strip(L[-1]) == "": 
	L=L[:-1]
    return string.join(L, '\n')

if __name__ == '__main__':
    import sys
    if len(sys.argv) == 1:
	print 'Usage: fortune.py <filename>'
	sys.exit()
    print get(sys.argv[1])



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Martin.Holzherr

Switzerland Switzerland
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 7 Oct 2008
Article Copyright 2008 by Martin.Holzherr
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid