Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7785023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:11:21+00:00 2026-06-01T20:11:21+00:00

I am trying to a simple grammar for parsing python like structures, this is

  • 0

I am trying to a simple grammar for parsing python like structures, this is what I could come up with for a list/set

list : '[' atom ( ',' atom)* ']'
set : '(' atom ( ',' atom)* ']'

atom : 'a'..'z' | 'A'..'Z'
     | '[' list ']'
     | '(' set ')'

Note that this is in antlr, I wanted to know about its correctness and any resources that would help me out

I did look at the python’ grammar http://docs.python.org/reference/grammar.html but couldn’t quite figure out it was handling list of lists or set of lists or list of sets etc..

Any help would appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:11:22+00:00Added an answer on June 1, 2026 at 8:11 pm

    couldn’t quite figure out it was handling list of lists or set of lists or list of sets etc..

    It doesn’t distinguish lists from sets or whatever:

    atom: ('(' [yield_expr|testlist_comp] ')' |
           '[' [listmaker] ']' |
           '{' [dictorsetmaker] '}' |
           '`' testlist1 '`' |
           NAME | NUMBER | STRING+)
    

    The way they handle recursion of the sort you’re describing is that listmaker, dictorsetmaker etc. ultimately may contain atom. For example:

    listmaker: test ( list_for | (',' test)* [','] )
    test: or_test ['if' or_test 'else' test] | lambdef
    or_test: and_test ('or' and_test)*
    and_test: not_test ('and' not_test)*
    not_test: 'not' not_test | comparison
    comparison: expr (comp_op expr)*
    expr: xor_expr ('|' xor_expr)*
    xor_expr: and_expr ('^' and_expr)*
    and_expr: shift_expr ('&' shift_expr)*
    shift_expr: arith_expr (('<<'|'>>') arith_expr)*
    arith_expr: term (('+'|'-') term)*
    term: factor (('*'|'/'|'%'|'//') factor)*
    factor: ('+'|'-'|'~') factor | power
    power: atom trailer* ['**' factor]
    

    There are a lot of intermediates; that’s because they need to establish precedence for a bunch of mathematical operators. Then there list_for, which allows for adding the extra stuff for a list comprehension.

    A much more simplified example might look like:

    atom: ('[' [list_or_set] ']' |
           '{' [list_or_set] '}' |
           NAME | NUMBER | STRING+)
    
    list_or_set: atom (',' atom)* [',']
    

    Or if you want the distinction between lists and sets to be made at this level:

    atom: list | set | NAME | NUMBER | STRING+
    list: '[' atom (',' atom)* [','] ']'
    set: '{' atom (',' atom)* [','] '}'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm OCaml newbie and I'm trying to write a simple OCaml-like grammar, and I
I'm trying to create a simple BaSH-like grammar on ANTLRv3 but haven't been able
I am trying realize python like indent-depending grammar. Source example: ABC QWE CDE EFG
With ANTLR2, you could define something like this in grammar definition file: options {
I am trying this simple tutorial from oracle : http://www.oracle.com/technetwork/java/socket-140484.html (the Example 1 ).
Trying to make simple minesweeper game in python, but have one problem. I have
I'm trying to get the basic of Treetop parsing. Here's a very simple bit
I am trying to define a simple functional language grammar, I am almost done
I've been trying to make a parser for a (very) simple language that looks
I'm trying to write an XML grammar for OkudaKit. I noticed that the bundled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.