add "unit" top-level parsing target
This commit is contained in:
parent
b8e617bd39
commit
581be4f10d
@ -2,6 +2,8 @@
|
|||||||
class Node(object):
|
class Node(object):
|
||||||
def __init__(self, children=None):
|
def __init__(self, children=None):
|
||||||
self.children = children
|
self.children = children
|
||||||
|
if self.children is None:
|
||||||
|
self.children = []
|
||||||
|
|
||||||
class StatementNode(Node):
|
class StatementNode(Node):
|
||||||
def __init__(self, children=None):
|
def __init__(self, children=None):
|
||||||
@ -15,3 +17,7 @@ class CExprNode(ExprNode):
|
|||||||
def __init__(self, cstring):
|
def __init__(self, cstring):
|
||||||
ExprNode.__init__(self)
|
ExprNode.__init__(self)
|
||||||
self.cstring = cstring
|
self.cstring = cstring
|
||||||
|
|
||||||
|
class UnitNode(Node):
|
||||||
|
def __init__(self, children=None):
|
||||||
|
Node.__init__(self, children)
|
||||||
|
@ -2,6 +2,22 @@
|
|||||||
from lexrules import tokens
|
from lexrules import tokens
|
||||||
from nodes import *
|
from nodes import *
|
||||||
|
|
||||||
|
def p_unit(p):
|
||||||
|
'unit : unit_items'
|
||||||
|
p[0] = UnitNode([p[1]])
|
||||||
|
|
||||||
|
def p_unit_items(p):
|
||||||
|
'unit_items : unit_item unit_items'
|
||||||
|
p[0] = Node([p[1]] + p[2].children)
|
||||||
|
|
||||||
|
def p_unit_items_empty(p):
|
||||||
|
'unit_items : empty'
|
||||||
|
p[0] = p[1]
|
||||||
|
|
||||||
|
def p_unit_item_c_stmt(p):
|
||||||
|
'unit_item : c_expr SEMICOLON'
|
||||||
|
p[0] = p[1]
|
||||||
|
|
||||||
def p_statement(p):
|
def p_statement(p):
|
||||||
'statement : expr SEMICOLON'
|
'statement : expr SEMICOLON'
|
||||||
p[0] = StatementNode([p[1]])
|
p[0] = StatementNode([p[1]])
|
||||||
@ -13,3 +29,7 @@ def p_expr(p):
|
|||||||
def p_c_expr(p):
|
def p_c_expr(p):
|
||||||
'c_expr : C LPAREN STRING RPAREN'
|
'c_expr : C LPAREN STRING RPAREN'
|
||||||
p[0] = CExprNode(p[3])
|
p[0] = CExprNode(p[3])
|
||||||
|
|
||||||
|
def p_empty(p):
|
||||||
|
'empty :'
|
||||||
|
p[0] = Node()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user