summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2017-04-23 03:19:05 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2017-04-23 03:19:05 +0000
commitd56dee3d3cc7e2d8f9ee05edcdba1a659d449216 (patch)
treeae9c8a56879e5f627a5224dcfd904c5857682e2e
parent754bb208f2484f2eefd53f15a2f5ea76e9cb6c17 (diff)
downloadpyparsing-d56dee3d3cc7e2d8f9ee05edcdba1a659d449216.tar.gz
Update oc.py example: fix regex for '==' operator, add packrat parsing and function call as expression operand
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@462 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/examples/oc.py10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/examples/oc.py b/src/examples/oc.py
index 42b9d48..cf656ec 100644
--- a/src/examples/oc.py
+++ b/src/examples/oc.py
@@ -71,6 +71,7 @@ The following is a description of the OC grammar:
"""
from pyparsing import *
+ParserElement.enablePackrat()
LPAR,RPAR,LBRACK,RBRACK,LBRACE,RBRACE,SEMI,COMMA = map(Suppress, "()[]{};,")
INT, CHAR, WHILE, DO, IF, ELSE, RETURN = map(Keyword,
@@ -82,10 +83,10 @@ char = Regex(r"'.'")
string_ = dblQuotedString
TYPE = Group((INT | CHAR) + ZeroOrMore("*"))
-
expr = Forward()
-operand = NAME | integer | char | string_
-expr << (infixNotation(operand,
+func_call = Group(NAME + LPAR + Group(Optional(delimitedList(expr))) + RPAR)
+operand = func_call | NAME | integer | char | string_
+expr <<= (infixNotation(operand,
[
(oneOf('! - *'), 1, opAssoc.RIGHT),
(oneOf('++ --'), 1, opAssoc.RIGHT),
@@ -93,7 +94,7 @@ expr << (infixNotation(operand,
(oneOf('* / %'), 2, opAssoc.LEFT),
(oneOf('+ -'), 2, opAssoc.LEFT),
(oneOf('< == > <= >= !='), 2, opAssoc.LEFT),
- (Regex(r'=[^=]'), 2, opAssoc.LEFT),
+ (Regex(r'(?<!=)=(?!=)'), 2, opAssoc.LEFT),
]) +
Optional( LBRACK + expr + RBRACK |
LPAR + Group(Optional(delimitedList(expr))) + RPAR )
@@ -176,6 +177,7 @@ main()
{
int i;
i = 0;
+ if(a() == 1){}
while(i < 10)
facpr(i++);
return 0;