pyparsing - parse scoped variables

103 views Asked by At

So far, I was able to use pyparsing to parse ebnf grammars. However, I wanted to try the following code sample but could not come up with a good grammar.

global radius = 5
DrawCircle(radius)
{
  radius = 10
  DrawCircle(radius)
}
DrawCircle(radius)

The value of radius with in the scope should be 10, 5 otherwise. Any help would be appreciated ?

Regards

Praveen

1

There are 1 answers

1
Praveen Ramanujam On

I was able to get the parser for the above code by running:

enclosed = Forward()
curls = nestedExpr('{', '}', content=enclosed)
enclosed << (OneOrMore(commands | ',' | curls))

I have a follow up question. I am used to write ebnf grammar using http://pyparsing.wikispaces.com/file/view/ebnf.py

Can I get some help identifying the ebnf for forward or equivalent of the above code ? or should I do outside ebnf ?

Regards

Praveen