forked from dabeaz/ply
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further refinement of table handling with packages. More unit tests.
- Loading branch information
Showing
20 changed files
with
470 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
It is compatible with both Python 2 and Python 3. | ||
""", | ||
license="""BSD""", | ||
version = "3.5", | ||
version = "3.6", | ||
author = "David Beazley", | ||
author_email = "[email protected]", | ||
maintainer = "David Beazley", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Tests proper handling of lextab and parsetab files in package structures | ||
|
||
# Here for testing purposes | ||
import sys | ||
if '..' not in sys.path: | ||
sys.path.insert(0, '..') | ||
|
||
from .parsing.calcparse import parser | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# ----------------------------------------------------------------------------- | ||
# calclex.py | ||
# ----------------------------------------------------------------------------- | ||
|
||
import ply.lex as lex | ||
|
||
tokens = ( | ||
'NAME','NUMBER', | ||
'PLUS','MINUS','TIMES','DIVIDE','EQUALS', | ||
'LPAREN','RPAREN', | ||
) | ||
|
||
# Tokens | ||
|
||
t_PLUS = r'\+' | ||
t_MINUS = r'-' | ||
t_TIMES = r'\*' | ||
t_DIVIDE = r'/' | ||
t_EQUALS = r'=' | ||
t_LPAREN = r'\(' | ||
t_RPAREN = r'\)' | ||
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' | ||
|
||
def t_NUMBER(t): | ||
r'\d+' | ||
try: | ||
t.value = int(t.value) | ||
except ValueError: | ||
print("Integer value too large %s" % t.value) | ||
t.value = 0 | ||
return t | ||
|
||
t_ignore = " \t" | ||
|
||
def t_newline(t): | ||
r'\n+' | ||
t.lexer.lineno += t.value.count("\n") | ||
|
||
def t_error(t): | ||
print("Illegal character '%s'" % t.value[0]) | ||
t.lexer.skip(1) | ||
|
||
# Build the lexer | ||
lexer = lex.lex(optimize=True) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# ----------------------------------------------------------------------------- | ||
# yacc_simple.py | ||
# | ||
# A simple, properly specifier grammar | ||
# ----------------------------------------------------------------------------- | ||
|
||
from .calclex import tokens | ||
from ply import yacc | ||
|
||
# Parsing rules | ||
precedence = ( | ||
('left','PLUS','MINUS'), | ||
('left','TIMES','DIVIDE'), | ||
('right','UMINUS'), | ||
) | ||
|
||
# dictionary of names | ||
names = { } | ||
|
||
def p_statement_assign(t): | ||
'statement : NAME EQUALS expression' | ||
names[t[1]] = t[3] | ||
|
||
def p_statement_expr(t): | ||
'statement : expression' | ||
t[0] = t[1] | ||
|
||
def p_expression_binop(t): | ||
'''expression : expression PLUS expression | ||
| expression MINUS expression | ||
| expression TIMES expression | ||
| expression DIVIDE expression''' | ||
if t[2] == '+' : t[0] = t[1] + t[3] | ||
elif t[2] == '-': t[0] = t[1] - t[3] | ||
elif t[2] == '*': t[0] = t[1] * t[3] | ||
elif t[2] == '/': t[0] = t[1] / t[3] | ||
|
||
def p_expression_uminus(t): | ||
'expression : MINUS expression %prec UMINUS' | ||
t[0] = -t[2] | ||
|
||
def p_expression_group(t): | ||
'expression : LPAREN expression RPAREN' | ||
t[0] = t[2] | ||
|
||
def p_expression_number(t): | ||
'expression : NUMBER' | ||
t[0] = t[1] | ||
|
||
def p_expression_name(t): | ||
'expression : NAME' | ||
try: | ||
t[0] = names[t[1]] | ||
except LookupError: | ||
print("Undefined name '%s'" % t[1]) | ||
t[0] = 0 | ||
|
||
def p_error(t): | ||
print("Syntax error at '%s'" % t.value) | ||
|
||
parser = yacc.yacc() | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Tests proper handling of lextab and parsetab files in package structures | ||
|
||
# Here for testing purposes | ||
import sys | ||
if '..' not in sys.path: | ||
sys.path.insert(0, '..') | ||
|
||
from .parsing.calcparse import parser | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# ----------------------------------------------------------------------------- | ||
# calclex.py | ||
# ----------------------------------------------------------------------------- | ||
|
||
import ply.lex as lex | ||
|
||
tokens = ( | ||
'NAME','NUMBER', | ||
'PLUS','MINUS','TIMES','DIVIDE','EQUALS', | ||
'LPAREN','RPAREN', | ||
) | ||
|
||
# Tokens | ||
|
||
t_PLUS = r'\+' | ||
t_MINUS = r'-' | ||
t_TIMES = r'\*' | ||
t_DIVIDE = r'/' | ||
t_EQUALS = r'=' | ||
t_LPAREN = r'\(' | ||
t_RPAREN = r'\)' | ||
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' | ||
|
||
def t_NUMBER(t): | ||
r'\d+' | ||
try: | ||
t.value = int(t.value) | ||
except ValueError: | ||
print("Integer value too large %s" % t.value) | ||
t.value = 0 | ||
return t | ||
|
||
t_ignore = " \t" | ||
|
||
def t_newline(t): | ||
r'\n+' | ||
t.lexer.lineno += t.value.count("\n") | ||
|
||
def t_error(t): | ||
print("Illegal character '%s'" % t.value[0]) | ||
t.lexer.skip(1) | ||
|
||
# Build the lexer | ||
lexer = lex.lex(optimize=True, lextab='calclextab') | ||
|
||
|
||
|
Oops, something went wrong.