ebnftest.py :  » Parser » pyparsing » pyparsing-1.5.2 » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Parser » pyparsing 
pyparsing » pyparsing 1.5.2 » examples » ebnftest.py
print 'Importing pyparsing...'
from pyparsing import *

print 'Constructing EBNF parser with pyparsing...'
import ebnf
import sets


grammar = '''
syntax = (syntax_rule), {(syntax_rule)};
syntax_rule = meta_identifier, '=', definitions_list, ';';
definitions_list = single_definition, {'|', single_definition};
single_definition = syntactic_term, {',', syntactic_term};
syntactic_term = syntactic_factor,['-', syntactic_factor];
syntactic_factor = [integer, '*'], syntactic_primary;
syntactic_primary = optional_sequence | repeated_sequence |
  grouped_sequence | meta_identifier | terminal_string;
optional_sequence = '[', definitions_list, ']';
repeated_sequence = '{', definitions_list, '}';
grouped_sequence = '(', definitions_list, ')';
(* 
terminal_string = "'", character - "'", {character - "'"}, "'" |
  '"', character - '"', {character - '"'}, '"';
 meta_identifier = letter, {letter | digit};
integer = digit, {digit}; 
*)
'''

table = {}
#~ table['character'] = Word(printables, exact=1)
#~ table['letter'] = Word(alphas + '_', exact=1)
#~ table['digit'] = Word(nums, exact=1)
table['terminal_string'] = sglQuotedString
table['meta_identifier'] = Word(alphas+"_", alphas+"_"+nums)
table['integer'] = Word(nums)

print 'Parsing EBNF grammar with EBNF parser...'
parsers = ebnf.parse(grammar, table)
ebnf_parser = parsers['syntax']

commentcharcount = 0
commentlocs = sets.Set()
def tallyCommentChars(s,l,t):
    global commentcharcount,commentlocs
    # only count this comment if we haven't seen it before
    if l not in commentlocs:
        charCount = ( len(t[0]) - len(filter(str.isspace, t[0])) )
        commentcharcount += charCount
        commentlocs.add(l)
    return l,t

#ordinarily, these lines wouldn't be necessary, but we are doing extra stuff with the comment expression
ebnf.ebnfComment.setParseAction( tallyCommentChars )
ebnf_parser.ignore( ebnf.ebnfComment )

print 'Parsing EBNF grammar with generated EBNF parser...\n'
parsed_chars = ebnf_parser.parseString(grammar)
parsed_char_len = len(parsed_chars)

print "],\n".join(str( parsed_chars.asList() ).split("],"))

#~ grammar_length = len(grammar) - len(filter(str.isspace, grammar))-commentcharcount

#~ assert parsed_char_len == grammar_length

print 'Ok!'
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.