def getGrammar(filename): from dparser import Parser gramgram = open( os.path.join(os.path.dirname(__file__), 'grammargrammar.txt')).read() d_parse_grammar.__doc__ = gramgram parser = Parser() return parser.parse(open(filename).read()).getStructure()
def parse_expr(x): global dom impl = getDOMImplementation() dom = impl.createDocument(None, "daexml", None) y = Parser().parse(x,start_symbol='expression') # y is now a dparser.ParsedStructure instance z = y.getStructure() return z
def daestring2daexml(filedata, startsymbol): global dom impl = getDOMImplementation() dom = impl.createDocument(None, "daexml", None) try: y = Parser().parse(filedata,start_symbol=startsymbol) # y is now a dparser.ParsedStructure instance # except Exception, e: except SyntaxError, e: print e # print sys.exc_info()[0] print 'DAE Parse Error: Unable to handle this model currently' sys.exit(-1)
def main(args): data = Pexel(args.filename) data.df.fillna('', inplace=True) # Check that all required columns exist in the source file if not all(col in data.df.columns for col in ['Cat', 'Artist', 'Album']): sys.exit("Error: File must have columns 'Cat', 'Artist' and 'Album'") parser = Parser() if not args.no_urls: find_urls(data, parser) if not args.no_details: find_details(data, parser) data.save() print('Done!')
# turn a tree of strings into a single string (slowly): def stringify(s): if type(s) == str: return s out = '' for c in s: out += stringify(c) return out def d_add1(t, s): "add : add '%' exp" s[1] = '+ ' # replace the % with + def d_add2(t, s): "add : exp" def d_exp(t): 'exp : "[0-9]+" ' # if the start action specifies the 's' argument, then parser # will contain a member, s, parser = Parser() parsedmessage = parser.parse('1 % 2 % 3') if stringify(parsedmessage.getStringLeft()) != '1 + 2 + 3': print 'error'
global indent_level, start_column if loc.s < len(loc.buf): print('WHITESPACE', loc.s, len(loc.buf), repr(chr(loc.buf[loc.s]))) else: print('WHITESPACE', loc.s, len(loc.buf)) while loc.s < len(loc.buf): c = chr(loc.buf[loc.s]) if c == '\n': start_column = 0 elif c.isspace(): start_column += 1 #TODO Remove # to end of line else: # c is not whitespace, so start_column is new indent level return loc.s += 1 p = Parser() print(dir(p)) p.inserted_thing = 'YAH' #print(p.parse('2+3+4', initial_skip_space_fn=whitespace).getStructure()) #print() s = open('numbo3.farg', 'r').read() x = p.parse(s, initial_skip_space_fn=whitespace) print(x.getStructure()) generate_code(x.getStructure()) #print() #y = p.parse(" blah\nx\n", initial_skip_space_fn=whitespace)
def parse(s): parser = Parser() return parser.parse(s, print_debug_info=0).getStructure()
#!/usr/bin/env python2.3 "Identify sequence of a-, b-, c- words" # #-- The grammar def d_phrase(t, s): 'phrase : words ( ABC | AB | A ) words' print "Head:", ''.join(s[0]) print t[1][0]+":", ''.join(s[1]) print "Tail:", ''.join(s[2]) def d_words(t): 'words : word*' def d_word(t): 'word : "[a-z]+" ' def d_A(t): '''A : "a[a-z]*" ''' return 'A' def d_AB(t): '''AB : A "b[a-z]*" ''' return 'AB' def d_ABC(t): '''ABC : AB "c[a-z]*" ''' return 'ABC' # #-- Parse STDIN from dparser import Parser from sys import argv, stdin phrase, arg = stdin.read(), argv[-1] Parser().parse(phrase, print_debug_info=(arg=='--debug'))
def d_number1(t): '''number1 : number''' return t[0] def d_number2(t): '''number2 : number''' return t[0] def d_number(t): '''number : "[0-9]+"''' return t[0] def ambiguity_func(v): return v[0] def d_whitespace(t, spec): "whitespace : ' '*" del t, spec if Parser().parse('1 +2* (3+ 4+5)', ambiguity_fn=ambiguity_func, print_debug_info=0).getStructure() != 25: print('fail')
# and leaving whitespace intact. from dparser import Parser # turn a tree of strings into a single string (slowly): def stringify(s): if type(s) == str: return s out = '' for c in s: out += stringify(c) return out def d_add1(t, s): "add : add '%' exp" s[1] = '+ ' # replace the % with + def d_add2(t, s): "add : exp" def d_exp(t): 'exp : "[0-9]+" ' # if the start action specifies the 's' argument, then parser # will contain a member, s, parser = Parser() parsedmessage = parser.parse('1 % 2 % 3') if stringify(parsedmessage.getStringLeft()) != '1 + 2 + 3': print 'error'
return t[0] + t[2] def d_add2(t, nodes): '''add : mul''' return nodes[0].user.t def d_mul1(t): '''mul : mul '*' exp''' return t[0] * t[2] def d_mul2(t): '''mul : exp''' return t[0] def d_exp1(t): '''exp : "[0-9]+"''' return int(t[0]) def d_exp2(t): '''exp : '(' add ')' ''' return t[1] if Parser().parse('''3*(3+4)''').getStructure() != 21: print 'fail'
""" infix_op: '+' | '-' | '*' | '/' | '.' """ return t[0] def d_string(t): """ string: "'" "[^']*" "'" """ return ConstString(t[1]) def d_variable(t): """ variable: '$' identifier """ return Variable(t[1]) def d_identifier(t): """ identifier: "[a-zA-Z_][a-zA-Z0-9_]*" """ return Identifier(t[0]) parser = Parser() print parser.parse("(print 'abc')").emit()
# into relevant parts of buf: buf[noun.start_loc.s:noun.end] # 'cat' buf[noun.start_loc.s:] # 'cat flies' buf[noun.end:] # ' flies' buf[noun.end_skip:] # 'flies' # line numbers and columns: noun.start_loc.line noun.start_loc.col # the 'this' argument is the D_ParseNode for this action: buf[this.start_loc.s:this.end] # 'cat flies' # children of a node can also be obtained with the 'c' member: # this.c[0] is the same as nodes[0] def d_noun(t, this): "noun : 'cat'" del this return t[0] def d_verb(t, this): "verb : 'flies'" del this return t[0] Parser().parse('cat flies').getStructure()
def d_S(t): '''S : d '+' d''' return t[0] + t[2] def d_number(t): '''d : "[0-9]+" ''' return int(t[0]) def skip_space(loc): while loc.s < len(loc.buf) and loc.buf[loc.s:loc.s + len('hello')] == 'hello': loc.s = loc.s + len('hello') parser = Parser(make_grammar_file=1) buf = 'hi10hello+3hellohi' if parser.parse( buf, buf_offset=2, partial_parses=1, initial_skip_space_fn=skip_space).getStructure() != 13: print 'fail' buf = '87+5' if parser.parse(buf, initial_skip_space_fn=skip_space).getStructure() != 92: print 'fail'
from dparser import Parser def d_S(t): '''S : d '+' d''' return t[0] + t[2] def d_number(t): '''d : "[0-9]+" ''' return int(t[0]) def skip_space(loc): while loc.s < len(loc.buf) and loc.buf[loc.s:loc.s+len('hello')] == 'hello': loc.s = loc.s + len('hello') parser = Parser(make_grammar_file=1) buf = 'hi10hello+3hellohi' if parser.parse(buf, buf_offset=2, partial_parses=1, initial_skip_space_fn = skip_space).getStructure() != 13: print 'fail' buf = '87+5' if parser.parse(buf, initial_skip_space_fn = skip_space).getStructure() != 92: print 'fail'
# and leaving whitespace intact. from dparser import Parser # turn a tree of strings into a single string (slowly): def stringify(s): if type(s) == str: return s out = '' for c in s: out += stringify(c) return out def d_add1(t, s): "add : add '%' exp" s[1] = '+ ' # replace the % with + def d_add2(t, s): "add : exp" def d_exp(t): 'exp : "[0-9]+" ' # if the start action specifies the 's' argument, then parser # will contain a member, s, parser = Parser() parser.parse('1 % 2 % 3') if stringify(parser.s) != '1 + 2 + 3': print 'error'
from dparser import Parser def d_s(t): "s : a" def d_a(t, spec): "a ::= 'a'" parser = Parser() parser.parse("a")
def parse(s, parser=None): if parser is None: parser = Parser(file_prefix='.d_parser_mach_gen') return parser.parse(preprocessor.preprocess(s)).structure
from dparser import Parser # turn a tree of strings into a single string (slowly): def stringify(s): if not isinstance(s, str): return "".join(map(stringify, s)) return s def d_add1(t, s): "add : add '%' exp" s[1] = "+ " # replace the % with + def d_add2(t, s): "add : exp" def d_exp(t): 'exp : "[0-9]+" ' # if the start action specifies the 's' argument, then parser # will contain a member, s, parser = Parser() parsedmessage = parser.parse("1 % 2 % 3") if stringify(parsedmessage.getStringLeft()) != "1 + 2 + 3": print "error"
def getGrammar(filename): from dparser import Parser gramgram = open(os.path.join(os.path.dirname(__file__), 'grammargrammar.txt')).read() d_parse_grammar.__doc__ = gramgram parser = Parser() return parser.parse(open(filename).read()).getStructure()
def parse(program): parser = Parser(file_prefix='.d_parser_assign') # ohne die ambiguity function gibt es einen segfault return Module(parser.parse(program, ambiguity_fn=lambda a: None).structure)