forked from dabeaz/ply
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmh_test_syntax3.py
110 lines (74 loc) · 1.7 KB
/
mh_test_syntax3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import ply
import ply.lex as lex
import ply.yacc as yacc
import ply.syntax
reserved = {
'true' : 'TRUE',
'false' : 'FALSE',
'null' : 'NULL',
}
tokens = [
'LSQUARE',
'RSQUARE',
'LPARAN',
'RPARAN',
'COMMA',
'COLON',
'STRING',
'INT',
'ID',
] + list(reserved.values())
t_LSQUARE = r'\['
t_RSQUARE = r'\]'
t_LPARAN = r'\('
t_RPARAN = r'\)'
t_COLON= r':'
t_COMMA= r','
t_ignore = ' \t\n'
#t_LBRACKET = r'\['
#t_RBRACKET = r'\]'
#t_EQUALS = r'='
def t_STRING(t):
r'''".*?"'''
#t.type = reserved.get(t.value,'ID') # Check for reserved words
return t
def t_INT(t):
r'[0-9]+'
t.value = int(t.value)
return t
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reserved.get(t.value,'ID') # Check for reserved words
return t
# Error handling rule
def t_error(t):
print "Illegal character '%s'" % t.value[0]
assert False,'Problem in LEXER'
#t.lexer.skip(1)
def p_func1(p):
''' data := ID ( (LSQUARE INT? RSQUARE) | ID | INT ) (LPARAN INT* RPARAN)? ID'''
#''' data := ID (INT|ID| (LSQUARE RSQUARE) ) ID'''
p[0] = p[1:]
# Error rule for syntax errors
def p_error(p):
print "Syntax error in input!"
assert False
import pprint
from pprint import pprint
test_string = '''hello [] (8 5 6) world '''
test_string = '''hello [] world '''
# Turn our grammar from MBNF to EBNF
ply.syntax.mbnf.rewrite_module_in_bnf(namespace=locals())
lexer = lex.lex(debug=True)
#lexer.input(test_string)
#while 1:
# print lexer.next()
#assert False
parser = yacc.yacc(start='data',debug=True)
print '\n\n'
res = parser.parse(test_string,debug=True)
print '\nFinal results:: '
pprint (res)
print res
print
print '\n\nParsed OK!'