Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Add the parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmbaturin committed Aug 22, 2014
1 parent e49f2fb commit 806145f
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions parser.py
@@ -0,0 +1,62 @@
#!/usr/bin/env python
import sys
import pprint
import ply.yacc as yacc

from lexer import tokens

def p_config(p):
''' config : sections '''
p[0] = p[1]

def p_sections(p):
''' sections : sections section
| section
'''
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[2]]

def p_section(p):
''' section : SECTION IDENTIFIER LBRACE statements RBRACE '''
p[0] = { 'name': p[2], 'content': p[4] }

def p_statements(p):
''' statements : statements statement
| statement
'''
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[2]]

def p_statement(p):
''' statement : IDENTIFIER EQU value SEMI '''
p[0] = {'name': p[1], 'value': p[3]}

def p_value(p):
''' value : IDENTIFIER
| STRING
| boolean
'''
p[0] = p[1]

def p_boolean(p):
''' boolean : TRUE
| FALSE
'''
p[0] = p[1]

start = 'config'

filename = sys.argv[1]

parser = yacc.yacc()

pp = pprint.PrettyPrinter(indent=4)

with open(filename, 'r') as f:
input = f.read()
pp.pprint(parser.parse(input))

0 comments on commit 806145f

Please sign in to comment.