Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed some shift/reduce conflicts.

  • Loading branch information...
commit 2a96fda9e2022d85bddbcaff0b693c52258ddf08 1 parent ccad1b5
@uberj authored
Showing with 26 additions and 21 deletions.
  1. +10 −0 README.mkd
  2. +16 −21 invparse.py
View
10 README.mkd
@@ -2,3 +2,13 @@ Requirements:
```
ply
```
+
+WARNING: shift/reduce conflict for AND in state 21 resolved as shift
+WARNING: shift/reduce conflict for OR in state 21 resolved as shift
+WARNING: shift/reduce conflict for NOT in state 21 resolved as shift
+WARNING: shift/reduce conflict for LPAREN in state 21 resolved as shift
+WARNING: shift/reduce conflict for DIRECTIVE in state 21 resolved as shift
+WARNING: shift/reduce conflict for TEXT in state 21 resolved as shift
+WARNING: shift/reduce conflict for RE in state 21 resolved as shift
+
+These are OK. The parser doesn't know whether to reduce an ``expression OR expression`` and decides to shift's the OR statement onto the stack. This is what we want because a space between two terms implies and AND, and OR is always the last thing you should do.
View
37 invparse.py
@@ -28,6 +28,8 @@ def __repr__(self):
precedence = (
('left', 'AND'),
+ ('left', 'IMPAND'),
+ ('left', 'PAREN'),
('right', 'NOT'),
)
@@ -35,20 +37,25 @@ def p_statement_expr(p):
'statement : expression'
p[0] = p[1]
-def p_expression_expr_factor(p):
- 'expression : expression factor'
+def p_expression_expr_term(p):
+ 'expression : expression term %prec IMPAND'
p[0] = BOP('AND', p[1], p[2])
-def p_expression_factor(p):
- 'expression : factor'
+def p_expression_term(p):
+ 'expression : term'
p[0] = p[1]
+def p_expression_uminus(p):
+ 'expression : NOT expression'
+ p[0] = UOP('NOT', p[2])
+
+
def p_expression_binop(p):
- '''expression : expression AND expression
+ '''expression : expression OR expression
| expression NOT expression
- | expression OR expression
- | LPAREN expression RPAREN expression
- | expression LPAREN expression RPAREN'''
+ | expression AND expression
+ | LPAREN expression RPAREN expression %prec PAREN
+ | expression LPAREN expression RPAREN %prec PAREN'''
if p[2] == 'NOT':
p3_invert = UOP('NOT', p[3])
p[0] = BOP('AND', p[1], p3_invert)
@@ -60,23 +67,11 @@ def p_expression_binop(p):
p[0] = BOP('AND', p[1], p[3])
-def p_expression_uminus(p):
- 'expression : NOT expression'
- p[0] = UOP('NOT', p[2])
-
def p_expression_group(p):
- 'expression : LPAREN expression RPAREN'
+ 'expression : LPAREN expression RPAREN %prec PAREN'
p[0] = p[2]
-def p_factor(p):
- '''factor : NOT term
- | term'''
- if len(p) == 3:
- p[0] = UOP('NOT', p[2])
- else:
- p[0] = p[1]
-
def p_term_DIRECTIVE(p):
'term : DIRECTIVE'
p[0] = p[1]
Please sign in to comment.
Something went wrong with that request. Please try again.