1
votes

I have to do Lexing and Parsing using PLY. I have gone through http://www.dabeaz.com/ply/ply.html this documentation to learn about PLY and looked at the calculator example. I'm new to all this. So, the step-1 to proceed towards this process is to figure out the grammar rules according to the input expressions.

Some of the sample input expressions for my application will look like are:

1.) [foo, bar, baz] UNION/U ([foo,bar] INTERSECT [bar])
2.) [foo, bar] MINUS/- [bar]
3.) [foo, bar, baz, cat] INTERSECT/∩ [baz, cat] 
4.) [a,b,c,d] UNION/U [d,e,f]

[] (square bracket indicates array/list)

UNION/U, INTESERCT/∩, MINUS/- represent the set operations.

How do i write grammar rules for this kind of expressions involving list and SET operators, so that they will be properly identified as list and set operators when they are sent to lexer of PLY to get converted into tokens?

Please help. I have seen many examples of PLY but everywhere they have normal arithmetic expressions. I really want to understand how do i write them in the way i need.

1
@Joran beasley can you please help?zubug55
PLY grammar syntax is similar to yacc/bison, except for using Python instead of C in the semantic rules. So if you just need examples, you can look for examples in bison, for example.Ove
@Ove can you please point me to the example which does something similar, I really don't know anything about bison.zubug55
Then go through bison tutorials. Parsing (a+b+c) is no different from parsing [foo, bar, baz]. (Sure, the semantic actions may do different things, but that has nothing to do with the parser.) Then you can post here if there's something in particular you're stuck on.Ove

1 Answers

0
votes

I think the grammar rules will look like below:

expression -> expression U expression | expression ∩ expression | 
              expression - expression

expression -> (expression)

expression -> <List>

<List> ::= <List> ',' <List Item>
        |  <List Item>

<List Item> ::= Identifier