1

I'm trying to parse a little pseudo-code I'm writing and having some trouble finding which expression format is actually provided as a string.

I was successful to get it working with regex and tokenizer methods, but the strings will be more in future and i dont want to pass through all via a regex or add more code to tokenizer, instead i though of using lark. I am starter of lark so it was difficult to parse the following string.

'filter:property-insensitive:name:%Name'
'filter:property-insensitive:gender:M:F:U'
'filter:age:on:${today}:Equal:26:years'
'filter:date:Equal:${today}'
'filter:property:Type:regular:temp'
'filter:property:Status:active:unknown'
from lark import Lark

parser = Lark(r"""
        start: greet greet greet
        greet: "filter"
            | ":" -> seperator
            | "property-insensitive" -> key_case
            | ":" -> seperator
    """, parser="lalr")


sample_conf = """filter:property-insensitive:Surname:%Name"""

print(parser.parse(sample_conf).pretty())

Try 2:

import lark
grammar = r'''start: instruction

?instruction: filters_function

FUNCNAME: (LETTER+) (LETTER+|DIGIT+|"_")* // no parentheses allowed in the func name
PROPERTY: (LETTER+) (LETTER+|DIGIT+|"-")*
FIELD: (LETTER+) (LETTER+|DIGIT+|"-"|"_")*
VALUE: (LETTER+) (LETTER+|DIGIT+|"-"|"%")*
ARGSEP: ":" // argument separator
WORDSEP: ":" // word separator
CONDSEP: ":" // condition separator
STAR: "*"
filters_function: FUNCNAME PROPERTY FIELD  VALUE*

%import common.LETTER
%import common.WORD
%import common.DIGIT
%ignore ARGSEP
%ignore WORDSEP
'''

parser = lark.Lark(grammar, parser='earley') # lalr
print(parser.parse("filter:property-insensitive:Surname_name:Name"))

Try 3:

import lark
grammar = r'''start: instruction

?instruction: filters_function
property:"property-insensitive"         -> property_insensitive
        | "property"                    -> property
        | "age"                         -> age_filter
        | "date"                        -> date_filter

FUNCNAME: (LETTER+) (LETTER+|DIGIT+|"_")* // no parentheses allowed in the func name

FIELD: (LETTER+) (LETTER+|DIGIT+|"-"|"_")*
VALUE: (LETTER+) (LETTER+|DIGIT+|"-")*
ARGSEP: ":" // argument separator
WORDSEP: ":" // word separator
CONDSEP: ":" // condition separator
STAR: "*"
filters_function: FUNCNAME property FIELD  VALUE *

%import common.LETTER
%import common.WORD
%import common.DIGIT
%ignore ARGSEP
%ignore WORDSEP
%ignore " "
%ignore "$"
%ignore "{"
%ignore "}"
%ignore "}"
%ignore "0".."9"
'''

parser = lark.Lark(grammar, parser='earley') # lalr
print(parser.parse("filter:property-insensitive:Surname:Name"))
print(parser.parse("filter:property-insensitive:Sex:M:F:U"))
print(parser.parse("filter:age:on:${today}:Equal:26:years"))
print(parser.parse("filter:date:Equal:${today}"))
print(parser.parse("filter:property:Registration_Type:regular:temp"))

Try 3 - Issues:

  • I cant do a OR for the field and values if the property == age | date consider the rest of the values as VALUE

  • If we find the property i want the string to be in the tree output currently it is [] Tree(start, [Tree(filters_function, [Token(FUNCNAME, 'filter'), Tree(property_insensitive, []), Token(FIELD, 'Surname'), Token(VALUE, 'Name')])])

  • As you can see im ignoring few actually values that are meaning full for my equation for now, i need to make find a way to get that in the tree values.

Try 4:

import lark
grammar = r'''start: instruction

?instruction: filters_function

property: "property-insensitive"        -> property_insensitive
        | "property"                    -> property
property_date: "age"                    -> age_filter
             | "date"                   -> date_filter



// To run over special characters
TEXT: (LETTER+) (LETTER+|DIGIT+|"-"|"_")*
FILTER: TEXT
SPECIAL_VALUE: "${" (LETTER+) (LETTER+|DIGIT+|"-"|"_") "}" *
WILD_CARD: "%" TEXT | TEXT | NUMBER
VALUE: WILD_CARD|SPECIAL_VALUE

ARGSEP: ":" // argument separator

STAR: "*"
filters_function: FILTER (property | property_date) VALUE *

// find the whitespace so we can ignore
WHITESPACE: (" " | "\n")+

%ignore WHITESPACE
%import common.LETTER
%import common.WORD
%import common.DIGIT
%import common.INT -> NUMBER
%ignore ARGSEP
%ignore " "
'''

parser = lark.Lark(grammar, parser='earley') # lalr
print(parser.parse("filter:property-insensitive:Surname:%Name"))
print(parser.parse("filter:property-insensitive:Sex:M:F:U"))
print(parser.parse("filter:age:on:${today}:Equal:26:years"))
print(parser.parse("filter:date:Equal:${today}"))
print(parser.parse("filter:property:Registration_Type:regular:temp"))

Output:

Tree(start, [Tree(filters_function, [Token(FILTER, 'filter'), Tree(property_insensitive, []), Token(VALUE, 'Surname'), Token(VALUE, '%Name')])])
Tree(start, [Tree(filters_function, [Token(FILTER, 'filter'), Tree(property_insensitive, []), Token(VALUE, 'Sex'), Token(VALUE, 'M'), Token(VALUE, 'F'), Token(VALUE, 'U')])])
Tree(start, [Tree(filters_function, [Token(FILTER, 'filter'), Tree(age_filter, []), Token(VALUE, 'on'), Token(VALUE, '${today}'), Token(VALUE, 'Equal'), Token(VALUE, '26'), Token(VALUE, 'years')])])
Tree(start, [Tree(filters_function, [Token(FILTER, 'filter'), Tree(date_filter, []), Token(VALUE, 'Equal'), Token(VALUE, '${today}')])])
Tree(start, [Tree(filters_function, [Token(FILTER, 'filter'), Tree(property, []), Token(VALUE, 'Registration_Type'), Token(VALUE, 'regular'), Token(VALUE, 'temp')])])

Issue:

  • I couldn't able to find a way to return the value for the tree Tree(date_filter, [])

Would be nice if i was pointed to a good tutorial for a starter in lark.

DonOfDen
  • 3,968
  • 11
  • 62
  • 112
  • 5
    If you're only looking for tutorials, this question is off-topic. If not, please describe your attempts and how exactly they don't work – ForceBru May 04 '20 at 19:32
  • @ForceBru added my code try here. If thats what needed. Forgot to add them the first time.. – DonOfDen May 04 '20 at 21:11

0 Answers0