summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo <hugovk@users.noreply.github.com>2018-03-08 17:19:48 +0200
committerGitHub <noreply@github.com>2018-03-08 17:19:48 +0200
commit063eff1a7886f0bcba7648be5c2b977b1003d357 (patch)
treedca820effca5656c30390311823c8647dea289f4
parent0f398b72618c1564d71f7dc0558e6722b241875a (diff)
downloadply-063eff1a7886f0bcba7648be5c2b977b1003d357.tar.gz
Remove code formatting to make links clickable, add Python formatting to code
-rw-r--r--README.md227
1 files changed, 116 insertions, 111 deletions
diff --git a/README.md b/README.md
index 05df32a..bef5b3b 100644
--- a/README.md
+++ b/README.md
@@ -77,14 +77,18 @@ within the 'ply' directory which may also be used as a Python package.
To use PLY, simply copy the 'ply' directory to your project and import
lex and yacc from the associated 'ply' package. For example:
- import ply.lex as lex
- import ply.yacc as yacc
+```python
+import ply.lex as lex
+import ply.yacc as yacc
+```
Alternatively, you can copy just the files lex.py and yacc.py
individually and use them as modules. For example:
- import lex
- import yacc
+```python
+import lex
+import yacc
+```
The file setup.py can be used to install ply using distutils.
@@ -107,7 +111,7 @@ Resources
=========
More information about PLY can be obtained on the PLY webpage at:
- http://www.dabeaz.com/ply
+* http://www.dabeaz.com/ply
For a detailed overview of parsing theory, consult the excellent
book "Compilers : Principles, Techniques, and Tools" by Aho, Sethi, and
@@ -116,11 +120,11 @@ may also be useful.
The GitHub page for PLY can be found at:
- https://github.com/dabeaz/ply
+* https://github.com/dabeaz/ply
An old and relatively inactive discussion group for PLY is found at:
- http://groups.google.com/group/ply-hack
+* http://groups.google.com/group/ply-hack
Acknowledgments
===============
@@ -147,110 +151,111 @@ Example
Here is a simple example showing a PLY implementation of a calculator
with variables.
- # -----------------------------------------------------------------------------
- # calc.py
- #
- # A simple calculator with variables.
- # -----------------------------------------------------------------------------
-
- 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+'
- t.value = int(t.value)
- return t
-
- # Ignored characters
- 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
- import ply.lex as lex
- lex.lex()
-
- # Precedence rules for the arithmetic operators
- precedence = (
- ('left','PLUS','MINUS'),
- ('left','TIMES','DIVIDE'),
- ('right','UMINUS'),
- )
-
- # dictionary of names (for storing variables)
- names = { }
-
- def p_statement_assign(p):
- 'statement : NAME EQUALS expression'
- names[p[1]] = p[3]
-
- def p_statement_expr(p):
- 'statement : expression'
- print(p[1])
-
- def p_expression_binop(p):
- '''expression : expression PLUS expression
- | expression MINUS expression
- | expression TIMES expression
- | expression DIVIDE expression'''
- if p[2] == '+' : p[0] = p[1] + p[3]
- elif p[2] == '-': p[0] = p[1] - p[3]
- elif p[2] == '*': p[0] = p[1] * p[3]
- elif p[2] == '/': p[0] = p[1] / p[3]
-
- def p_expression_uminus(p):
- 'expression : MINUS expression %prec UMINUS'
- p[0] = -p[2]
-
- def p_expression_group(p):
- 'expression : LPAREN expression RPAREN'
- p[0] = p[2]
-
- def p_expression_number(p):
- 'expression : NUMBER'
- p[0] = p[1]
-
- def p_expression_name(p):
- 'expression : NAME'
- try:
- p[0] = names[p[1]]
- except LookupError:
- print("Undefined name '%s'" % p[1])
- p[0] = 0
-
- def p_error(p):
- print("Syntax error at '%s'" % p.value)
-
- import ply.yacc as yacc
- yacc.yacc()
-
- while True:
- try:
- s = raw_input('calc > ') # use input() on Python 3
- except EOFError:
- break
- yacc.parse(s)
-
+```python
+# -----------------------------------------------------------------------------
+# calc.py
+#
+# A simple calculator with variables.
+# -----------------------------------------------------------------------------
+
+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+'
+ t.value = int(t.value)
+ return t
+
+# Ignored characters
+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
+import ply.lex as lex
+lex.lex()
+
+# Precedence rules for the arithmetic operators
+precedence = (
+ ('left','PLUS','MINUS'),
+ ('left','TIMES','DIVIDE'),
+ ('right','UMINUS'),
+ )
+
+# dictionary of names (for storing variables)
+names = { }
+
+def p_statement_assign(p):
+ 'statement : NAME EQUALS expression'
+ names[p[1]] = p[3]
+
+def p_statement_expr(p):
+ 'statement : expression'
+ print(p[1])
+
+def p_expression_binop(p):
+ '''expression : expression PLUS expression
+ | expression MINUS expression
+ | expression TIMES expression
+ | expression DIVIDE expression'''
+ if p[2] == '+' : p[0] = p[1] + p[3]
+ elif p[2] == '-': p[0] = p[1] - p[3]
+ elif p[2] == '*': p[0] = p[1] * p[3]
+ elif p[2] == '/': p[0] = p[1] / p[3]
+
+def p_expression_uminus(p):
+ 'expression : MINUS expression %prec UMINUS'
+ p[0] = -p[2]
+
+def p_expression_group(p):
+ 'expression : LPAREN expression RPAREN'
+ p[0] = p[2]
+
+def p_expression_number(p):
+ 'expression : NUMBER'
+ p[0] = p[1]
+
+def p_expression_name(p):
+ 'expression : NAME'
+ try:
+ p[0] = names[p[1]]
+ except LookupError:
+ print("Undefined name '%s'" % p[1])
+ p[0] = 0
+
+def p_error(p):
+ print("Syntax error at '%s'" % p.value)
+
+import ply.yacc as yacc
+yacc.yacc()
+
+while True:
+ try:
+ s = raw_input('calc > ') # use input() on Python 3
+ except EOFError:
+ break
+ yacc.parse(s)
+```
Bug Reports and Patches
=======================