简体   繁体   English

python 样式结构的 BNF 文法

[英]BNF Grammar for python style structures

I am trying to a simple grammar for parsing python like structures, this is what I could come up with for a list/set我正在尝试使用一个简单的语法来解析 python 之类的结构,这就是我可以为列表/集合想出的

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请注意,这是在 antlr 中,我想知道它的正确性和任何可以帮助我的资源

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..我确实看过 python 的语法http://docs.python.org/reference/grammar.html但不太明白它是在处理列表列表或列表集或列表集等。

Any help would appreciated.任何帮助将不胜感激。

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 .他们处理您所描述的那种递归的方式是listmakerdictorsetmaker等最终可能包含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.然后list_for ,它允许为列表理解添加额外的东西。

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)* [','] '}'

This might be closer to what you are after:这可能更接近你所追求的:

list : '[' element ( ',' element )* ']';
set : '(' element ( ',' element )* ')';

element: list | set | atom;

alpha:  'a'..'z' | 'A'..'Z' | '_' ;
alphanum: alpha | '0'..'9';
atom : alpha alphanum*;

Note: never used antlr before, this may not be the correct syntax.注意:之前从未使用过 antlr,这可能不是正确的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM