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

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

, 7 Oct 2008
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
def comp2(n):
    "Constrain n to the range of a 32-bit 2s-complement number"
    if not (-pow(2,31) <=n < pow(2,31)): 
	if n>0: n = -pow(2,31) + (n-pow(2,31))
	else:   n = pow(2,31) + n + pow(2,31) 
    return n

class SSEM:
    def __init__(self):
	# There are 32 32-bit words which make up the memory
	self.store = [0]*32
	# Zero the accumulator and the program counter
	self.accum = self.CI = 0
	self.stopped = 0

    def step(self):
	"""Perform a single instruction, updating the store and registers."""

	if self.stopped: return  # Do nothing when the machine is stopped
	# 2.3: "The CI is always incremented prior to fetching an
	#       instruction for execution..."
	self.CI = comp2( (self.CI + 1) )

	# Fetch the instruction
	inst = self.store[ self.CI & 31]

	# Decode the line number affected by the instruction, and the
	# function number
	lineno, funcno = inst & 31, (inst >> 13) & 7

	assert 0<= funcno <=7
	if funcno == 0:
	    # s,C : JMP : Copy content of Store line to CI
	    self.CI = self.store[ lineno ]
	elif funcno == 1:
	    # c+s,C : JRP : Add content of Store line to CI
	    self.CI = comp2(self.CI + self.store[ lineno ])
	elif funcno == 2:
	    # -s,A : LDN : Copy content of Store line, negated, to accum
	    self.accum = comp2 (- self.store[ lineno ])
	elif funcno == 3:
	    # a,S : STO : Copy content of acc. to Store line
	    self.store[ lineno ] = self.accum
	elif funcno == 4 or funcno==5:
	    # a-s,A : SUB : Subtract content of Store line from accum
	    self.accum = comp2( self.accum - self.store[ lineno ] )
	elif funcno == 6:
	    # Test : CMP : Skip next instruction if content of accum
	    #              is negative
	    if self.accum < 0: self.CI = comp2(self.CI + 1)
	elif funcno == 7:
	    # Stop : STOP : Light "Stop" neon and halt the machine
	    self.stopped = 1
	
	# Assertions to test invariants
	assert -pow(2,31) <= self.accum <pow(2,31)
	assert -pow(2,31) <= self.store[ lineno ] <pow(2,31)
	assert -pow(2,31) <= self.CI <pow(2,31)



import re, string
inst_pat = re.compile("""\s*
      (?P<mnemonic>JMP|JRP|LDN|STOP|STO|SUB|CMP)  
       \s* 
      (?P<arg>\d+)        # numeric argument""", re.VERBOSE | re.IGNORECASE)

# There are two variants of the SUB instruction; SUB has an opcode of
# 4, and SUBF has an opcode of 5.  Both have the exact same effects,
# and differ only in their opcode.

opcode = {'JMP':0, 'JRP':1, 'LDN':2, 'STO':3, 'SUB':4, 'SUBF':5,
	  'CMP':6, 'STOP':7 }
	  

def assemble(inst_list):
    if len(inst_list)>32:
	raise ValueError, "Only room for 32 instructions"
    machine = SSEM() 

    for i in range(len(inst_list)):
	m = inst_pat.match(string.upper(inst_list[i]))
	if not m: raise ValueError, "Error in assembly code: " + inst_list[i]
	mnemonic, arg = m.group('mnemonic'), m.group('arg')	
	arg = string.atoi(arg)
	machine.store[ i ] = arg | (opcode[mnemonic]  << 13)
    return machine

if __name__ == '__main__':
    # Try the addition test
    p=pow(2,31)

    assert (comp2(p-1), comp2(p), comp2(p+1)) == (p-1, -p, -p+1)
    assert (comp2(-p), comp2(-p-1), comp2(-p-2)) == (-p, p-1, p-2)

    m=assemble(['JMP 0', 
		'LDN 28', 
		'SUB 29', 
		'STO 30', 
		'LDN 30', 
		'STO 30', 
		'STOP 0'])
    x = p-1 ; y = -p
    m.store[28] = x ; m.store[29] = y
    while not m.stopped:
	m.step()
    assert m.store[30] == comp2(x+y)
	     

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 | Mobile
Web03 | 2.8.140916.1 | Last Updated 7 Oct 2008
Article Copyright 2008 by Martin.Holzherr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid