add "unit" top-level parsing target

This commit is contained in:
Josh Holtrop 2011-08-30 12:02:52 -04:00
parent b8e617bd39
commit 581be4f10d
2 changed files with 26 additions and 0 deletions

View File

@ -2,6 +2,8 @@
class Node(object):
def __init__(self, children=None):
self.children = children
if self.children is None:
self.children = []
class StatementNode(Node):
def __init__(self, children=None):
@ -15,3 +17,7 @@ class CExprNode(ExprNode):
def __init__(self, cstring):
ExprNode.__init__(self)
self.cstring = cstring
class UnitNode(Node):
def __init__(self, children=None):
Node.__init__(self, children)

View File

@ -2,6 +2,22 @@
from lexrules import tokens
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):
'statement : expr SEMICOLON'
p[0] = StatementNode([p[1]])
@ -13,3 +29,7 @@ def p_expr(p):
def p_c_expr(p):
'c_expr : C LPAREN STRING RPAREN'
p[0] = CExprNode(p[3])
def p_empty(p):
'empty :'
p[0] = Node()