Skip to content

Commit

Permalink
in the middle of parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Siwei Yang authored and Siwei Yang committed Jul 6, 2012
0 parents commit 0c3a697
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
*\.idea
*\.iml
*\.pyc
1 change: 1 addition & 0 deletions IDLParser/__init__.py
@@ -0,0 +1 @@
__author__ = 'maluuba'
44 changes: 44 additions & 0 deletions IDLParser/parser.py
@@ -0,0 +1,44 @@
#!/usr/bin/env python
__author__ = 'maluuba'
from scanner import lexer, tokens
from ply.yacc import yacc

def p_assignment(p):
'assignment : identifier ASSIGN intconstant'
p[0] = (p[1], p[3])

def p_type(p):
'''type : Boolean
| Double
| String
| Object
| Set LT type GT
| List LT type GT
| Map LT type COMMA type GT'''
if len(p) == 2:
p[0] = p[1]
elif len(p) == 5:
p[0] = (p[1], p[3])
elif len(p) == 7:
p[0] = (p[1], p[3], p[5])

def p_declaration(p):
'declaration : intconstant SEMICOLON type identifier'
p[0] = (p[1], p[3])

def p_declaration_list(p):
'''declaration_list : declaration
| declaration_list COMMA declaration'''
if len(p) == 2:
p[0] = [p[1]]
if len(p) == 4:
p[0] = p[1] + [p[3]]

def p_error(t):
print("Syntax error at '%s'" % t.value)

start = 'declaration_list'
parser = yacc()

if __name__ == '__main__':
parser.parse('bool')
40 changes: 40 additions & 0 deletions IDLParser/parsetab.py
@@ -0,0 +1,40 @@

# parsetab.py
# This file is automatically generated. Do not edit.
_tabversion = '3.2'

_lr_method = 'LALR'

_lr_signature = '\xbb1\xf9\x879\x04\x8d;g\xbcr\xa4^\xc1\x9dh'

_lr_action_items = {'Map':([4,15,16,17,22,],[6,6,6,6,6,]),'GT':([8,9,10,12,20,21,23,24,25,26,],[-7,-6,-8,-5,23,24,-9,-10,26,-11,]),'Set':([4,15,16,17,22,],[7,7,7,7,7,]),'String':([4,15,16,17,22,],[8,8,8,8,8,]),'SEMICOLON':([1,],[4,]),'intconstant':([0,5,],[1,1,]),'Double':([4,15,16,17,22,],[9,9,9,9,9,]),'Object':([4,15,16,17,22,],[10,10,10,10,10,]),'List':([4,15,16,17,22,],[11,11,11,11,11,]),'LT':([6,7,11,],[15,16,17,]),'Boolean':([4,15,16,17,22,],[12,12,12,12,12,]),'COMMA':([2,3,8,9,10,12,14,18,19,23,24,26,],[5,-3,-7,-6,-8,-5,-4,-2,22,-9,-10,-11,]),'identifier':([8,9,10,12,13,23,24,26,],[-7,-6,-8,-5,18,-9,-10,-11,]),'$end':([2,3,14,18,],[0,-3,-4,-2,]),}

_lr_action = { }
for _k, _v in _lr_action_items.items():
for _x,_y in zip(_v[0],_v[1]):
if not _x in _lr_action: _lr_action[_x] = { }
_lr_action[_x][_k] = _y
del _lr_action_items

_lr_goto_items = {'declaration':([0,5,],[3,14,]),'type':([4,15,16,17,22,],[13,19,20,21,25,]),'declaration_list':([0,],[2,]),}

_lr_goto = { }
for _k, _v in _lr_goto_items.items():
for _x,_y in zip(_v[0],_v[1]):
if not _x in _lr_goto: _lr_goto[_x] = { }
_lr_goto[_x][_k] = _y
del _lr_goto_items
_lr_productions = [
("S' -> declaration_list","S'",1,None,None,None),
('assignment -> identifier ASSIGN intconstant','assignment',3,'p_assignment','<stdin>',2),
('declaration -> intconstant SEMICOLON type identifier','declaration',4,'p_declaration','<stdin>',2),
('declaration_list -> declaration','declaration_list',1,'p_declaration_list','<stdin>',2),
('declaration_list -> declaration_list COMMA declaration','declaration_list',3,'p_declaration_list','<stdin>',3),
('type -> Boolean','type',1,'p_type','<stdin>',2),
('type -> Double','type',1,'p_type','<stdin>',3),
('type -> String','type',1,'p_type','<stdin>',4),
('type -> Object','type',1,'p_type','<stdin>',5),
('type -> Set LT type GT','type',4,'p_type','<stdin>',6),
('type -> List LT type GT','type',4,'p_type','<stdin>',7),
('type -> Map LT type COMMA type GT','type',6,'p_type','<stdin>',8),
]
70 changes: 70 additions & 0 deletions IDLParser/scanner.py
@@ -0,0 +1,70 @@
#!/usr/bin/env python
__author__ = 'maluuba'
from ply.lex import lex

reserved = {
"namespace" : 'namespace',
"persistent" : 'persistent',
"response" : 'response',
"include" : 'include',
"import" : 'import',
"void" : 'void',
"bool" : 'Boolean',
"i32" : 'i32',
"i64" : 'i64',
"double" : 'Double',
"string" : 'String',
"object" : 'Object',
"map" : 'Map',
"list" : 'List',
"set" : 'Set',
"enum" : 'Enum',
"post" : 'post',
"struct" : 'struct',
"service" : 'service',
"identifier" : 'Identifier',
"index" : 'Index',
"range-index" : 'RangeIndex'
}

tokens = list(reserved.values()) + [
'intconstant',
'identifier',
# 'whitespace',
# 'sillycomm',
# 'multicomm',
# 'doctext',
# 'comment',
# 'unixcomment',
'SEMICOLON',
'COMMA',
'ASSIGN',
'GT',
'LT',
# 'symbol'
]

def t_identifier(t):
r'[a-zA-Z_][\-\.a-zA-Z_0-9]*'
t.type = reserved.get(t.value,'identifier')
return t

t_intconstant = r'[+-]?[0-9]+'
#t_whitespace = r'[ \t\r\n]+'
#t_sillycomm = r'"/*""*"*"*/"'
#t_multicomm = r'/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"'
#t_doctext = r'"/**"([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"'
#t_comment = r'//[^\n]*'
#t_unixcomment = r'\#[^\n]*'
t_SEMICOLON = r':'
t_COMMA = r','
t_ASSIGN = r'\='
t_GT = r'>'
t_LT = r'<'
#t_symbol = r'[:;\,\{\}\(\)\=<>\[\]]'

t_ignore = r'[ \t\r\n]+'

lexer = lex()


0 comments on commit 0c3a697

Please sign in to comment.