diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2017-04-23 03:19:05 +0000 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2017-04-23 03:19:05 +0000 |
commit | 6f7937d30758774a7acd036ff2c15fdce89af543 (patch) | |
tree | ae9c8a56879e5f627a5224dcfd904c5857682e2e | |
parent | 6e4db095917b13fad9e70ab50749c9e8f7fde28f (diff) | |
download | pyparsing-git-6f7937d30758774a7acd036ff2c15fdce89af543.tar.gz |
Update oc.py example: fix regex for '==' operator, add packrat parsing and function call as expression operand
-rw-r--r-- | src/examples/oc.py | 10 |
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;
|